package router import ( "applet/app/cfg" "applet/app/hdl" internalHdl "applet/app/hdl/zhimeng_internal" platformHdl "applet/app/hdl/zhimeng_platform" "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) routeInternal(r.Group("/api/internal")) routeZhimeng(r.Group("/api/admin")) return r } func routeInternal(r *gin.RouterGroup) { r.Use(mw.DB) // 以下接口需要用到数据库 { r.POST("/playlet_order", internalHdl.GetPlayletOrder) } r.Use(mw.Checker) // 以下接口需要检查Header: platform { } r.Use(mw.Auth) // 以下接口需要JWT验证 { } } func routeZhimeng(r *gin.RouterGroup) { r.GET("/authorizationCode", hdl.GetAuthorizationCode) r.GET("/token", hdl.GetToken) r.POST("/rToken", hdl.RefreshToken) r.Use(mw.AuthJWT, mw.RequestCache) // 以下接口需要JWT验证 { r.POST("/order_list", platformHdl.OrderList) r.POST("/order_output", platformHdl.OrderOutput) r.POST("/withdrawal_income", platformHdl.WithdrawalIncome) r.POST("/withdrawal_bind_alipay", platformHdl.WithdrawalBindAlipay) r.POST("/withdrawal_list", platformHdl.WithdrawalList) r.POST("/withdrawal_doing", platformHdl.WithdrawalDoing) r.POST("/withdrawal_output", platformHdl.WithdrawalOutput) r.POST("/withdrawal_invoice_img", platformHdl.WithdrawalInvoiceImg) } }