-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
39 lines (32 loc) · 844 Bytes
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package web
import (
"fmt"
"net/http"
"github.com/sjsafranek/logger"
)
func apiFormatError(err error) string {
return fmt.Sprintf(`{"status":"error","error":"%v"}`, err.Error())
}
func apiErrorResponse(w http.ResponseWriter, statusCode int, err error) {
if 500 > statusCode {
logger.Warn(err)
} else {
logger.Error(err)
}
apiJSONResponse(w, []byte(apiFormatError(err)), statusCode)
}
func apiBasicResponse(w http.ResponseWriter, statusCode int, err error) {
if nil != err {
apiErrorResponse(w, statusCode, err)
return
}
apiOKResponse(w)
}
func apiOKResponse(w http.ResponseWriter) {
apiJSONResponse(w, []byte(`{"status":"ok"}`), http.StatusOK)
}
func apiJSONResponse(w http.ResponseWriter, data []byte, statusCode int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
w.Write(data)
}