123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- package router
- import (
- "cold-delivery/common/middleware"
- handler2 "cold-delivery/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)
- // openapi
- sysOpenapiRouter(g, middleware.ApiKeyAuthMiddleware())
- 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)
- }
- }
- func sysOpenapiRouter(r *gin.RouterGroup, openapiMiddleware gin.HandlerFunc) {
- v1 := r.Group("/openapi/")
- for _, f := range routerOpenapi {
- f(v1, openapiMiddleware)
- }
- }
|