Repository navigation
Expand file tree
/
Copy pathapi.go
More file actions
170 lines (146 loc) · 5.43 KB
/
Copy pathapi.go
File metadata and controls
170 lines (146 loc) · 5.43 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package api
import (
"net/http"
"github.com/jgolang/api/core"
)
// PPPGMethodsKey POST, PUT, PATCH, and GET http methods ..
const PPPGMethodsKey = "pppg"
// PPPMethodsKey POST, PUT and PATCH http methods ..
const PPPMethodsKey = "ppp"
// PPMethodsKey POST and PUT http methods ..
const PPMethodsKey = "pp"
// MethodPostKey POST http method key ..
const MethodPostKey = "post"
// MethodGetKey GET http method key ..
const MethodGetKey = "get"
// MethodPutKey PUT http method key ..
const MethodPutKey = "put"
// MethodPatchKey PATCH http method key ..
const MethodPatchKey = "patch"
// responseFormatter is the formatter registered in api, kept here because
// core.API does not expose it; ResponseWriter formats its 500 body with it.
var responseFormatter core.APIResponseFormatter = ResponseFormatter{}
var api = core.New(
RequestReceiver{},
responseFormatter,
ResponseWriter{},
&SecurityGuaranter{},
&mapMethods,
ParamValidatorV0,
)
// Write API response in JSON format in screen. You can to define response
// JSON format implemented the APIResponseFormatter interface. Write takes no
// request, so it always uses the default instance, never one attached by
// (*API).Handler.
func Write(data core.ResponseData, w http.ResponseWriter) {
api.Write(data, w)
}
// ProcessEncryptedBody API request. You can to define request url encoding format and how to
// validate it implemented the APIRequestReciver interface.
// Without an instance in r, it replaces r.Body with an http.MaxBytesReader
// at 10 MiB, so a larger body returns an *http.MaxBytesError. It has no
// ResponseWriter, so the server is not told to close the connection on
// overflow; ProcessRequest and RequestBody pass theirs.
func ProcessEncryptedBody(r *http.Request) (*EncryptedRequest, error) {
boundDefaultBody(nil, r)
requestData, err := instanceFrom(r).core.ProcessEncryptedBody(r)
return &EncryptedRequest{
RequestEncryptedData: requestData,
}, err
}
// ProcessBody API request. You can to define request JSON format and how to
// validate it implemented the APIRequestReciver interface.
// Without an instance in r, it replaces r.Body with an http.MaxBytesReader
// at 10 MiB, so a larger body returns an *http.MaxBytesError. It has no
// ResponseWriter, so the server is not told to close the connection on
// overflow; ProcessRequest and RequestBody pass theirs.
func ProcessBody(r *http.Request) (*RequestContext, error) {
boundDefaultBody(nil, r)
return processBody(r)
}
// processBody is ProcessBody for a caller that already bounded the body.
func processBody(r *http.Request) (*RequestContext, error) {
requestDataContext, err := instanceFrom(r).core.ProcessBody(r)
return &RequestContext{
RequestDataContext: requestDataContext,
}, err
}
// RegisterNewAPIResponseFormatter inject a new implementation in the
// APIResponseFormatter interface.
//
// Deprecated: use api.New(api.WithResponseFormatter(...)) instead. It affects only the
// default instance. The package-level Write(data, w) and the middlewares
// outside (*API).Handler always use the default instance's formatter and
// writer.
func RegisterNewAPIResponseFormatter(formatter core.APIResponseFormatter) {
responseFormatter = formatter
api.RegisterNewAPIResponseFormatter(formatter)
}
// RegisterNewAPIResponseWriter inject a new implementation in the
// core.APIResponseWriter interface.
//
// Deprecated: use api.New(api.WithResponseWriter(...)) instead. It affects only the
// default instance. The package-level Write(data, w) and the middlewares
// outside (*API).Handler always use the default instance's formatter and
// writer.
func RegisterNewAPIResponseWriter(writer core.APIResponseWriter) {
api.RegisterNewAPIResponseWriter(writer)
}
// RegisterNewAPIRequestReceiver inject a new implementation in the
// core.APIRequestReceiver interface.
//
// Deprecated: use api.New(api.WithRequestReceiver(...)) instead. It affects only the
// default instance.
func RegisterNewAPIRequestReceiver(receiver core.APIRequestReceiver) {
api.RegisterNewAPIRequestReceiver(receiver)
}
// RegisterNewAPISecurityGuarantor inject a new implementation in the
// core.APISecurityGuarantor interface. Every instance from New shares it.
func RegisterNewAPISecurityGuarantor(guarantor core.APISecurityGuarantor) {
api.RegisterNewAPISecurityGuarantor(guarantor)
}
// ValidateMethods validates if a method exist in a methods map.
func ValidateMethods(keyMapMethod, method string) bool {
return api.ValidateMethods(keyMapMethod, method)
}
// AddNewMapMethod add a new method in a map of methods.
func AddNewMapMethod(key string, methods []string) {
api.AddMapMethod(key, methods)
}
// RegisterParamValidator inject a new implementation in the Validator.
// Every instance from New shares it.
func RegisterParamValidator(paramValidator func(any) (errMsg string, err error)) {
api.RegisterParamValidator(paramValidator)
}
var mapMethods core.MapMethods
func init() {
SkipPaths = make(map[SkipPath]bool)
mapMethods = make(core.MapMethods)
mapMethods[PPPGMethodsKey] = []string{
http.MethodPost,
http.MethodGet,
http.MethodPut,
http.MethodPatch,
}
mapMethods[PPPMethodsKey] = []string{
http.MethodPost,
http.MethodPut,
http.MethodPatch,
}
mapMethods[PPMethodsKey] = []string{
http.MethodPost,
http.MethodPut,
}
mapMethods[MethodPostKey] = []string{
http.MethodPost,
}
mapMethods[MethodGetKey] = []string{
http.MethodGet,
}
mapMethods[MethodPutKey] = []string{
http.MethodPut,
}
mapMethods[MethodPatchKey] = []string{
http.MethodPatch,
}
}