router.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package initialize
  2. import (
  3. "github.com/gin-gonic/gin"
  4. swaggerFiles "github.com/swaggo/files"
  5. ginSwagger "github.com/swaggo/gin-swagger"
  6. "net/http"
  7. "os"
  8. "server/docs"
  9. "server/global"
  10. "server/middleware"
  11. "server/router"
  12. )
  13. type justFilesFilesystem struct {
  14. fs http.FileSystem
  15. }
  16. func (fs justFilesFilesystem) Open(name string) (http.File, error) {
  17. f, err := fs.fs.Open(name)
  18. if err != nil {
  19. return nil, err
  20. }
  21. stat, err := f.Stat()
  22. if stat.IsDir() {
  23. return nil, os.ErrPermission
  24. }
  25. return f, nil
  26. }
  27. // 初始化总路由
  28. func Routers() *gin.Engine {
  29. Router := gin.New()
  30. Router.Use(gin.Recovery())
  31. if gin.Mode() == gin.DebugMode {
  32. Router.Use(gin.Logger())
  33. }
  34. InstallPlugin(Router) // 安装插件
  35. systemRouter := router.RouterGroupApp.System
  36. exampleRouter := router.RouterGroupApp.Example
  37. adminRouter := router.RouterGroupApp.Admin
  38. workflowRouter := router.RouterGroupApp.Workflow
  39. storehouseRouter := router.RouterGroupApp.Storehouse
  40. // 如果想要不使用nginx代理前端网页,可以修改 web/.env.production 下的
  41. // VUE_APP_BASE_API = /
  42. // VUE_APP_BASE_PATH = http://localhost
  43. // 然后执行打包命令 npm run build。在打开下面3行注释
  44. //Router.Static("/favicon.ico", "./dist/favicon.ico")
  45. //Router.Static("/assets", "./dist/assets") // dist里面的静态资源
  46. //Router.StaticFile("/", "./dist/index.html") // 前端网页入口页面
  47. Router.StaticFS(global.GVA_CONFIG.Local.StorePath, justFilesFilesystem{http.Dir(global.GVA_CONFIG.Local.StorePath)}) // Router.Use(middleware.LoadTls()) // 如果需要使用https 请打开此中间件 然后前往 core/server.go 将启动模式 更变为 Router.RunTLS("端口","你的cre/pem文件","你的key文件")
  48. // 跨域,如需跨域可以打开下面的注释
  49. // Router.Use(middleware.Cors()) // 直接放行全部跨域请求
  50. // Router.Use(middleware.CorsByRules()) // 按照配置的规则放行跨域请求
  51. //global.GVA_LOG.Info("use middleware cors")
  52. docs.SwaggerInfo.BasePath = global.GVA_CONFIG.System.RouterPrefix
  53. Router.GET(global.GVA_CONFIG.System.RouterPrefix+"/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
  54. global.GVA_LOG.Info("register swagger handler")
  55. // 方便统一添加路由组前缀 多服务器上线使用
  56. PublicGroup := Router.Group(global.GVA_CONFIG.System.RouterPrefix)
  57. {
  58. // 健康监测
  59. PublicGroup.GET("/health", func(c *gin.Context) {
  60. c.JSON(http.StatusOK, "ok")
  61. })
  62. }
  63. {
  64. systemRouter.InitBaseRouter(PublicGroup) // 注册基础功能路由 不做鉴权
  65. }
  66. PrivateGroup := Router.Group(global.GVA_CONFIG.System.RouterPrefix)
  67. PrivateGroup.Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
  68. {
  69. systemRouter.InitApiRouter(PrivateGroup, PublicGroup) // 注册功能api路由
  70. systemRouter.InitJwtRouter(PrivateGroup) // jwt相关路由
  71. systemRouter.InitUserRouter(PrivateGroup, PublicGroup) // 注册用户路由
  72. systemRouter.InitMenuRouter(PrivateGroup) // 注册menu路由
  73. systemRouter.InitCasbinRouter(PrivateGroup) // 权限相关路由
  74. systemRouter.InitAuthorityRouter(PrivateGroup) // 注册角色路由
  75. systemRouter.InitSysDictionaryRouter(PrivateGroup) // 字典管理
  76. systemRouter.InitSysOperationRecordRouter(PrivateGroup) // 操作记录
  77. systemRouter.InitSysDictionaryDetailRouter(PrivateGroup) // 字典详情管理
  78. systemRouter.InitAuthorityBtnRouterRouter(PrivateGroup) // 字典详情管理
  79. systemRouter.InitNoticeRouter(PrivateGroup) //通知
  80. exampleRouter.InitFileUploadAndDownloadRouter(PrivateGroup)
  81. adminRouter.InitDepartmentRouter(PublicGroup)
  82. adminRouter.InitProjectRouter(PrivateGroup)
  83. adminRouter.InitFileRouter(PrivateGroup)
  84. adminRouter.InitFinanceRouter(PrivateGroup)
  85. adminRouter.InitConstructorRouter(PrivateGroup)
  86. adminRouter.InitExpensesRouter(PrivateGroup)
  87. workflowRouter.InitApproverRouter(PrivateGroup)
  88. workflowRouter.InitNodeRouter(PrivateGroup)
  89. workflowRouter.InitProcessRouter(PrivateGroup)
  90. workflowRouter.InitDescriptionRouter(PrivateGroup)
  91. workflowRouter.InitProjectProcessRouter(PrivateGroup)
  92. storehouseRouter.InitSupplierRouter(PrivateGroup)
  93. storehouseRouter.InitWarehouseRouter(PrivateGroup)
  94. storehouseRouter.InitCommodityRouter(PrivateGroup)
  95. storehouseRouter.InitCommodityGenreRouter(PrivateGroup)
  96. storehouseRouter.InitPlaceRouter(PrivateGroup)
  97. storehouseRouter.InitStorageAreaRouter(PrivateGroup)
  98. storehouseRouter.InitStorageAreaGenreRouter(PrivateGroup)
  99. }
  100. global.GVA_LOG.Info("router register success")
  101. return Router
  102. }