router.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package routers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/astaxie/beego"
  6. "github.com/astaxie/beego/context"
  7. "github.com/astaxie/beego/logs"
  8. "net/http"
  9. config2 "nginx-ui/server/config"
  10. "nginx-ui/server/middleware"
  11. "nginx-ui/server/models"
  12. "nginx-ui/server/modules/ldap"
  13. "nginx-ui/server/modules/nginx/nginx_controller"
  14. "nginx-ui/server/modules/oauth2"
  15. "nginx-ui/server/modules/settings"
  16. "nginx-ui/server/modules/user"
  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.AddNamespace(ns)
  66. // LDAP路由
  67. beego.AddNamespace(ldap.InitRouter(config.BaseApi))
  68. beego.InsertFilter(fmt.Sprintf("%s/**", config.BaseApi), beego.BeforeRouter, middleware.AuthFilter)
  69. beego.Router(fmt.Sprintf("%s/config.js", config.ContextPath), &nginx_controller.ConfigController{})
  70. // portal static assets
  71. beego.SetStaticPath(config.ContextPath, "static/web")
  72. webPrefix := config.ContextPath
  73. if !strings.HasSuffix(webPrefix, "/") {
  74. webPrefix = webPrefix + "/"
  75. }
  76. beego.Get("/", func(ctx *context.Context) {
  77. ctx.Redirect(301, webPrefix)
  78. })
  79. beego.Get(config.ContextPath+"/*", func(ctx *context.Context) {
  80. ctx.Redirect(301, webPrefix)
  81. })
  82. beego.ErrorHandler("404", func(writer http.ResponseWriter, request *http.Request) {
  83. accept := request.Header.Get("accept")
  84. logs.Warn("404", accept)
  85. if strings.Contains(accept, "json") {
  86. writer.Header().Set("content-type", "application/json")
  87. writer.WriteHeader(200)
  88. resp := models.RespData{
  89. Code: -2,
  90. Msg: "server error",
  91. }
  92. str, _ := json.Marshal(&resp)
  93. writer.Write(str)
  94. } else {
  95. writer.WriteHeader(404)
  96. writer.Write([]byte(""))
  97. }
  98. })
  99. }