-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnew.go
75 lines (63 loc) · 2.57 KB
/
new.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
package nchi
import (
"github.com/julienschmidt/httprouter"
"github.com/muir/nject"
)
type Option func(*rtr)
// rtr is defined the way it is so to prevent users from defining their own Options
type rtr struct {
*httprouter.Router
}
// The following comment is copied from https://github.com/julienschmidt/httprouter
// WithRedirectTrailingSlash enables/disables automatic redirection if the current route can't be matched but a
// handler for the path with (without) the trailing slash exists.
// For example if /foo/ is requested but a route only exists for /foo, the
// client is redirected to /foo with http status code 301 for GET requests
// and 307 for all other request methods. The default is: enabled.
func WithRedirectTrailingSlash(b bool) Option {
return func(r *rtr) {
r.RedirectTrailingSlash = b
}
}
// The following comment is copied from https://github.com/julienschmidt/httprouter
// WithRedirectFixedPath enables/disables trying to fix the current request path, if no
// handle is registered for it.
// First superfluous path elements like ../ or // are removed.
// Afterwards the router does a case-insensitive lookup of the cleaned path.
// If a handle can be found for this route, the router makes a redirection
// to the corrected path with status code 301 for GET requests and 307 for
// all other request methods.
// For example /FOO and /..//Foo could be redirected to /foo.
// RedirectTrailingSlash is independent of this option. The default is: enabled.
func WithRedirectFixedPath(b bool) Option {
return func(r *rtr) {
r.RedirectFixedPath = b
}
}
// The following comment is copied from https://github.com/julienschmidt/httprouter
// WithHandleMethodNotAllowed enables/disables, checking if another method is allowed for the
// current route, if the current request can not be routed.
// If this is the case, the request is answered with 'Method Not Allowed'
// and HTTP status code 405.
// If no other Method is allowed, the request is delegated to the NotFound
// handler. The default is: enabled..
func WithHandleMethodNotAllowed(b bool) Option {
return func(r *rtr) {
r.HandleMethodNotAllowed = b
}
}
// The following comment is copied from https://github.com/julienschmidt/httprouter
// WIthHandleOPTIONS enables/disables automatic replies to OPTIONS requests.
// Custom OPTIONS handlers take priority over automatic replies.
// The default is: enabled.
func WithHandleOPTIONS(b bool) Option {
return func(r *rtr) {
r.HandleOPTIONS = b
}
}
func NewRouter(options ...Option) *Mux {
return &Mux{
providers: nject.Sequence("router"),
options: options,
}
}