-
Notifications
You must be signed in to change notification settings - Fork 0
/
routing-builder.go
230 lines (200 loc) · 6.43 KB
/
routing-builder.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package mvp
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"regexp"
"sort"
"strings"
"github.com/andreyvit/mvp/httperrors"
mvpm "github.com/andreyvit/mvp/mvpmodel"
"github.com/andreyvit/mvp/mvputil"
"github.com/uptrace/bunrouter"
"golang.org/x/exp/maps"
)
type (
// RouteBuilder helps to define named routes, and holds the path and middleware.
RouteBuilder struct {
app *App
bg *bunrouter.Group
path string
routingContext
}
RouteOption = any
RouteFlagOption int
)
const (
ReadOnly RouteFlagOption = 1 + iota
Mutator
IdempotentMutator
)
const (
methodGetOrPost = "GET/POST"
)
var (
funcNameRegexp = regexp.MustCompile(`^[A-Z][a-zA-Z0-9_]*$`)
)
func (g *RouteBuilder) Raw() *bunrouter.Group {
return g.bg
}
// group defines a group of routes with a common prefix and/or middleware.
func (g *RouteBuilder) Group(path string, f func(b *RouteBuilder)) {
sg := RouteBuilder{
app: g.app,
bg: g.bg.NewGroup(path),
path: g.path + path,
routingContext: g.routingContext.clone(),
}
f(&sg)
}
func (g *RouteBuilder) Static(path string) {
setupStaticServer(g.bg, path, g.app.staticFS)
}
// Route defines a named route. methodAndPath are space-separated.
//
// The handler function must have func(rc *RC, in *SomeStruct) (output, error) signature,
// where output can be anything, but must be something that app.writeResponse accepts.
func (g *RouteBuilder) Route(routeName string, methodAndPath string, f any, options ...RouteOption) *Route {
method, path, ok := strings.Cut(methodAndPath, " ")
if !ok {
panic(fmt.Errorf(`%s: %q is not a valid "METHOD path" string`, routeName, methodAndPath))
}
mi, ok := validHTTPMethods[method]
if !ok {
panic(fmt.Errorf(`%s: %q is not a valid "METHOD path" string, method %q is invalid`, routeName, methodAndPath, method))
}
return g.addRoute(routeName, method, path, mi.Idempotent, f, options...)
}
func (g *RouteBuilder) RouteForm(routeName string, path string, f any, options ...RouteOption) *Route {
if strings.ContainsAny(path, " ") {
panic(fmt.Errorf("%v: RouteForm routes cannot contain methods or spaces in path: %q", routeName, path))
}
get := g.addRoute(routeName, "GET", path, true, f, options...)
g.addRoute(routeName+".save", "POST", path, false, f, options...)
return get
}
func (g *RouteBuilder) Func(f any, options ...RouteOption) *Route {
var funcName string
var possibleFuncNameOpt string
if len(options) > 0 {
if s, ok := options[0].(string); ok {
if funcNameRegexp.MatchString(s) {
funcName = s
} else {
possibleFuncNameOpt = s
}
}
}
if funcName == "" {
implFuncName := mvputil.FuncName(f)
var ok bool
funcName, ok = strings.CutPrefix(implFuncName, "do")
if ok && !funcNameRegexp.MatchString(funcName) {
ok = false
}
if !ok {
if possibleFuncNameOpt == "" {
panic(fmt.Errorf(`func %q must have "do" prefix followed by a valid func name matching %v (e.g. "doMyCall")`, implFuncName, funcNameRegexp))
} else {
panic(fmt.Errorf(`either func %q must have "do" prefix followed by a valid func name (e.g. "doMyCall"), or option %q must be a valid func name matching %v`, implFuncName, possibleFuncNameOpt, funcNameRegexp))
}
}
}
return g.addRoute(funcName, methodGetOrPost, "/"+funcName, false, f, options...)
}
func (g *RouteBuilder) addRoute(routeName, method, path string, isIdempotentByDefault bool, f any, options ...RouteOption) *Route {
fv := reflect.ValueOf(f)
ft := fv.Type()
if ft.Kind() != reflect.Func {
panic(fmt.Errorf(`%s: function is invalid, got %v, wanted a function`, routeName, ft))
}
if ft.NumOut() != 2 || ft.Out(1) != errorType {
panic(fmt.Errorf(`%s: got %v, wanted a function returning (something, error)`, routeName, ft))
}
if ft.NumIn() != 2 || ft.In(0).Kind() != reflect.Ptr || ft.In(1).Kind() != reflect.Ptr || ft.In(1).Elem().Kind() != reflect.Struct {
panic(fmt.Errorf(`%s: got %v, wanted a function accepting (*RC, *SomeStruct)`, routeName, ft))
}
rcFacet := BaseRC.FacetByPtrType(ft.In(0))
if rcFacet == nil {
panic(fmt.Errorf(`%s: got %v, wanted a function accepting (*RC, *SomeStruct), where RC is any of RC facets`, routeName, ft))
}
// inTypPtr := ft.In(1)
inTyp := ft.In(1).Elem()
route := &Route{
routeName: routeName,
method: method,
path: g.path + path,
funcVal: fv,
rcFacet: rcFacet,
inType: inTyp,
idempotent: isIdempotentByDefault,
routingContext: g.routingContext.clone(),
}
exampleInput := reflect.New(inTyp).Interface()
exampleInputBytes := must(json.Marshal(exampleInput))
var exampleInputMap map[string]any
ensure(json.Unmarshal(exampleInputBytes, &exampleInputMap))
bodyParamNames := maps.Keys(exampleInputMap)
sort.Strings(bodyParamNames)
route.bodyParamNames = bodyParamNames
for _, param := range pathParamsRe.FindAllString(route.path, -1) {
route.pathParams = append(route.pathParams, param[1:])
}
for _, opt := range options {
switch opt := opt.(type) {
case RateLimitPreset:
route.rateLimitPreset = opt
case mvpm.StoreAffinity:
route.storeAffinity = opt
if opt.IsWriter() {
route.idempotent = false
}
case RouteFlagOption:
switch opt {
case Mutator, IdempotentMutator:
route.idempotent = false
case ReadOnly:
route.idempotent = true
}
default:
panic(fmt.Errorf("%s: invalid option %T %v", routeName, opt, opt))
}
}
var addPostAlternative bool
if route.method == methodGetOrPost {
if route.idempotent {
route.method = http.MethodGet
addPostAlternative = true
} else {
route.method = http.MethodPost
}
}
route.desc = routeName + " " + method + " " + route.path
if route.storeAffinity == mvpm.UnknownAffinity {
if route.idempotent {
route.storeAffinity = mvpm.SafeReader
} else {
route.storeAffinity = mvpm.SafeWriter
}
}
if prev := g.app.routesByName[route.routeName]; prev != nil {
panic(fmt.Errorf("route %s: duplicate path %s, previous was %s", route.routeName, route.method+" "+route.path, prev.method+" "+prev.path))
}
g.app.routesByName[route.routeName] = route
handler := func(w http.ResponseWriter, req bunrouter.Request) error {
rc := g.app.NewHTTPRequestRC(w, req)
defer rc.Close()
err := g.app.callRoute(route, rc, w, req)
logRequest(rc, req.Request, err)
if err != nil {
http.Error(w, err.Error(), httperrors.HTTPCode(err))
}
return nil
}
g.bg.Handle(route.method, path, handler)
if addPostAlternative {
g.bg.Handle(http.MethodPost, path, handler)
}
return route
}