-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandlerFuncs.go
More file actions
101 lines (83 loc) · 3.26 KB
/
handlerFuncs.go
File metadata and controls
101 lines (83 loc) · 3.26 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
package shiftapi
import (
"fmt"
"net/http"
"reflect"
)
func registerRoute[In, Resp any](
api *API,
method string,
path string,
fn HandlerFunc[In, Resp],
options ...RouteOption,
) {
cfg := applyRouteOptions(options)
var in In
inType := reflect.TypeOf(in)
// Dereference pointer to get the underlying struct type
rawInType := inType
for rawInType != nil && rawInType.Kind() == reflect.Pointer {
rawInType = rawInType.Elem()
}
hasQuery, hasBody, hasForm := partitionFields(rawInType)
var queryType reflect.Type
if hasQuery {
queryType = rawInType
}
// POST/PUT/PATCH conventionally carry a request body, so always attempt
// body decode for these methods — even when the input is struct{}.
// This means Post(api, path, func(r, _ struct{}) ...) requires at least "{}".
methodRequiresBody := method == http.MethodPost || method == http.MethodPut || method == http.MethodPatch
decodeBody := !hasForm && (hasBody || methodRequiresBody)
var bodyType reflect.Type
if !hasForm {
if hasBody {
bodyType = inType
} else if methodRequiresBody {
bodyType = rawInType
}
}
var resp Resp
outType := reflect.TypeOf(resp)
if err := api.updateSchema(method, path, queryType, bodyType, outType, hasForm, rawInType, cfg.info, cfg.status); err != nil {
panic(fmt.Sprintf("shiftapi: schema generation failed for %s %s: %v", method, path, err))
}
pattern := fmt.Sprintf("%s %s", method, path)
api.mux.HandleFunc(pattern, adapt(fn, cfg.status, api.validateBody, hasQuery, decodeBody, hasForm, api.maxUploadSize))
}
// Get registers a GET handler.
func Get[In, Resp any](api *API, path string, fn HandlerFunc[In, Resp], options ...RouteOption) {
registerRoute(api, http.MethodGet, path, fn, options...)
}
// Post registers a POST handler.
func Post[In, Resp any](api *API, path string, fn HandlerFunc[In, Resp], options ...RouteOption) {
registerRoute(api, http.MethodPost, path, fn, options...)
}
// Put registers a PUT handler.
func Put[In, Resp any](api *API, path string, fn HandlerFunc[In, Resp], options ...RouteOption) {
registerRoute(api, http.MethodPut, path, fn, options...)
}
// Patch registers a PATCH handler.
func Patch[In, Resp any](api *API, path string, fn HandlerFunc[In, Resp], options ...RouteOption) {
registerRoute(api, http.MethodPatch, path, fn, options...)
}
// Delete registers a DELETE handler.
func Delete[In, Resp any](api *API, path string, fn HandlerFunc[In, Resp], options ...RouteOption) {
registerRoute(api, http.MethodDelete, path, fn, options...)
}
// Head registers a HEAD handler.
func Head[In, Resp any](api *API, path string, fn HandlerFunc[In, Resp], options ...RouteOption) {
registerRoute(api, http.MethodHead, path, fn, options...)
}
// Options registers an OPTIONS handler.
func Options[In, Resp any](api *API, path string, fn HandlerFunc[In, Resp], options ...RouteOption) {
registerRoute(api, http.MethodOptions, path, fn, options...)
}
// Trace registers a TRACE handler.
func Trace[In, Resp any](api *API, path string, fn HandlerFunc[In, Resp], options ...RouteOption) {
registerRoute(api, http.MethodTrace, path, fn, options...)
}
// Connect registers a CONNECT handler.
func Connect[In, Resp any](api *API, path string, fn HandlerFunc[In, Resp], options ...RouteOption) {
registerRoute(api, http.MethodConnect, path, fn, options...)
}