router.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/utils"
  16. "strings"
  17. )
  18. var NginxR = "/nginx"
  19. var NginxGetR = "/nginx/:id"
  20. var NginxRefreshR = "/nginx/:id/http/refresh"
  21. var NginxStartR = "/nginx/:id/start"
  22. var NginxStopR = "/nginx/:id/stop"
  23. var NginxStatusR = "/nginx/:id/status"
  24. func init() {
  25. config := config2.Config
  26. userController := user.NewUserController()
  27. logs.Info("baseApi", config.BaseApi)
  28. ns := beego.NewNamespace(config.BaseApi,
  29. beego.NSRouter(NginxR, &nginx_controller.NginxController{}),
  30. beego.NSRouter(NginxGetR, &nginx_controller.NginxController{}, "post:Update"),
  31. beego.NSRouter(NginxGetR, &nginx_controller.NginxController{}, "get:GetNginx"),
  32. beego.NSRouter(NginxGetR, &nginx_controller.NginxController{}, "delete:DelNginx"),
  33. beego.NSRouter(NginxRefreshR, &nginx_controller.NginxController{}, "post:RefreshHttp"),
  34. beego.NSRouter(NginxStartR, &nginx_controller.NginxController{}, "post:StartNginx"),
  35. beego.NSRouter(NginxStopR, &nginx_controller.NginxController{}, "post:StopNginx"),
  36. beego.NSRouter(NginxStatusR, &nginx_controller.NginxController{}, "post:StatusNginx"),
  37. // certs
  38. beego.NSRouter("/nginx/:id/certs", &nginx_controller.CertController{}),
  39. beego.NSRouter("/nginx/:id/certs/sync", &nginx_controller.CertController{}, "post:Sync"),
  40. // nginx server apis
  41. beego.NSRouter("/nginx/:id/server", &nginx_controller.ServerController{}),
  42. beego.NSRouter("/nginx/:id/server/refresh", &nginx_controller.ServerController{}, "post:Refresh"),
  43. // file upload download
  44. beego.NSRouter("/nginx/:id/file/deploy", &nginx_controller.FileController{}, "post:Deploy"),
  45. beego.NSRouter("/file", &nginx_controller.FileController{}),
  46. beego.NSRouter("/logger", &nginx_controller.LoggerController{}),
  47. beego.NSRouter("/user/login", userController, "post:Login"),
  48. beego.NSRouter("/user/logout", userController, "post:Logout"),
  49. beego.NSRouter("/user/info", userController, "get:User"),
  50. beego.NSRouter("/user/info", userController, "post:UpdateUserInfo"),
  51. beego.NSRouter("/user/register", userController, "post:Register"),
  52. beego.NSRouter("/user/list", userController, "post:Users"),
  53. beego.NSRouter("/user/detail", userController, "get:GetDetail"),
  54. beego.NSRouter("/user/save", userController, "post:Update"),
  55. beego.NSRouter("/user/modifyPassword", userController, "post:UpdatePassword"),
  56. beego.NSRouter("/user/resetPassword", userController, "post:ResetPassword"),
  57. beego.NSRouter("/oauth2", &oauth2.Controller{}),
  58. beego.NSRouter("/oauth2/callback", &oauth2.Controller{}, "post:Callback"),
  59. // settings
  60. beego.NSRouter("/settings/list", &settings.SettingController{}, "post:List"),
  61. beego.NSRouter("/settings/save", &settings.SettingController{}, "post:Save"),
  62. beego.NSRouter("/settings/delete", &settings.SettingController{}, "post:Delete"),
  63. )
  64. beego.AddNamespace(ns)
  65. // LDAP路由
  66. beego.AddNamespace(ldap.InitRouter(config.BaseApi))
  67. beego.InsertFilter(fmt.Sprintf("%s/**", config.BaseApi), beego.BeforeRouter, middleware.AuthFilter)
  68. beego.Router(fmt.Sprintf("%s/config.js", config.ContextPath), &nginx_controller.ConfigController{})
  69. // portal static assets
  70. if utils.IsExist("static/web") {
  71. beego.SetStaticPath(config.ContextPath, "static/web")
  72. } else if utils.IsExist("frontend/dist") {
  73. beego.SetStaticPath(config.ContextPath, "frontend/dist")
  74. } else {
  75. beego.SetStaticPath(config.ContextPath, "static/web")
  76. logs.Warn("no static web dir for static/web or frontend/dist, please check")
  77. }
  78. //
  79. webPrefix := config.ContextPath
  80. if !strings.HasSuffix(webPrefix, "/") {
  81. webPrefix = webPrefix + "/"
  82. }
  83. beego.Get("/", func(ctx *context.Context) {
  84. ctx.Redirect(301, webPrefix)
  85. })
  86. beego.ErrorController(&base.ErrorController{
  87. WebPath: webPrefix,
  88. })
  89. }