-
Notifications
You must be signed in to change notification settings - Fork 93
/
routers.go
54 lines (42 loc) · 1.24 KB
/
routers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"morningo/config"
"morningo/filters"
"morningo/filters/auth"
routeRegister "morningo/routes"
"net/http"
// proxy "github.com/chenhg5/gin-reverseproxy"
)
func initRouter() *gin.Engine {
router := gin.New()
router.LoadHTMLGlob(config.GetEnv().TemplatePath + "/*") // html模板
if config.GetEnv().Debug {
pprof.Register(router) // 性能分析工具
}
router.Use(gin.Logger())
router.Use(handleErrors()) // 错误处理
router.Use(filters.RegisterSession()) // 全局session
router.Use(filters.RegisterCache()) // 全局cache
router.Use(auth.RegisterGlobalAuthDriver("cookie", "web_auth")) // 全局auth cookie
router.Use(auth.RegisterGlobalAuthDriver("jwt", "jwt_auth")) // 全局auth jwt
router.NoRoute(func(c *gin.Context) {
c.JSON(http.StatusNotFound, gin.H{
"code": 404,
"msg": "找不到该路由",
})
})
router.NoMethod(func(c *gin.Context) {
c.JSON(http.StatusNotFound, gin.H{
"code": 404,
"msg": "找不到该方法",
})
})
routeRegister.RegisterApiRouter(router)
// ReverseProxy
// router.Use(proxy.ReverseProxy(map[string] string {
// "localhost:4000" : "localhost:9090",
// }))
return router
}