sys_router.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package router
  2. import (
  3. "go-admin/app/admin/apis"
  4. "mime"
  5. "net/http"
  6. "os"
  7. "github.com/go-admin-team/go-admin-core/sdk/config"
  8. "github.com/gin-gonic/gin"
  9. jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
  10. ginSwagger "github.com/swaggo/gin-swagger"
  11. swaggerfiles "github.com/swaggo/files"
  12. "go-admin/common/middleware"
  13. "go-admin/common/middleware/handler"
  14. _ "go-admin/docs/admin"
  15. )
  16. func InitSysRouter(r *gin.Engine, authMiddleware *jwt.GinJWTMiddleware) *gin.RouterGroup {
  17. g := r.Group("")
  18. sysBaseRouter(g)
  19. // 静态文件
  20. sysStaticFileRouter(g)
  21. // swagger;注意:生产环境可以注释掉
  22. if config.ApplicationConfig.Mode != "prod" {
  23. sysSwaggerRouter(g)
  24. }
  25. // 需要认证
  26. sysCheckRoleRouterInit(g, authMiddleware)
  27. return g
  28. }
  29. func sysBaseRouter(r *gin.RouterGroup) {
  30. // go ws.WebsocketManager.Start()
  31. // go ws.WebsocketManager.SendService()
  32. // go ws.WebsocketManager.SendAllService()
  33. if config.ApplicationConfig.Mode != "prod" {
  34. r.GET("/", apis.GoAdmin)
  35. }
  36. r.GET("/info", handler.Ping)
  37. }
  38. func sysStaticFileRouter(r *gin.RouterGroup) {
  39. err := mime.AddExtensionType(".js", "application/javascript")
  40. if err != nil {
  41. return
  42. }
  43. r.Static("/static", "./static")
  44. if config.ApplicationConfig.Mode != "prod" {
  45. r.Static("/form-generator", "./static/form-generator")
  46. }
  47. }
  48. func sysSwaggerRouter(r *gin.RouterGroup) {
  49. r.GET("/swagger/admin/*any", ginSwagger.WrapHandler(swaggerfiles.NewHandler(), ginSwagger.InstanceName("admin")))
  50. }
  51. func sysCheckRoleRouterInit(r *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
  52. // wss := r.Group("").Use(authMiddleware.MiddlewareFunc())
  53. // {
  54. // wss.GET("/ws/:id/:channel", ws.WebsocketManager.WsClient)
  55. // wss.GET("/wslogout/:id/:channel", ws.WebsocketManager.UnWsClient)
  56. // }
  57. v1 := r.Group("/api/v1")
  58. {
  59. v1.POST("/login", authMiddleware.LoginHandler)
  60. // Refresh time can be longer than token timeout
  61. v1.GET("/refresh_token", authMiddleware.RefreshHandler)
  62. v1.GET("/login/pubkey", func(ctx *gin.Context) {
  63. bs, err := os.ReadFile("id_rsa.pub")
  64. if err != nil {
  65. ctx.JSON(http.StatusInternalServerError, map[string]any{
  66. "code": http.StatusInternalServerError,
  67. "data": "",
  68. "msg": err.Error(),
  69. })
  70. return
  71. }
  72. ctx.JSON(http.StatusOK, map[string]any{
  73. "code": http.StatusOK,
  74. "data": string(bs),
  75. "msg": "success",
  76. })
  77. })
  78. }
  79. registerBaseRouter(v1, authMiddleware)
  80. }
  81. func registerBaseRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
  82. api := apis.SysMenu{}
  83. api2 := apis.SysDept{}
  84. v1auth := v1.Group("").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole())
  85. {
  86. v1auth.GET("/roleMenuTreeselect/:roleId", api.GetMenuTreeSelect)
  87. //v1.GET("/menuTreeselect", api.GetMenuTreeSelect)
  88. v1auth.GET("/roleDeptTreeselect/:roleId", api2.GetDeptTreeRoleSelect)
  89. v1auth.POST("/logout", handler.LogOut)
  90. }
  91. }