package router import ( "cold-logistics/common/middleware" handler2 "cold-logistics/common/middleware/handler" "github.com/gin-gonic/gin" swaggerfiles "github.com/swaggo/files" ginSwagger "github.com/swaggo/gin-swagger" jwt "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth" "gogs.baozhida.cn/zoie/OAuth-core/sdk/config" "os" log "gogs.baozhida.cn/zoie/OAuth-core/logger" "gogs.baozhida.cn/zoie/OAuth-core/sdk" ) // InitRouter 路由初始化,不要怀疑,这里用到了 func InitRouter() { var r *gin.Engine h := sdk.Runtime.GetEngine() if h == nil { log.Fatal("not found engine...") os.Exit(-1) } switch h.(type) { case *gin.Engine: r = h.(*gin.Engine) default: log.Fatal("not support other engine") os.Exit(-1) } // the jwt middleware authMiddleware, err := middleware.AuthInit() if err != nil { log.Fatalf("JWT Init Error, %s", err.Error()) } // 注册系统路由 InitSysRouter(r, authMiddleware) } func InitSysRouter(r *gin.Engine, authMiddleware *jwt.GinJWTMiddleware) *gin.RouterGroup { g := r.Group("") // 静态文件 // sysStaticFileRouter(g) // swagger;注意:生产环境可以注释掉 if config.ApplicationConfig.Mode != "prod" { sysSwaggerRouter(g) } // 需要认证 sysCheckRoleRouterInit(g, authMiddleware) return g } func sysSwaggerRouter(r *gin.RouterGroup) { //r.GET("/swagger/admin/*any", ginSwagger.WrapHandler(swaggerfiles.NewHandler(), ginSwagger.InstanceName("admin"))) //r.GET("/swagger/*any", func(c *gin.Context) { // ginSwagger.DisablingWrapHandler(swaggerfiles.Handler, "SWAGGER")(c) //}) r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler)) } func sysCheckRoleRouterInit(r *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) { v1 := r.Group("/api/") { v1.POST("/login", authMiddleware.LoginHandler) // Refresh time can be longer than token timeout v1.GET("/refresh_token", authMiddleware.RefreshHandler) } for _, f := range routerCheckRole { f(v1, authMiddleware) } for _, f := range routerNoCheckRole { f(v1) } registerBaseRouter(v1, authMiddleware) } func registerBaseRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) { v1auth := v1.Group("").Use(authMiddleware.MiddlewareFunc()) { v1auth.POST("/logout", handler2.LogOut) } }