fixed healthcheck failed to disable and nil dereference

This commit is contained in:
yusing
2024-10-19 00:13:55 +08:00
parent 53557e38b6
commit b296fb2965
11 changed files with 60 additions and 20 deletions

View File

@@ -31,6 +31,7 @@ func NewHandler() http.Handler {
mux.HandleFunc("POST", "/v1/reload", v1.Reload)
mux.HandleFunc("GET", "/v1/list", v1.List)
mux.HandleFunc("GET", "/v1/list/{what}", v1.List)
mux.HandleFunc("GET", "/v1/list/{what}/{which}", v1.List)
mux.HandleFunc("GET", "/v1/file", v1.GetFileContent)
mux.HandleFunc("GET", "/v1/file/{filename...}", v1.GetFileContent)
mux.HandleFunc("POST", "/v1/file/{filename...}", v1.SetFileContent)

View File

@@ -14,6 +14,7 @@ import (
)
const (
ListRoute = "route"
ListRoutes = "routes"
ListConfigFiles = "config_files"
ListMiddlewares = "middlewares"
@@ -28,8 +29,16 @@ func List(w http.ResponseWriter, r *http.Request) {
if what == "" {
what = ListRoutes
}
which := r.PathValue("which")
switch what {
case ListRoute:
if route := listRoute(which); route == nil {
http.Error(w, "not found", http.StatusNotFound)
return
} else {
U.RespondJSON(w, r, route)
}
case ListRoutes:
U.RespondJSON(w, r, config.RoutesByAlias(route.RouteType(r.FormValue("type"))))
case ListConfigFiles:
@@ -49,6 +58,21 @@ func List(w http.ResponseWriter, r *http.Request) {
}
}
func listRoute(which string) any {
if which == "" {
which = "all"
}
if which == "all" {
return config.RoutesByAlias()
}
routes := config.RoutesByAlias()
route, ok := routes[which]
if !ok {
return nil
}
return route
}
func listConfigFiles(w http.ResponseWriter, r *http.Request) {
files, err := utils.ListFiles(common.ConfigBasePath, 1)
if err != nil {