router.go 4.7 KB

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