From 5e4b35e03ab0c658225c135a8510d0f7dd6437ef Mon Sep 17 00:00:00 2001 From: Dreamacro <305009791@qq.com> Date: Mon, 10 Dec 2018 11:33:37 +0800 Subject: [PATCH] Chore: standardize API returns --- hub/route/configs.go | 22 +++++++++++----------- hub/route/proxies.go | 36 ++++++++++++++++++------------------ hub/route/rules.go | 3 +-- hub/route/server.go | 10 +++++----- 4 files changed, 35 insertions(+), 36 deletions(-) diff --git a/hub/route/configs.go b/hub/route/configs.go index bf4f89ef..26d9ca4f 100644 --- a/hub/route/configs.go +++ b/hub/route/configs.go @@ -32,7 +32,7 @@ type configSchema struct { func getConfigs(w http.ResponseWriter, r *http.Request) { general := executor.GetGeneral() - render.Respond(w, r, general) + render.JSON(w, r, general) } func pointerOrDefault(p *int, def int) int { @@ -46,8 +46,8 @@ func pointerOrDefault(p *int, def int) int { func patchConfigs(w http.ResponseWriter, r *http.Request) { general := &configSchema{} if err := render.DecodeJSON(r.Body, general); err != nil { - w.WriteHeader(http.StatusBadRequest) - render.Respond(w, r, ErrBadRequest) + render.Status(r, http.StatusBadRequest) + render.JSON(w, r, ErrBadRequest) return } @@ -68,7 +68,7 @@ func patchConfigs(w http.ResponseWriter, r *http.Request) { log.SetLevel(*general.LogLevel) } - w.WriteHeader(http.StatusNoContent) + render.NoContent(w, r) } type updateConfigRequest struct { @@ -78,25 +78,25 @@ type updateConfigRequest struct { func updateConfigs(w http.ResponseWriter, r *http.Request) { req := updateConfigRequest{} if err := render.DecodeJSON(r.Body, &req); err != nil { - w.WriteHeader(http.StatusBadRequest) - render.Respond(w, r, ErrBadRequest) + render.Status(r, http.StatusBadRequest) + render.JSON(w, r, ErrBadRequest) return } if !filepath.IsAbs(req.Path) { - w.WriteHeader(http.StatusBadRequest) - render.Respond(w, r, newError("path is not a absoluted path")) + render.Status(r, http.StatusBadRequest) + render.JSON(w, r, newError("path is not a absoluted path")) return } force := r.URL.Query().Get("force") == "true" cfg, err := executor.ParseWithPath(req.Path) if err != nil { - w.WriteHeader(http.StatusBadRequest) - render.Respond(w, r, newError(err.Error())) + render.Status(r, http.StatusBadRequest) + render.JSON(w, r, newError(err.Error())) return } executor.ApplyConfig(cfg, force) - w.WriteHeader(http.StatusNoContent) + render.NoContent(w, r) } diff --git a/hub/route/proxies.go b/hub/route/proxies.go index 65beac1e..404d1a32 100644 --- a/hub/route/proxies.go +++ b/hub/route/proxies.go @@ -47,8 +47,8 @@ func findProxyByName(next http.Handler) http.Handler { proxies := T.Instance().Proxies() proxy, exist := proxies[name] if !exist { - w.WriteHeader(http.StatusNotFound) - render.Respond(w, r, ErrNotFound) + render.Status(r, http.StatusNotFound) + render.JSON(w, r, ErrNotFound) return } @@ -59,14 +59,14 @@ func findProxyByName(next http.Handler) http.Handler { func getProxies(w http.ResponseWriter, r *http.Request) { proxies := T.Instance().Proxies() - render.Respond(w, r, map[string]map[string]C.Proxy{ + render.JSON(w, r, map[string]map[string]C.Proxy{ "proxies": proxies, }) } func getProxy(w http.ResponseWriter, r *http.Request) { proxy := r.Context().Value(CtxKeyProxy).(C.Proxy) - render.Respond(w, r, proxy) + render.JSON(w, r, proxy) } type UpdateProxyRequest struct { @@ -76,8 +76,8 @@ type UpdateProxyRequest struct { func updateProxy(w http.ResponseWriter, r *http.Request) { req := UpdateProxyRequest{} if err := render.DecodeJSON(r.Body, &req); err != nil { - w.WriteHeader(http.StatusBadRequest) - render.Respond(w, r, ErrBadRequest) + render.Status(r, http.StatusBadRequest) + render.JSON(w, r, ErrBadRequest) return } @@ -85,18 +85,18 @@ func updateProxy(w http.ResponseWriter, r *http.Request) { selector, ok := proxy.(*A.Selector) if !ok { - w.WriteHeader(http.StatusBadRequest) - render.Respond(w, r, ErrBadRequest) + render.Status(r, http.StatusBadRequest) + render.JSON(w, r, ErrBadRequest) return } if err := selector.Set(req.Name); err != nil { - w.WriteHeader(http.StatusBadRequest) - render.Respond(w, r, newError(fmt.Sprintf("Selector update error: %s", err.Error()))) + render.Status(r, http.StatusBadRequest) + render.JSON(w, r, newError(fmt.Sprintf("Selector update error: %s", err.Error()))) return } - w.WriteHeader(http.StatusNoContent) + render.NoContent(w, r) } func getProxyDelay(w http.ResponseWriter, r *http.Request) { @@ -104,8 +104,8 @@ func getProxyDelay(w http.ResponseWriter, r *http.Request) { url := query.Get("url") timeout, err := strconv.ParseInt(query.Get("timeout"), 10, 16) if err != nil { - w.WriteHeader(http.StatusBadRequest) - render.Respond(w, r, ErrBadRequest) + render.Status(r, http.StatusBadRequest) + render.JSON(w, r, ErrBadRequest) return } @@ -122,14 +122,14 @@ func getProxyDelay(w http.ResponseWriter, r *http.Request) { select { case <-time.After(time.Millisecond * time.Duration(timeout)): - w.WriteHeader(http.StatusRequestTimeout) - render.Respond(w, r, ErrRequestTimeout) + render.Status(r, http.StatusRequestTimeout) + render.JSON(w, r, ErrRequestTimeout) case t := <-sigCh: if t == 0 { - w.WriteHeader(http.StatusServiceUnavailable) - render.Respond(w, r, newError("An error occurred in the delay test")) + render.Status(r, http.StatusServiceUnavailable) + render.JSON(w, r, newError("An error occurred in the delay test")) } else { - render.Respond(w, r, map[string]int16{ + render.JSON(w, r, map[string]int16{ "delay": t, }) } diff --git a/hub/route/rules.go b/hub/route/rules.go index 02e19b9b..b8cd3aeb 100644 --- a/hub/route/rules.go +++ b/hub/route/rules.go @@ -33,8 +33,7 @@ func getRules(w http.ResponseWriter, r *http.Request) { }) } - w.WriteHeader(http.StatusOK) - render.Respond(w, r, map[string][]Rule{ + render.JSON(w, r, map[string][]Rule{ "rules": rules, }) } diff --git a/hub/route/server.go b/hub/route/server.go index 9ae1a347..a3e7a791 100644 --- a/hub/route/server.go +++ b/hub/route/server.go @@ -77,8 +77,8 @@ func authentication(next http.Handler) http.Handler { hasUnvalidHeader := text[0] != "Bearer" hasUnvalidSecret := len(text) == 2 && text[1] != serverSecret if hasUnvalidHeader || hasUnvalidSecret { - w.WriteHeader(http.StatusUnauthorized) - render.Respond(w, r, ErrUnauthorized) + render.Status(r, http.StatusUnauthorized) + render.JSON(w, r, ErrUnauthorized) return } next.ServeHTTP(w, r) @@ -87,7 +87,7 @@ func authentication(next http.Handler) http.Handler { } func traffic(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) + render.Status(r, http.StatusOK) tick := time.NewTicker(time.Second) t := T.Instance().Traffic() @@ -116,8 +116,8 @@ func getLogs(w http.ResponseWriter, r *http.Request) { level, ok := log.LogLevelMapping[levelText] if !ok { - w.WriteHeader(http.StatusBadRequest) - render.Respond(w, r, ErrBadRequest) + render.Status(r, http.StatusBadRequest) + render.JSON(w, r, ErrBadRequest) return }