-
Notifications
You must be signed in to change notification settings - Fork 1
/
special.go
144 lines (126 loc) · 4.77 KB
/
special.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
package nchi
import (
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/muir/nject"
"github.com/pkg/errors"
)
// only one field of special will be set at a time
type special struct {
serveFiles http.FileSystem
globalOPTIONS bool
methodNotAllowed bool
notFound bool
panicHandler bool
}
func bindSpecialHandler(name string, hf *http.Handler, combinedProviders *nject.Collection) error {
if *hf == nil {
var handler http.HandlerFunc
err := combinedProviders.Bind(&handler, nil)
if err != nil {
return errors.Wrapf(err, "bind %s", name)
}
*hf = handler
}
return nil
}
func (mux *Mux) bindSpecial(router *httprouter.Router, combinedPath string, combinedProviders *nject.Collection) error {
switch {
case mux.special.serveFiles != nil:
router.ServeFiles(combinedPath, mux.special.serveFiles)
case mux.special.globalOPTIONS:
return bindSpecialHandler("GlobalOPTIONS", &router.GlobalOPTIONS, combinedProviders)
case mux.special.methodNotAllowed:
return bindSpecialHandler("MethodNotAllowed", &router.MethodNotAllowed, combinedProviders)
case mux.special.notFound:
return bindSpecialHandler("NotFound", &router.NotFound, combinedProviders)
case mux.special.panicHandler:
if router.PanicHandler == nil {
var ph func(w http.ResponseWriter, r *http.Request, rec RecoverInterface)
err := combinedProviders.Bind(&ph, nil)
if err != nil {
return errors.Wrap(err, "bind PanicHandler")
}
router.PanicHandler = func(w http.ResponseWriter, r *http.Request, rec interface{}) {
ph(w, r, rec)
}
}
default:
return errors.New("should not get here")
}
return nil
}
func (mux *Mux) addSpecial(name string, providers []interface{}) *Mux {
return mux.add(&Mux{
providers: nject.Sequence(name, translateMiddleware(providers)...),
special: &special{},
})
}
// The following comment is derrived from https://github.com/julienschmidt/httprouter
// GlobalOPTIONS sets a handler that is called on automatically on OPTIONS requests.
// The handler is only called if HandleOPTIONS is true and no OPTIONS
// handler for the specific path was set.
// The "Allowed" header is set before calling the handler.
//
// Only the first GobalOPTIONS call counts.
func (mux *Mux) GlobalOPTIONS(providers ...interface{}) {
mux.addSpecial("globalOPTIONS", providers).special.globalOPTIONS = true
}
// The following comment is derrived from https://github.com/julienschmidt/httprouter
// NotFound sets a handler
// which is called when no matching route is
// found. If it is not set, http.NotFound is used.
//
// Only the first NotFound call counts.
func (mux *Mux) NotFound(providers ...interface{}) {
mux.addSpecial("notFound", providers).special.notFound = true
}
// The following comment is derrived from https://github.com/julienschmidt/httprouter
// MethodNotAllowed sets a handler
// which is called when a request
// cannot be routed and HandleMethodNotAllowed is true.
// If it is not set, http.Error with http.StatusMethodNotAllowed is used.
// The "Allow" header with allowed request methods is set before the handler
// is called.
//
// Only the first MethodNotFound call counts.
func (mux *Mux) MethodNotAllowed(providers ...interface{}) {
mux.addSpecial("methodNotAllowed", providers).special.methodNotAllowed = true
}
type RecoverInterface interface{}
// The following comment is derrived from https://github.com/julienschmidt/httprouter
// PanicHandler sets a handler
// to handle panics recovered from http handlers.
// It should be used to generate a error page and return the http error code
// 500 (Internal Server Error).
// The handler can be used to keep your server from crashing because of
// unrecovered panics.
//
// The type RecoverInterface can be used to receive the interface{} that
// is returned from recover().
//
// Alternatively, use the nvelope.CatchPanic middleware to catch panics.
//
// Only the first PanicHandler call counts.
func (mux *Mux) PanicHandler(providers ...interface{}) {
mux.addSpecial("panicHandler", providers).special.panicHandler = true
}
// The following comment is copied from https://github.com/julienschmidt/httprouter
// ServeFiles serves files from the given file system root. The path
// must end with "/*filepath", files are then served from the local path
// /defined/root/dir/*filepath. For example if root is "/etc" and *filepath
// is "passwd", the local file "/etc/passwd" would be served. Internally
// a http.FileServer is used, therefore http.NotFound is used instead of
// the Router's NotFound handler. To use the operating system's file system
// implementation, use http.Dir:
//
// Currently, ServeFiles does not use any middleware. That may change in
// a future release.
func (mux *Mux) ServeFiles(path string, fs http.FileSystem) {
mux.add(&Mux{
path: path,
special: &special{
serveFiles: fs,
},
})
}