|
|
@ -11,10 +11,29 @@ import ( |
|
|
|
"gopkg.in/yaml.v2" |
|
|
|
) |
|
|
|
|
|
|
|
func stripCode(c int) int { |
|
|
|
if c >= 100 && c < 1000 { |
|
|
|
return c |
|
|
|
} else if c > 1000 { |
|
|
|
return stripCode(c / 10) |
|
|
|
} else { |
|
|
|
return stripCode(c * 10) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func FailureResponse(w http.ResponseWriter, code int, message string) error { |
|
|
|
w.Header().Set("Content-Type", "text/plain") |
|
|
|
w.WriteHeader(code) |
|
|
|
_, _ = w.Write([]byte(message)) |
|
|
|
if bs, err := json.Marshal(map[string]interface{}{ |
|
|
|
"code": code, |
|
|
|
"message": message, |
|
|
|
}); nil != err { |
|
|
|
w.Header().Set("Content-Type", "text/plain") |
|
|
|
w.WriteHeader(stripCode(code)) |
|
|
|
_, _ = w.Write([]byte(message)) |
|
|
|
} else { |
|
|
|
w.Header().Set("Content-Type", "application/json") |
|
|
|
w.WriteHeader(stripCode(code)) |
|
|
|
_, _ = w.Write(bs) |
|
|
|
} |
|
|
|
// Make sure all contents flushed
|
|
|
|
if flusher, ok := w.(http.Flusher); ok { |
|
|
|
flusher.Flush() |
|
|
@ -29,7 +48,7 @@ func StandardResponse(data []byte, code int, contentType string, w http.Response |
|
|
|
http.SetCookie(w, x) |
|
|
|
} |
|
|
|
w.Header().Set("Content-Type", contentType) |
|
|
|
w.WriteHeader(code) |
|
|
|
w.WriteHeader(stripCode(code)) |
|
|
|
if len(data) > 0 { |
|
|
|
if n, e := w.Write(data); nil != e { |
|
|
|
log.Errorln("StandardResponse:> Write response error:", e, n) |
|
|
|