|
- package router
-
- import (
- "applet/app/cfg"
- "applet/app/mw"
- _ "applet/docs"
- "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)
- return r
- }
|