router.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/user"
  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("/oauth2", &oauth2.Controller{}),
  57. beego.NSRouter("/oauth2/callback", &oauth2.Controller{}, "post:Callback"),
  58. )
  59. beego.AddNamespace(ns)
  60. // LDAP路由
  61. beego.AddNamespace(ldap.InitRouter(config.BaseApi))
  62. beego.InsertFilter(fmt.Sprintf("%s/**", config.BaseApi), beego.BeforeRouter, middleware.AuthFilter)
  63. beego.Router(fmt.Sprintf("%s/config.js", config.ContextPath), &nginx_controller.ConfigController{})
  64. // portal static assets
  65. beego.SetStaticPath(config.ContextPath, "static/web")
  66. beego.Get("/", func(ctx *context.Context) {
  67. ctx.Redirect(301, fmt.Sprintf("%s/index.html", config.ContextPath))
  68. })
  69. beego.ErrorHandler("404", func(writer http.ResponseWriter, request *http.Request) {
  70. accept := request.Header.Get("accept")
  71. logs.Warn("404", accept)
  72. if strings.Contains(accept, "json") {
  73. writer.Header().Set("content-type", "application/json")
  74. writer.WriteHeader(200)
  75. resp := models.RespData{
  76. Code: -2,
  77. Msg: "server error",
  78. }
  79. str, _ := json.Marshal(&resp)
  80. writer.Write(str)
  81. } else {
  82. writer.WriteHeader(404)
  83. writer.Write([]byte(""))
  84. }
  85. })
  86. }