router.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package routers
  2. import (
  3. "fmt"
  4. "github.com/astaxie/beego"
  5. "github.com/astaxie/beego/context"
  6. "github.com/astaxie/beego/logs"
  7. "nginx-ui/server/base"
  8. config2 "nginx-ui/server/config"
  9. "nginx-ui/server/middleware"
  10. "nginx-ui/server/modules/ldap"
  11. "nginx-ui/server/modules/nginx/nginx_controller"
  12. "nginx-ui/server/modules/oauth2"
  13. "nginx-ui/server/modules/settings"
  14. "nginx-ui/server/modules/user"
  15. "nginx-ui/server/modules/wechat"
  16. "nginx-ui/server/utils"
  17. "strings"
  18. )
  19. var NginxR = "/nginx"
  20. var NginxGetR = "/nginx/:id"
  21. var NginxRefreshR = "/nginx/:id/http/refresh"
  22. var NginxStartR = "/nginx/:id/start"
  23. var NginxStopR = "/nginx/:id/stop"
  24. var NginxStatusR = "/nginx/:id/status"
  25. func init() {
  26. config := config2.Config
  27. userController := user.NewUserController()
  28. logs.Info("baseApi", config.BaseApi)
  29. ns := beego.NewNamespace(config.BaseApi,
  30. beego.NSRouter(NginxR, &nginx_controller.NginxController{}),
  31. beego.NSRouter(NginxGetR, &nginx_controller.NginxController{}, "post:Update"),
  32. beego.NSRouter(NginxGetR, &nginx_controller.NginxController{}, "get:GetNginx"),
  33. beego.NSRouter(NginxGetR, &nginx_controller.NginxController{}, "delete:DelNginx"),
  34. beego.NSRouter(NginxRefreshR, &nginx_controller.NginxController{}, "post:RefreshHttp"),
  35. beego.NSRouter(NginxStartR, &nginx_controller.NginxController{}, "post:StartNginx"),
  36. beego.NSRouter(NginxStopR, &nginx_controller.NginxController{}, "post:StopNginx"),
  37. beego.NSRouter(NginxStatusR, &nginx_controller.NginxController{}, "post:StatusNginx"),
  38. // certs
  39. beego.NSRouter("/nginx/:id/certs", &nginx_controller.CertController{}),
  40. beego.NSRouter("/nginx/:id/certs/sync", &nginx_controller.CertController{}, "post:Sync"),
  41. // nginx server apis
  42. beego.NSRouter("/nginx/:id/server", &nginx_controller.ServerController{}),
  43. beego.NSRouter("/nginx/:id/server/refresh", &nginx_controller.ServerController{}, "post:Refresh"),
  44. // file upload download
  45. beego.NSRouter("/nginx/:id/file/deploy", &nginx_controller.FileController{}, "post:Deploy"),
  46. beego.NSRouter("/file", &nginx_controller.FileController{}),
  47. beego.NSRouter("/logger", &nginx_controller.LoggerController{}),
  48. // 角色
  49. beego.NSRouter("/role/list", &user.RoleController{}, "post:List"),
  50. beego.NSRouter("/role/save", &user.RoleController{}, "post:Save"),
  51. beego.NSRouter("/role/delete", &user.RoleController{}, "post:Delete"),
  52. // 用户
  53. beego.NSRouter("/user/login", userController, "post:Login"),
  54. beego.NSRouter("/user/logout", userController, "post:Logout"),
  55. beego.NSRouter("/user/info", userController, "get:User"),
  56. beego.NSRouter("/user/info", userController, "post:UpdateUserInfo"),
  57. beego.NSRouter("/user/register", userController, "post:Register"),
  58. beego.NSRouter("/user/list", userController, "post:Users"),
  59. beego.NSRouter("/user/detail", userController, "get:GetDetail"),
  60. beego.NSRouter("/user/save", userController, "post:Update"),
  61. beego.NSRouter("/user/modifyPassword", userController, "post:UpdatePassword"),
  62. beego.NSRouter("/user/resetPassword", userController, "post:ResetPassword"),
  63. beego.NSRouter("/oauth2", &oauth2.Controller{}),
  64. beego.NSRouter("/oauth2/callback", &oauth2.Controller{}, "post:Callback"),
  65. // settings
  66. beego.NSRouter("/settings/list", &settings.SettingController{}, "post:List"),
  67. beego.NSRouter("/settings/save", &settings.SettingController{}, "post:Save"),
  68. beego.NSRouter("/settings/delete", &settings.SettingController{}, "post:Delete"),
  69. // 路由
  70. beego.NSRouter("/settings/route/list", &settings.RouteController{}, "post:List"),
  71. beego.NSRouter("/settings/route/save", &settings.RouteController{}, "post:Save"),
  72. beego.NSRouter("/settings/route/delete", &settings.RouteController{}, "post:Delete"),
  73. // 其它设置
  74. beego.NSRouter("/wechat/webhook/gitlab", &wechat.Controller{}, "post:WebhookFromGitlab"),
  75. )
  76. beego.AddNamespace(ns)
  77. // LDAP路由
  78. beego.AddNamespace(ldap.InitRouter(config.BaseApi))
  79. beego.InsertFilter(fmt.Sprintf("%s/**", config.BaseApi), beego.BeforeRouter, middleware.AuthFilter)
  80. beego.Router(fmt.Sprintf("%s/config.js", config.ContextPath), &nginx_controller.ConfigController{})
  81. // portal static assets
  82. if utils.IsExist("static/web") {
  83. beego.SetStaticPath(config.ContextPath, "static/web")
  84. } else if utils.IsExist("frontend/dist") {
  85. beego.SetStaticPath(config.ContextPath, "frontend/dist")
  86. } else {
  87. beego.SetStaticPath(config.ContextPath, "static/web")
  88. logs.Warn("no static web dir for static/web or frontend/dist, please check")
  89. }
  90. //
  91. webPrefix := config.ContextPath
  92. if !strings.HasSuffix(webPrefix, "/") {
  93. webPrefix = webPrefix + "/"
  94. }
  95. beego.Get("/", func(ctx *context.Context) {
  96. ctx.Redirect(301, webPrefix)
  97. })
  98. beego.ErrorController(&base.ErrorController{
  99. WebPath: webPrefix,
  100. })
  101. }