-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
145 lines (127 loc) · 3.81 KB
/
common.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package handler
import (
"encoding/json"
"net/http"
"time"
log "github.com/sirupsen/logrus"
)
type responseAPI struct {
Status int `json:"status"`
ProcessTime int `json:"process_time"`
Data interface{} `json:"data"`
}
type simpleMessage struct {
Message string `json:"message"`
}
func respErrorJSON(w http.ResponseWriter, r *http.Request, status int, errStr string) {
processTimeRaw := r.Context().Value(ctxKey("processTime"))
if processTimeRaw == nil {
log.WithFields(log.Fields{
"status": status,
"errorMsg": errStr,
}).Errorln("[respErrorJSON] processTime:",
"don't forget to add the middleware that add 'processTime' context")
respErrorText(w, r)
return
}
processTime, ok := processTimeRaw.(middlewareProcessTime)
if !ok {
log.WithFields(log.Fields{
"status": status,
"errorMsg": errStr,
}).Errorln("[respErrorJSON] processTime:",
"don't forget to add the middleware that add 'processTime' context")
respErrorText(w, r)
return
}
js, err := json.Marshal(responseAPI{
Status: status,
ProcessTime: int(time.Now().Sub(processTime.ProcessTime) / time.Microsecond),
Data: struct {
Message string `json:"message"`
}{errStr},
})
if err != nil {
log.WithFields(log.Fields{
"status": status,
"errorMsg": errStr,
}).Errorln("[respErrorJSON] marshal:", err.Error())
respErrorText(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
w.Write(js)
return
}
func respSuccessJSON(w http.ResponseWriter, r *http.Request, status int, data interface{}) {
processTimeRaw := r.Context().Value(ctxKey("processTime"))
if processTimeRaw == nil {
log.WithFields(log.Fields{
"status": status,
"data": data,
}).Errorln("[respSuccessJSON] processTime:",
"don't forget to add the middleware that add 'processTime' context")
respErrorText(w, r)
return
}
processTime, ok := processTimeRaw.(middlewareProcessTime)
if !ok {
log.WithFields(log.Fields{
"status": status,
"data": data,
}).Errorln("[respSuccessJSON] processTime:",
"don't forget to add the middleware that add 'processTime' context")
respErrorText(w, r)
return
}
if data == nil {
log.WithFields(log.Fields{
"status": status,
"data": data,
}).Errorln("[respSuccessJSON] data should not be nil")
respErrorText(w, r)
return
}
js, err := json.Marshal(responseAPI{
Status: status,
ProcessTime: int(time.Now().Sub(processTime.ProcessTime) / time.Microsecond),
Data: data,
})
if err != nil {
log.WithFields(log.Fields{
"status": status,
"data": data,
}).Errorln("[respSuccessJSON] marshal:", err.Error())
respErrorText(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
w.Write(js)
return
}
func respErrorText(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500 - Something bad happened!"))
}
type ctxKey string
const errorWrongJSONFormat = "Wrong JSON Format"
// const errorWrongJWTSigningMethod = "Wrong JWT Signing Method"
const errorCredentialProblem = "Credential Problem"
const errorExpiredJWTToken = "Expired JWT Token"
const errorMissingJWTData = "Missing JWT Data"
const errorDeformedJWTToken = "Deformed JWT Token"
const errorMissingAuthSessionData = "Missing Auth Session Data"
const errorMissingProcessingTimeData = "Missing Processing Time Data"
const errorInvalidRequestMethod = "Invalid Request Method"
// config.C.JWTSecret
// internal server error based on endpoints
const (
errorFailedToRegister = "Failed to Register"
errorFailedToLogin = "Failed to Login"
errorFailedToListEmployees = "Failed to List Employees"
errorFailedToCreateEmployee = "Failed to Create Employees"
errorFailedToUpdateEmployee = "Failed to Update Employees"
)