-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
116 lines (97 loc) · 2.57 KB
/
handler.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
package api_utils
import (
"encoding/json"
"encoding/xml"
"io/ioutil"
"net/http"
"github.com/asaskevich/govalidator"
)
type IDefaultHttpHandler interface {
Handle(w http.ResponseWriter, r *http.Request)
}
type DefaultHttpHandler struct {
IHttpBodyUnmarshaler
IStructValidator
IDefaultService
IRespWriter
}
/* Example Annotations
// CreateUser example
// @Summary Create new User
// @Description Create new User
// @ID create-user
// @Accept json
// @Produce json
// @Param models.CreateUserReq body models.CreateUserReq true "CreateUserReq"
// @Success 201 {object} models.Response
// @Failure 400 {object} models.Response
// @Failure 409 {object} models.Response
// @Router /user/ [post]
*/
func (dh DefaultHttpHandler) Handle(w http.ResponseWriter, r *http.Request) {
resp := dh.Process(r.Context(), nil)
dh.Write(w, 200, map[string]interface{}{"message": resp})
}
func GetDefaultHandler() DefaultHttpHandler {
return DefaultHttpHandler{
IHttpBodyUnmarshaler: &JsonBodyUnmarshaler{},
IStructValidator: &ReqStructValidator{},
IDefaultService: &DefaultService{},
IRespWriter: &JsonRespWriter{},
}
}
type IHttpBodyUnmarshaler interface {
Unmarshal(r *http.Request, req interface{}) (err error)
}
type JsonBodyUnmarshaler struct {
}
func (rf *JsonBodyUnmarshaler) Unmarshal(r *http.Request, req interface{}) (err error) {
bodyByte, err := ioutil.ReadAll(r.Body)
if err != nil {
return
}
err = json.Unmarshal(bodyByte, req)
if err != nil {
return
}
return
}
type IStructValidator interface {
ValidateStruct(req interface{}) (err error)
}
type ReqStructValidator struct {
}
func (rsv *ReqStructValidator) ValidateStruct(req interface{}) (err error) {
_, err = govalidator.ValidateStruct(req)
if err != nil {
return
}
return
}
type IRespWriter interface {
Write(w http.ResponseWriter, httpCode int, resp interface{})
}
type TextRespWriter struct {
}
func (rw *TextRespWriter) Write(w http.ResponseWriter, httpCode int, resp interface{}) {
w.Header().Add("Content-type", "text/html")
w.WriteHeader(httpCode)
rByte := []byte(resp.(string))
w.Write(rByte)
}
type JsonRespWriter struct {
}
func (rw *JsonRespWriter) Write(w http.ResponseWriter, httpCode int, resp interface{}) {
w.Header().Add("Content-type", "application/json")
w.WriteHeader(httpCode)
rByte, _ := json.Marshal(&resp)
w.Write(rByte)
}
type XmlRespWriter struct {
}
func (rw *XmlRespWriter) Write(w http.ResponseWriter, httpCode int, resp interface{}) {
w.Header().Add("Content-type", "application/xml")
w.WriteHeader(httpCode)
rByte, _ := xml.Marshal(&resp)
w.Write(rByte)
}