router.go 2.8 KB

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