router.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. beego.NSRouter("/user/login", userController, "post:Login"),
  49. beego.NSRouter("/user/logout", userController, "post:Logout"),
  50. beego.NSRouter("/user/info", userController, "get:User"),
  51. beego.NSRouter("/user/info", userController, "post:UpdateUserInfo"),
  52. beego.NSRouter("/user/register", userController, "post:Register"),
  53. beego.NSRouter("/user/list", userController, "post:Users"),
  54. beego.NSRouter("/user/detail", userController, "get:GetDetail"),
  55. beego.NSRouter("/user/save", userController, "post:Update"),
  56. beego.NSRouter("/user/modifyPassword", userController, "post:UpdatePassword"),
  57. beego.NSRouter("/user/resetPassword", userController, "post:ResetPassword"),
  58. beego.NSRouter("/oauth2", &oauth2.Controller{}),
  59. beego.NSRouter("/oauth2/callback", &oauth2.Controller{}, "post:Callback"),
  60. // settings
  61. beego.NSRouter("/settings/list", &settings.SettingController{}, "post:List"),
  62. beego.NSRouter("/settings/save", &settings.SettingController{}, "post:Save"),
  63. beego.NSRouter("/settings/delete", &settings.SettingController{}, "post:Delete"),
  64. // 其它设置
  65. beego.NSRouter("/wechat/webhook/gitlab", &wechat.Controller{}, "post:WebhookFromGitlab"),
  66. )
  67. beego.AddNamespace(ns)
  68. // LDAP路由
  69. beego.AddNamespace(ldap.InitRouter(config.BaseApi))
  70. beego.InsertFilter(fmt.Sprintf("%s/**", config.BaseApi), beego.BeforeRouter, middleware.AuthFilter)
  71. beego.Router(fmt.Sprintf("%s/config.js", config.ContextPath), &nginx_controller.ConfigController{})
  72. // portal static assets
  73. if utils.IsExist("static/web") {
  74. beego.SetStaticPath(config.ContextPath, "static/web")
  75. } else if utils.IsExist("frontend/dist") {
  76. beego.SetStaticPath(config.ContextPath, "frontend/dist")
  77. } else {
  78. beego.SetStaticPath(config.ContextPath, "static/web")
  79. logs.Warn("no static web dir for static/web or frontend/dist, please check")
  80. }
  81. //
  82. webPrefix := config.ContextPath
  83. if !strings.HasSuffix(webPrefix, "/") {
  84. webPrefix = webPrefix + "/"
  85. }
  86. beego.Get("/", func(ctx *context.Context) {
  87. ctx.Redirect(301, webPrefix)
  88. })
  89. beego.ErrorController(&base.ErrorController{
  90. WebPath: webPrefix,
  91. })
  92. }