-
Notifications
You must be signed in to change notification settings - Fork 3
/
httpResponse.go
66 lines (59 loc) · 1.9 KB
/
httpResponse.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Package helpers are code saving utility and helper functions
package helpers
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
)
// ParsePathParams parse request path param according to path template and extract its values.
func ParsePathParams(template, path string) (map[string]string, error) {
var pth string
if strings.Contains(path, "?") {
pth = path[:strings.Index(path, "?")]
} else {
pth = path
}
templatePaths := strings.Split(template, "/")
pathPaths := strings.Split(pth, "/")
if len(templatePaths) != len(pathPaths) {
return nil, fmt.Errorf("pathElement length not equals to templateElement length")
}
ret := make(map[string]string)
for idx, templateElement := range templatePaths {
pathElement := pathPaths[idx]
if len(templateElement) > 0 && len(pathElement) > 0 {
if templateElement[:1] == "{" && templateElement[len(templateElement)-1:] == "}" {
tKey := templateElement[1 : len(templateElement)-1]
ret[tKey] = pathElement
} else if templateElement != pathElement {
return nil, fmt.Errorf("template %s not compatible with path %s", template, path)
}
}
}
return ret, nil
}
// ResponseJSON define the structure of all response
type ResponseJSON struct {
Message string `json:"message"`
Status string `json:"status"`
Data interface{} `json:"data,omitempty"`
ErrorCode int `json:"error_code,omitempty"`
}
// HTTPResponseBuilder builds the response headers and payloads
func HTTPResponseBuilder(ctx context.Context, w http.ResponseWriter, r *http.Request, httpStatus int, message string, data interface{}, errorCode int) {
resp := ResponseJSON{
Data: data,
Message: message,
}
if httpStatus >= 200 && httpStatus < 300 {
resp.Status = "SUCCESS"
} else {
resp.Status = "FAIL"
resp.ErrorCode = errorCode
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(httpStatus)
json.NewEncoder(w).Encode(resp)
}