package routers import ( "encoding/json" "fmt" "github.com/astaxie/beego" "github.com/astaxie/beego/context" "github.com/astaxie/beego/logs" "net/http" config2 "nginx-ui/server/config" "nginx-ui/server/middleware" "nginx-ui/server/models" "nginx-ui/server/modules/ldap" "nginx-ui/server/modules/nginx/nginx_controller" "nginx-ui/server/modules/oauth2" "nginx-ui/server/modules/user" "strings" ) var NginxR = "/nginx" var NginxGetR = "/nginx/:id" var NginxRefreshR = "/nginx/:id/http/refresh" var NginxStartR = "/nginx/:id/start" var NginxStopR = "/nginx/:id/stop" var NginxStatusR = "/nginx/:id/status" func init() { config := config2.Config userController := user.NewUserController() logs.Info("baseApi", config.BaseApi) ns := beego.NewNamespace(config.BaseApi, beego.NSRouter(NginxR, &nginx_controller.NginxController{}), beego.NSRouter(NginxGetR, &nginx_controller.NginxController{}, "post:Update"), beego.NSRouter(NginxGetR, &nginx_controller.NginxController{}, "get:GetNginx"), beego.NSRouter(NginxGetR, &nginx_controller.NginxController{}, "delete:DelNginx"), beego.NSRouter(NginxRefreshR, &nginx_controller.NginxController{}, "post:RefreshHttp"), beego.NSRouter(NginxStartR, &nginx_controller.NginxController{}, "post:StartNginx"), beego.NSRouter(NginxStopR, &nginx_controller.NginxController{}, "post:StopNginx"), beego.NSRouter(NginxStatusR, &nginx_controller.NginxController{}, "post:StatusNginx"), // certs beego.NSRouter("/nginx/:id/certs", &nginx_controller.CertController{}), beego.NSRouter("/nginx/:id/certs/sync", &nginx_controller.CertController{}, "post:Sync"), // nginx server apis beego.NSRouter("/nginx/:id/server", &nginx_controller.ServerController{}), beego.NSRouter("/nginx/:id/server/refresh", &nginx_controller.ServerController{}, "post:Refresh"), // file upload download beego.NSRouter("/nginx/:id/file/deploy", &nginx_controller.FileController{}, "post:Deploy"), beego.NSRouter("/file", &nginx_controller.FileController{}), beego.NSRouter("/logger", &nginx_controller.LoggerController{}), beego.NSRouter("/user/login", userController, "post:Login"), beego.NSRouter("/user/logout", userController, "post:Logout"), beego.NSRouter("/user/info", userController, "get:User"), beego.NSRouter("/user/info", userController, "post:UpdateUserInfo"), beego.NSRouter("/user/register", userController, "post:Register"), beego.NSRouter("/user/list", userController, "post:Users"), beego.NSRouter("/user/detail", userController, "get:GetDetail"), beego.NSRouter("/user/save", userController, "post:Update"), beego.NSRouter("/user/modifyPassword", userController, "post:UpdatePassword"), beego.NSRouter("/oauth2", &oauth2.Controller{}), beego.NSRouter("/oauth2/callback", &oauth2.Controller{}, "post:Callback"), ) beego.AddNamespace(ns) // LDAP路由 beego.AddNamespace(ldap.InitRouter(config.BaseApi)) beego.InsertFilter(fmt.Sprintf("%s/**", config.BaseApi), beego.BeforeRouter, middleware.AuthFilter) beego.Router(fmt.Sprintf("%s/config.js", config.ContextPath), &nginx_controller.ConfigController{}) // portal static assets beego.SetStaticPath(config.ContextPath, "static/web") beego.Get("/", func(ctx *context.Context) { ctx.Redirect(301, fmt.Sprintf("%s/index.html", config.ContextPath)) }) beego.ErrorHandler("404", func(writer http.ResponseWriter, request *http.Request) { accept := request.Header.Get("accept") logs.Warn("404", accept) if strings.Contains(accept, "json") { writer.Header().Set("content-type", "application/json") writer.WriteHeader(200) resp := models.RespData{ Code: -2, Msg: "server error", } str, _ := json.Marshal(&resp) writer.Write(str) } else { writer.WriteHeader(404) writer.Write([]byte("")) } }) }