router.go 989 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package router
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth"
  5. jwt "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth"
  6. )
  7. var (
  8. routerNoCheckRole = make([]func(*gin.RouterGroup), 0)
  9. routerCheckRole = make([]func(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware), 0)
  10. )
  11. // 路由示例
  12. func InitExamplesRouter(r *gin.Engine, authMiddleware *jwt.GinJWTMiddleware) *gin.Engine {
  13. // 无需认证的路由
  14. NoCheckRoleRouter(r)
  15. // 需要认证的路由
  16. CheckRoleRouter(r, authMiddleware)
  17. return r
  18. }
  19. // 无需认证的路由示例
  20. func NoCheckRoleRouter(r *gin.Engine) {
  21. // 可根据业务需求来设置接口版本
  22. v1 := r.Group("/api")
  23. for _, f := range routerNoCheckRole {
  24. f(v1)
  25. }
  26. }
  27. // 需要认证的路由示例
  28. func CheckRoleRouter(r *gin.Engine, authMiddleware *jwtauth.GinJWTMiddleware) {
  29. // 可根据业务需求来设置接口版本
  30. v1 := r.Group("/api")
  31. for _, f := range routerCheckRole {
  32. f(v1, authMiddleware)
  33. }
  34. }