1. package main
    2. import (
    3. "fmt"
    4. "gindemo07/routers"
    5. "html/template"
    6. "time"
    7. "github.com/gin-gonic/gin"
    8. )
    9. //时间戳转换成日期
    10. func UnixToTime(timestamp int) string {
    11. fmt.Println(timestamp)
    12. t := time.Unix(int64(timestamp), 0)
    13. return t.Format("2006-01-02 15:04:05")
    14. }
    15. func main() {
    16. // 创建一个默认的路由引擎
    17. r := gin.Default()
    18. //自定义模板函数 注意要把这个函数放在加载模板前
    19. r.SetFuncMap(template.FuncMap{
    20. "UnixToTime": UnixToTime,
    21. })
    22. //加载模板 放在配置路由前面
    23. r.LoadHTMLGlob("templates/**/*")
    24. //配置静态web目录 第一个参数表示路由, 第二个参数表示映射的目录
    25. r.Static("/static", "./static")
    26. routers.AdminRoutersInit(r)
    27. routers.ApiRoutersInit(r)
    28. routers.DefaultRoutersInit(r)
    29. r.Run()
    30. }
    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 "github.com/gin-gonic/gin"
    3. type IndexController struct {
    4. }
    5. func (con IndexController) Index(c *gin.Context) {
    6. c.String(200, "用户列表--")
    7. }
    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. "net/http"
    4. "github.com/gin-gonic/gin"
    5. )
    6. type DefaultController struct{}
    7. func (con DefaultController) Index(c *gin.Context) {
    8. c.HTML(http.StatusOK, "default/index.html", gin.H{
    9. "msg": "我是一个msg",
    10. })
    11. }
    12. func (con DefaultController) News(c *gin.Context) {
    13. c.String(200, "News")
    14. }
    1. package routers
    2. import (
    3. "gindemo07/controllers/admin"
    4. "github.com/gin-gonic/gin"
    5. )
    6. func AdminRoutersInit(r *gin.Engine) {
    7. adminRouters := r.Group("/admin")
    8. {
    9. adminRouters.GET("/", admin.IndexController{}.Index)
    10. adminRouters.GET("/user", admin.UserController{}.Index)
    11. adminRouters.GET("/user/add", admin.UserController{}.Add)
    12. adminRouters.GET("/user/edit", admin.UserController{}.Edit)
    13. adminRouters.GET("/article", admin.ArticleController{}.Index)
    14. adminRouters.GET("/article/add", admin.ArticleController{}.Add)
    15. adminRouters.GET("/article/edit", admin.ArticleController{}.Edit)
    16. }
    17. }
    1. package routers
    2. import (
    3. "gindemo07/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. "gindemo07/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. }
    1. {{ define "admin/index.html" }}
    2. <!DOCTYPE html>
    3. <html lang="en">
    4. <head>
    5. <meta charset="UTF-8">
    6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    8. <title>Document</title>
    9. </head>
    10. <body>
    11. <h2>这是后台首页</h2>
    12. </body>
    13. </html>
    14. {{ end }}
    1. {{ define "admin/news.html" }}
    2. <!DOCTYPE html>
    3. <html lang="en">
    4. <head>
    5. <meta charset="UTF-8">
    6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    8. <title>Document</title>
    9. </head>
    10. <body>
    11. <h2>后台新闻页面</h2>
    12. </body>
    13. </html>
    14. {{ end }}
    1. <!-- 相当于给模板定义一个名字 define end 成对出现-->
    2. {{ define "default/index.html" }}
    3. <!DOCTYPE html>
    4. <html lang="en">
    5. <head>
    6. <meta charset="UTF-8">
    7. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    8. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    9. <title>Document</title>
    10. <link rel="stylesheet" href="/static/css/base.css">
    11. </head>
    12. <body>
    13. <h1>我是一个首页--index</h1>
    14. <br>
    15. {{.msg}}
    16. </body>
    17. </html>
    18. {{ end }}
    1. <!-- 相当于给模板定义一个名字 define end 成对出现-->
    2. {{ define "default/user.html" }}
    3. <!DOCTYPE html>
    4. <html lang="en">
    5. <head>
    6. <meta charset="UTF-8">
    7. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    8. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    9. <title>Document</title>
    10. <link rel="stylesheet" href="static/css/base.css">
    11. </head>
    12. <body>
    13. <form action="/doAddUser2" method="post">
    14. 用户名:<input type="text" name="username" /> <br><br>
    15. 密码:<input type="password" name="password" /> <br><br>
    16. <input type="submit" value="提交">
    17. </form>
    18. </body>
    19. </html>
    20. {{end}}
    1. <!-- 相当于给模板定义一个名字 define end 成对出现-->
    2. {{ define "public/page_footer.html" }}
    3. <h1>
    4. 我是一个公共的底部
    5. </h1>
    6. {{end}}
    1. <!-- 相当于给模板定义一个名字 define end 成对出现-->
    2. {{ define "public/page_header.html" }}
    3. <h1>
    4. 我是一个公共的标题---{{.title}}
    5. </h1>
    6. {{end}}

    07-gindemo07.7z