router.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/controllers"
  11. "nginx-ui/server/middleware"
  12. "nginx-ui/server/models"
  13. "strings"
  14. )
  15. var NginxR = "/nginx"
  16. var NginxGetR = "/nginx/:id"
  17. var NginxRefreshR = "/nginx/:id/http/refresh"
  18. var NginxStartR = "/nginx/:id/start"
  19. var NginxStopR = "/nginx/:id/stop"
  20. var NginxStatusR = "/nginx/:id/status"
  21. func init() {
  22. config := config2.Config
  23. userController := controllers.NewUserController()
  24. ns := beego.NewNamespace(config.BaseApi,
  25. beego.NSRouter(NginxR, &controllers.NginxController{}),
  26. beego.NSRouter(NginxGetR, &controllers.NginxController{}, "post:Update"),
  27. beego.NSRouter(NginxGetR, &controllers.NginxController{}, "get:GetNginx"),
  28. beego.NSRouter(NginxGetR, &controllers.NginxController{}, "delete:DelNginx"),
  29. beego.NSRouter(NginxRefreshR, &controllers.NginxController{}, "post:RefreshHttp"),
  30. beego.NSRouter(NginxStartR, &controllers.NginxController{}, "post:StartNginx"),
  31. beego.NSRouter(NginxStopR, &controllers.NginxController{}, "post:StopNginx"),
  32. beego.NSRouter(NginxStatusR, &controllers.NginxController{}, "post:StatusNginx"),
  33. // certs
  34. beego.NSRouter("/nginx/:id/certs", &controllers.CertController{}),
  35. beego.NSRouter("/nginx/:id/certs/sync", &controllers.CertController{}, "post:Sync"),
  36. // nginx server apis
  37. beego.NSRouter("/nginx/:id/server", &controllers.ServerController{}),
  38. beego.NSRouter("/nginx/:id/server/refresh", &controllers.ServerController{}, "post:Refresh"),
  39. // file upload download
  40. beego.NSRouter("/nginx/:id/file/deploy", &controllers.FileController{}, "post:Deploy"),
  41. beego.NSRouter("/file", &controllers.FileController{}),
  42. beego.NSRouter("/logger", &controllers.LoggerController{}),
  43. beego.NSRouter("/user/login", userController, "post:Login"),
  44. beego.NSRouter("/user/info", userController, "get:User"),
  45. beego.NSRouter("/user/register", userController, "post:Register"),
  46. beego.NSRouter("/oauth2", &controllers.Oauth2Controller{}),
  47. beego.NSRouter("/oauth2/callback", &controllers.Oauth2Controller{}, "post:Callback"),
  48. )
  49. beego.AddNamespace(ns)
  50. beego.InsertFilter(fmt.Sprintf("%s/**", config.BaseApi), beego.BeforeRouter, middleware.AuthFilter)
  51. beego.Router(fmt.Sprintf("%s/config.js", config.ContextPath), &controllers.ConfigController{})
  52. // portal static assets
  53. beego.SetStaticPath(config.ContextPath, "static/web")
  54. beego.Get("/", func(ctx *context.Context) {
  55. ctx.Redirect(301, fmt.Sprintf("%s/index.html", config.ContextPath))
  56. })
  57. beego.ErrorHandler("404", func(writer http.ResponseWriter, request *http.Request) {
  58. accept := request.Header.Get("accept")
  59. logs.Warn("404", accept)
  60. if strings.Contains(accept, "json") {
  61. writer.Header().Set("content-type", "application/json")
  62. writer.WriteHeader(200)
  63. resp := models.RespData{
  64. Code: -2,
  65. Msg: "server error",
  66. }
  67. str, _ := json.Marshal(&resp)
  68. writer.Write(str)
  69. } else {
  70. writer.WriteHeader(404)
  71. writer.Write([]byte(""))
  72. }
  73. })
  74. }