1. package main
    2. import (
    3. "gindemo09/models"
    4. "gindemo09/routers"
    5. "html/template"
    6. "github.com/gin-gonic/gin"
    7. )
    8. func main() {
    9. // 创建一个默认的路由引擎
    10. r := gin.Default()
    11. //自定义模板函数 注意要把这个函数放在加载模板前
    12. r.SetFuncMap(template.FuncMap{
    13. "UnixToTime": models.UnixToTime,
    14. })
    15. //加载模板 放在配置路由前面
    16. r.LoadHTMLGlob("templates/**/*")
    17. //配置静态web目录 第一个参数表示路由, 第二个参数表示映射的目录
    18. r.Static("/static", "./static")
    19. routers.AdminRoutersInit(r)
    20. routers.ApiRoutersInit(r)
    21. routers.DefaultRoutersInit(r)
    22. r.Run()
    23. }
    1. package admin
    2. import (
    3. "net/http"
    4. "github.com/gin-gonic/gin"
    5. )
    6. type ArticleController struct {
    7. BaseController
    8. }
    9. func (con ArticleController) Index(c *gin.Context) {
    10. // c.String(http.StatusOK, "文章列表--")
    11. con.success(c)
    12. }
    13. func (con ArticleController) Add(c *gin.Context) {
    14. c.String(http.StatusOK, "-add--文章-")
    15. }
    16. func (con ArticleController) Edit(c *gin.Context) {
    17. c.String(http.StatusOK, "-Edit---文章---")
    18. }
    1. package admin
    2. import "github.com/gin-gonic/gin"
    3. type BaseController struct{}
    4. func (con BaseController) success(c *gin.Context) {
    5. c.String(200, "成功")
    6. }
    7. func (con BaseController) error(c *gin.Context) {
    8. c.String(200, "失败")
    9. }
    1. package admin
    2. import (
    3. "fmt"
    4. "github.com/gin-gonic/gin"
    5. )
    6. type IndexController struct {
    7. }
    8. func (con IndexController) Index(c *gin.Context) {
    9. username, _ := c.Get("username")
    10. fmt.Println(username)
    11. //类型断言
    12. v, ok := username.(string)
    13. if ok {
    14. c.String(200, "用户列表--"+v)
    15. } else {
    16. c.String(200, "用户列表--获取用户失败")
    17. }
    18. }
    1. package admin
    2. import "github.com/gin-gonic/gin"
    3. type UserController struct {
    4. BaseController
    5. }
    6. func (con UserController) Index(c *gin.Context) {
    7. // c.String(200, "用户列表--")
    8. con.success(c)
    9. }
    10. func (con UserController) Add(c *gin.Context) {
    11. c.String(200, "用户列表-add---")
    12. }
    13. func (con UserController) Edit(c *gin.Context) {
    14. c.String(200, "用户列表-Edit------")
    15. }
    1. package api
    2. import "github.com/gin-gonic/gin"
    3. type ApiController struct{}
    4. func (con ApiController) Index(c *gin.Context) {
    5. c.String(200, "我是一个api接口")
    6. }
    7. func (con ApiController) Userlist(c *gin.Context) {
    8. c.String(200, "我是一个api接口-Userlist")
    9. }
    10. func (con ApiController) Plist(c *gin.Context) {
    11. c.String(200, "我是一个api接口-Plist")
    12. }
    1. package itying
    2. import (
    3. "fmt"
    4. "gindemo09/models"
    5. "net/http"
    6. "github.com/gin-gonic/gin"
    7. )
    8. type DefaultController struct{}
    9. func (con DefaultController) Index(c *gin.Context) {
    10. fmt.Println(models.UnixToTime(1629788564))
    11. c.HTML(http.StatusOK, "default/index.html", gin.H{
    12. "msg": "我是一个msg",
    13. "t": 1629788418,
    14. })
    15. }
    16. func (con DefaultController) News(c *gin.Context) {
    17. c.String(200, "News")
    18. }
    1. package middlewares
    2. import (
    3. "fmt"
    4. "time"
    5. "github.com/gin-gonic/gin"
    6. )
    7. func InitMiddleware(c *gin.Context) {
    8. //判断用户是否登录
    9. fmt.Println(time.Now())
    10. fmt.Println(c.Request.URL)
    11. c.Set("username", "张三")
    12. //定义一个goroutine统计日志 当在中间件或 handler 中启动新的 goroutine 时,不能使用原始的上下文(c *gin.Context), 必须使用其只读副本(c.Copy())
    13. cCp := c.Copy()
    14. go func() {
    15. time.Sleep(2 * time.Second)
    16. fmt.Println("Done! in path " + cCp.Request.URL.Path)
    17. }()
    18. }
    1. package models
    2. import (
    3. "time"
    4. )
    5. //时间戳转换成日期
    6. func UnixToTime(timestamp int) string {
    7. t := time.Unix(int64(timestamp), 0)
    8. return t.Format("2006-01-02 15:04:05")
    9. }
    10. //日期转换成时间戳 2020-05-02 15:04:05
    11. func DateToUnix(str string) int64 {
    12. template := "2006-01-02 15:04:05"
    13. t, err := time.ParseInLocation(template, str, time.Local)
    14. if err != nil {
    15. return 0
    16. }
    17. return t.Unix()
    18. }
    19. //获取时间戳
    20. func GetUnix() int64 {
    21. return time.Now().Unix()
    22. }
    23. //获取当前的日期
    24. func GetDate() string {
    25. template := "2006-01-02 15:04:05"
    26. return time.Now().Format(template)
    27. }
    28. //获取年月日
    29. func GetDay() string {
    30. template := "20060102"
    31. return time.Now().Format(template)
    32. }
    1. package routers
    2. import (
    3. "gindemo09/controllers/admin"
    4. "gindemo09/middlewares"
    5. "github.com/gin-gonic/gin"
    6. )
    7. func AdminRoutersInit(r *gin.Engine) {
    8. //middlewares.InitMiddleware中间件
    9. adminRouters := r.Group("/admin", middlewares.InitMiddleware)
    10. {
    11. adminRouters.GET("/", admin.IndexController{}.Index)
    12. adminRouters.GET("/user", admin.UserController{}.Index)
    13. adminRouters.GET("/user/add", admin.UserController{}.Add)
    14. adminRouters.GET("/user/edit", admin.UserController{}.Edit)
    15. adminRouters.GET("/article", admin.ArticleController{}.Index)
    16. adminRouters.GET("/article/add", admin.ArticleController{}.Add)
    17. adminRouters.GET("/article/edit", admin.ArticleController{}.Edit)
    18. }
    19. }
    1. package routers
    2. import (
    3. "gindemo09/controllers/api"
    4. "github.com/gin-gonic/gin"
    5. )
    6. func ApiRoutersInit(r *gin.Engine) {
    7. apiRouters := r.Group("/api")
    8. {
    9. apiRouters.GET("/", api.ApiController{}.Index)
    10. apiRouters.GET("/userlist", api.ApiController{}.Userlist)
    11. apiRouters.GET("/plist", api.ApiController{}.Plist)
    12. }
    13. }
    1. package routers
    2. import (
    3. "gindemo09/controllers/itying"
    4. "github.com/gin-gonic/gin"
    5. )
    6. func DefaultRoutersInit(r *gin.Engine) {
    7. defaultRouters := r.Group("/")
    8. {
    9. defaultRouters.GET("/", itying.DefaultController{}.Index)
    10. defaultRouters.GET("/news", itying.DefaultController{}.News)
    11. }
    12. }

    09-gindemo09.7z