fix golint issues in rest (#529)

This commit is contained in:
Kevin Wan
2021-03-01 19:15:35 +08:00
committed by GitHub
parent d894b88c3e
commit 655ae8034c
29 changed files with 132 additions and 32 deletions

View File

@@ -13,6 +13,7 @@ var (
lock sync.RWMutex
)
// Error writes err into w.
func Error(w http.ResponseWriter, err error) {
lock.RLock()
handler := errorHandler
@@ -32,20 +33,24 @@ func Error(w http.ResponseWriter, err error) {
}
}
// Ok writes HTTP 200 OK into w.
func Ok(w http.ResponseWriter) {
w.WriteHeader(http.StatusOK)
}
// OkJson writes v into w with 200 OK.
func OkJson(w http.ResponseWriter, v interface{}) {
WriteJson(w, http.StatusOK, v)
}
// SetErrorHandler sets the error handler, which is called on calling Error.
func SetErrorHandler(handler func(error) (int, interface{})) {
lock.Lock()
defer lock.Unlock()
errorHandler = handler
}
// WriteJson writes v as json string into w with code.
func WriteJson(w http.ResponseWriter, code int, v interface{}) {
w.Header().Set(ContentType, ApplicationJson)
w.WriteHeader(code)