-
Notifications
You must be signed in to change notification settings - Fork 3
/
response.go
39 lines (26 loc) · 827 Bytes
/
response.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 main
import (
"encoding/json"
"net/http"
"strconv"
"time"
"github.com/romana/rlog"
)
func respondWith(w http.ResponseWriter, r *http.Request, err error, message string, response interface{}, code int, success bool) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
var resp RESPONSE
if err != nil {
rlog.Debug("respondWith() with code " + strconv.Itoa(code) + " for: " + r.Method + " : " + r.URL.Path + " has Error: " + err.Error())
resp.Message = err.Error()
} else {
rlog.Debug("respondWith() with code " + strconv.Itoa(code) + " for: " + r.Method + " : " + r.URL.Path)
resp.Message = message
}
resp.Code = code
resp.Success = success
resp.Response = response
resp.Time = time.Now().UnixNano()
respons, err := json.Marshal(resp)
w.Write([]byte(respons))
}