Chore: standardize API returns
This commit is contained in:
parent
4e33035730
commit
8019d74125
4 changed files with 35 additions and 36 deletions
|
@ -32,7 +32,7 @@ type configSchema struct {
|
||||||
|
|
||||||
func getConfigs(w http.ResponseWriter, r *http.Request) {
|
func getConfigs(w http.ResponseWriter, r *http.Request) {
|
||||||
general := executor.GetGeneral()
|
general := executor.GetGeneral()
|
||||||
render.Respond(w, r, general)
|
render.JSON(w, r, general)
|
||||||
}
|
}
|
||||||
|
|
||||||
func pointerOrDefault(p *int, def int) int {
|
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) {
|
func patchConfigs(w http.ResponseWriter, r *http.Request) {
|
||||||
general := &configSchema{}
|
general := &configSchema{}
|
||||||
if err := render.DecodeJSON(r.Body, general); err != nil {
|
if err := render.DecodeJSON(r.Body, general); err != nil {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
render.Status(r, http.StatusBadRequest)
|
||||||
render.Respond(w, r, ErrBadRequest)
|
render.JSON(w, r, ErrBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,7 +68,7 @@ func patchConfigs(w http.ResponseWriter, r *http.Request) {
|
||||||
log.SetLevel(*general.LogLevel)
|
log.SetLevel(*general.LogLevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusNoContent)
|
render.NoContent(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
type updateConfigRequest struct {
|
type updateConfigRequest struct {
|
||||||
|
@ -78,25 +78,25 @@ type updateConfigRequest struct {
|
||||||
func updateConfigs(w http.ResponseWriter, r *http.Request) {
|
func updateConfigs(w http.ResponseWriter, r *http.Request) {
|
||||||
req := updateConfigRequest{}
|
req := updateConfigRequest{}
|
||||||
if err := render.DecodeJSON(r.Body, &req); err != nil {
|
if err := render.DecodeJSON(r.Body, &req); err != nil {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
render.Status(r, http.StatusBadRequest)
|
||||||
render.Respond(w, r, ErrBadRequest)
|
render.JSON(w, r, ErrBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !filepath.IsAbs(req.Path) {
|
if !filepath.IsAbs(req.Path) {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
render.Status(r, http.StatusBadRequest)
|
||||||
render.Respond(w, r, newError("path is not a absoluted path"))
|
render.JSON(w, r, newError("path is not a absoluted path"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
force := r.URL.Query().Get("force") == "true"
|
force := r.URL.Query().Get("force") == "true"
|
||||||
cfg, err := executor.ParseWithPath(req.Path)
|
cfg, err := executor.ParseWithPath(req.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
render.Status(r, http.StatusBadRequest)
|
||||||
render.Respond(w, r, newError(err.Error()))
|
render.JSON(w, r, newError(err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
executor.ApplyConfig(cfg, force)
|
executor.ApplyConfig(cfg, force)
|
||||||
w.WriteHeader(http.StatusNoContent)
|
render.NoContent(w, r)
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,8 +47,8 @@ func findProxyByName(next http.Handler) http.Handler {
|
||||||
proxies := T.Instance().Proxies()
|
proxies := T.Instance().Proxies()
|
||||||
proxy, exist := proxies[name]
|
proxy, exist := proxies[name]
|
||||||
if !exist {
|
if !exist {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
render.Status(r, http.StatusNotFound)
|
||||||
render.Respond(w, r, ErrNotFound)
|
render.JSON(w, r, ErrNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,14 +59,14 @@ func findProxyByName(next http.Handler) http.Handler {
|
||||||
|
|
||||||
func getProxies(w http.ResponseWriter, r *http.Request) {
|
func getProxies(w http.ResponseWriter, r *http.Request) {
|
||||||
proxies := T.Instance().Proxies()
|
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,
|
"proxies": proxies,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func getProxy(w http.ResponseWriter, r *http.Request) {
|
func getProxy(w http.ResponseWriter, r *http.Request) {
|
||||||
proxy := r.Context().Value(CtxKeyProxy).(C.Proxy)
|
proxy := r.Context().Value(CtxKeyProxy).(C.Proxy)
|
||||||
render.Respond(w, r, proxy)
|
render.JSON(w, r, proxy)
|
||||||
}
|
}
|
||||||
|
|
||||||
type UpdateProxyRequest struct {
|
type UpdateProxyRequest struct {
|
||||||
|
@ -76,8 +76,8 @@ type UpdateProxyRequest struct {
|
||||||
func updateProxy(w http.ResponseWriter, r *http.Request) {
|
func updateProxy(w http.ResponseWriter, r *http.Request) {
|
||||||
req := UpdateProxyRequest{}
|
req := UpdateProxyRequest{}
|
||||||
if err := render.DecodeJSON(r.Body, &req); err != nil {
|
if err := render.DecodeJSON(r.Body, &req); err != nil {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
render.Status(r, http.StatusBadRequest)
|
||||||
render.Respond(w, r, ErrBadRequest)
|
render.JSON(w, r, ErrBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,18 +85,18 @@ func updateProxy(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
selector, ok := proxy.(*A.Selector)
|
selector, ok := proxy.(*A.Selector)
|
||||||
if !ok {
|
if !ok {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
render.Status(r, http.StatusBadRequest)
|
||||||
render.Respond(w, r, ErrBadRequest)
|
render.JSON(w, r, ErrBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := selector.Set(req.Name); err != nil {
|
if err := selector.Set(req.Name); err != nil {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
render.Status(r, http.StatusBadRequest)
|
||||||
render.Respond(w, r, newError(fmt.Sprintf("Selector update error: %s", err.Error())))
|
render.JSON(w, r, newError(fmt.Sprintf("Selector update error: %s", err.Error())))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusNoContent)
|
render.NoContent(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getProxyDelay(w http.ResponseWriter, r *http.Request) {
|
func getProxyDelay(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -104,8 +104,8 @@ func getProxyDelay(w http.ResponseWriter, r *http.Request) {
|
||||||
url := query.Get("url")
|
url := query.Get("url")
|
||||||
timeout, err := strconv.ParseInt(query.Get("timeout"), 10, 16)
|
timeout, err := strconv.ParseInt(query.Get("timeout"), 10, 16)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
render.Status(r, http.StatusBadRequest)
|
||||||
render.Respond(w, r, ErrBadRequest)
|
render.JSON(w, r, ErrBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,14 +122,14 @@ func getProxyDelay(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-time.After(time.Millisecond * time.Duration(timeout)):
|
case <-time.After(time.Millisecond * time.Duration(timeout)):
|
||||||
w.WriteHeader(http.StatusRequestTimeout)
|
render.Status(r, http.StatusRequestTimeout)
|
||||||
render.Respond(w, r, ErrRequestTimeout)
|
render.JSON(w, r, ErrRequestTimeout)
|
||||||
case t := <-sigCh:
|
case t := <-sigCh:
|
||||||
if t == 0 {
|
if t == 0 {
|
||||||
w.WriteHeader(http.StatusServiceUnavailable)
|
render.Status(r, http.StatusServiceUnavailable)
|
||||||
render.Respond(w, r, newError("An error occurred in the delay test"))
|
render.JSON(w, r, newError("An error occurred in the delay test"))
|
||||||
} else {
|
} else {
|
||||||
render.Respond(w, r, map[string]int16{
|
render.JSON(w, r, map[string]int16{
|
||||||
"delay": t,
|
"delay": t,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,8 +33,7 @@ func getRules(w http.ResponseWriter, r *http.Request) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
render.JSON(w, r, map[string][]Rule{
|
||||||
render.Respond(w, r, map[string][]Rule{
|
|
||||||
"rules": rules,
|
"rules": rules,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,8 +77,8 @@ func authentication(next http.Handler) http.Handler {
|
||||||
hasUnvalidHeader := text[0] != "Bearer"
|
hasUnvalidHeader := text[0] != "Bearer"
|
||||||
hasUnvalidSecret := len(text) == 2 && text[1] != serverSecret
|
hasUnvalidSecret := len(text) == 2 && text[1] != serverSecret
|
||||||
if hasUnvalidHeader || hasUnvalidSecret {
|
if hasUnvalidHeader || hasUnvalidSecret {
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
render.Status(r, http.StatusUnauthorized)
|
||||||
render.Respond(w, r, ErrUnauthorized)
|
render.JSON(w, r, ErrUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
|
@ -87,7 +87,7 @@ func authentication(next http.Handler) http.Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
func traffic(w http.ResponseWriter, r *http.Request) {
|
func traffic(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
render.Status(r, http.StatusOK)
|
||||||
|
|
||||||
tick := time.NewTicker(time.Second)
|
tick := time.NewTicker(time.Second)
|
||||||
t := T.Instance().Traffic()
|
t := T.Instance().Traffic()
|
||||||
|
@ -116,8 +116,8 @@ func getLogs(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
level, ok := log.LogLevelMapping[levelText]
|
level, ok := log.LogLevelMapping[levelText]
|
||||||
if !ok {
|
if !ok {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
render.Status(r, http.StatusBadRequest)
|
||||||
render.Respond(w, r, ErrBadRequest)
|
render.JSON(w, r, ErrBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue