package router import ( "applet/app/cfg" "applet/app/hdl" "applet/app/mw" "github.com/gin-gonic/gin" ) //初始化路由 func Init() *gin.Engine { // debug, release, test 项目阶段 mode := "release" if cfg.Debug { mode = "debug" } gin.SetMode(mode) //创建一个新的启动器 r := gin.New() r.Use(mw.ChangeHeader) // 是否打印访问日志, 在非正式环境都打印 if mode != "release" { r.Use(gin.Logger()) } r.Use(gin.Recovery()) // r.Use(mw.Limiter) //r.LoadHTMLGlob("static/html/*") r.GET("/favicon.ico", func(c *gin.Context) { c.Status(204) }) r.NoRoute(func(c *gin.Context) { c.JSON(404, gin.H{"code": 404, "msg": "page not found", "data": []struct{}{}}) }) r.NoMethod(func(c *gin.Context) { c.JSON(405, gin.H{"code": 405, "msg": "method not allowed", "data": []struct{}{}}) }) r.Use(mw.Cors) route(r.Group("/api/v1")) return r } func route(r *gin.RouterGroup) { r.Any("/demo", hdl.Demo) r.POST("/login", hdl.Login) r.Group("/wx") { r.Use(mw.DB) // 微信公众号消息通知 r.GET("/msgReceive", hdl.WXCheckSignature) r.POST("/msgReceive", hdl.WXMsgReceive) } r.Use(mw.DB) // 以下接口需要用到数据库 { r.GET("/demo1", hdl.Demo1) } r.Use(mw.Checker) // 以下接口需要检查Header: platform { } r.GET("/qrcodeBatchDownload", hdl.QrcodeBatchDownload) //二维码批次-下载 r.Use(mw.Auth) // 以下接口需要JWT验证 { r.GET("/userInfo", hdl.UserInfo) //用户信息 r.GET("/sysCfg", hdl.GetSysCfg) //基础配置-获取 r.POST("/sysCfg", hdl.SetSysCfg) //基础配置-设置 r.POST("/qrcodeBatchList", hdl.QrcodeBatchList) //二维码批次-列表 r.GET("/getBatchAddName", hdl.GetBatchAddName) //二维码批次-自动获取添加时名称 r.POST("/qrcodeBatchAdd", hdl.QrcodeBatchAdd) //二维码批次-添加 r.GET("/qrcodeBatchDetail", hdl.QrcodeBatchDetail) //二维码批次-详情 r.DELETE("/qrcodeBatchDelete/:id", hdl.QrcodeBatchDelete) //二维码批次-删除 } }