-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
254 lines (233 loc) · 6.63 KB
/
handler.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package mux
import (
"bytes"
"errors"
"fmt"
"io"
"io/fs"
"log"
"net/http"
"net/http/httptest"
"net/url"
"runtime/debug"
"sync/atomic"
"time"
"golang.org/x/text/language"
)
// Handler is a http.Handler with application lifecycle helpers.
type Handler struct {
router Router
middleware []func(http.Handler) http.Handler
locales *localeMatcher
decoder DecoderFunc
encoder EncoderFunc
resolver Resolver
pool Pool
log Logger
observer Observer
}
// Logger represents the ability to log errors.
type Logger func(req *http.Request, err error)
// HandlerFunc represents a HTTP handler with error handling.
type HandlerFunc func(w http.ResponseWriter, req *http.Request) error
// New returns a new handler.
func New(opts ...Option) *Handler {
h := &Handler{}
for _, option := range opts {
option(h)
}
if h.router == nil {
h.router = &tree{}
}
if h.locales == nil {
h.locales = newLocaleMatcher([]language.Tag{language.English})
}
if h.decoder == nil {
h.decoder = NewContentTypeDecoder(map[string]Decoder{
"application/json": &jsonDecoder{},
})
}
if h.encoder == nil {
encoder := &jsonEncoder{}
h.encoder = NewAcceptEncoder(map[string]Encoder{
"": encoder,
"*/*": encoder,
"application/*": encoder,
"application/json": encoder,
})
}
if h.resolver == nil {
h.resolver = ResolverFunc(defaultResolver)
}
if h.pool == nil {
h.pool = &pool{free: make(chan *bytes.Buffer, 1<<6)}
}
if h.log == nil {
h.log = defaultLogger
}
if h.observer == nil {
h.observer = &discardObserver{}
}
return h
}
// Add registers a HandlerFunc.
func (h *Handler) Add(pattern string, handler HandlerFunc, opts ...RouteOption) *Route {
fn := func(w http.ResponseWriter, req *http.Request) {
err := handler(w, req)
if err != nil {
h.Abort(w, req, err)
}
}
return h.Handle(pattern, http.Handler(http.HandlerFunc(fn)), opts...)
}
// Build returns the URL for the named route.
func (h *Handler) Build(name string, params Params) (string, error) {
b, ok := h.router.(Builder)
if !ok {
return "", errors.New("mux: router is not a Builder")
}
return b.Build(name, params)
}
// FileServer registers a fs.FS as a file server.
//
// The pattern is expected to be a prefix wildcard route.
// The pattern prefix is removed from the request URL before handled.
//
// If fs is an implementation of CacheControlFS, the files will be
// served with the associated Cache-Control policy.
//
// Wrap the fs with AssetCacheFS to apply an aggressive caching policy,
// suitable for asset file names that contain a hash of their contents.
func (h *Handler) FileServer(pattern string, fs fs.FS, opts ...RouteOption) *Route {
opt := WithMethod(http.MethodGet)
opts = append([]RouteOption{opt}, opts...)
prefix := pattern[:len(pattern)-1]
handler := http.StripPrefix(prefix, &fileServer{h, fs})
return h.Handle(pattern, handler, opts...)
}
// Handle registers a standard net/http Handler.
func (h *Handler) Handle(pattern string, handler http.Handler, opts ...RouteOption) *Route {
opt := WithMiddleware(h.middleware...)
opts = append([]RouteOption{opt}, opts...)
r := NewRoute(pattern, handler, opts...)
err := h.router.Add(r)
if err != nil {
panic(err)
}
return r
}
// Redirect replies to the request with a redirect.
func (h *Handler) Redirect(url string, code int) error {
return ErrRedirect{URL: url, Code: code}
}
// RedirectTo replies to the request with a redirect to a named route.
func (h *Handler) RedirectTo(name string, params Params, query url.Values, code int) error {
url, err := h.Build(name, params)
if err != nil {
return err
}
if len(query) > 0 {
url += "?" + query.Encode()
}
return h.Redirect(url, code)
}
// ServeHTTP initializes a new request context and dispatches
// to the matching route by calling it's prepared handler.
//
// ServeHTTP implements the http.Handler interface.
func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
t := time.Now().UTC()
n := atomic.AddUint64(&seq, 1)
rc := &requestContext{seq: n, locale: h.locales.match(req)}
req = setContext(req, rc)
defer h.abort(w, req)
r, params, err := h.router.Match(req)
if err != nil {
merr, ok := err.(ErrMethodNotAllowed)
if ok && req.Method == http.MethodOptions {
allowed := merr.Error()
w.Header().Set("Allow", allowed)
return
}
h.Abort(w, req, err)
return
}
rc.route = r
rc.params = params
h.observer.Begin(req)
defer h.observer.Commit(req, t)
r.ServeHTTP(w, req)
}
// abort resolves an error if the application panics.
func (h *Handler) abort(w http.ResponseWriter, req *http.Request) {
err := recover()
if err != nil {
p := Panic{err: err, stack: debug.Stack()}
h.Abort(w, req, p)
}
}
// Use appends middleware to the global middleware stack.
func (h *Handler) Use(middleware ...func(http.Handler) http.Handler) {
h.middleware = append(h.middleware, middleware...)
}
// Walk walks the named routes if the Router is a Walker.
func (h *Handler) Walk(fn WalkFunc) error {
w, ok := h.router.(Walker)
if !ok {
return errors.New("mux: router is not a Walker")
}
return w.Walk(fn)
}
// Export walks the named routes and applies the exporter to the response body.
// A nil exporter writes to the dist directory within the current working
// directory. See FileSystemExporter documentation for more details.
func (h *Handler) Export(exporter Exporter) error {
server := httptest.NewServer(h)
defer server.Close()
if exporter == nil {
exporter = FileSystemExporter("dist")
}
fn := func(r *Route) error {
route := r.Pattern()
resp, err := http.Get(server.URL + route)
if err != nil {
return err
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
return exporter.Export(r, b)
}
return h.Walk(fn)
}
// Query returns the first query value associated with the given key.
// If there are no values associated with the key, Query returns the
// empty string.
func Query(req *http.Request, name string) string {
return req.URL.Query().Get(name)
}
// RequestID returns the request identifier from the X-Request-ID header.
// The formatted request sequence number is returned if the header is not set.
func RequestID(req *http.Request) string {
id := req.Header.Get("X-Request-ID")
if id == "" {
n := Sequence(req)
id = fmt.Sprintf("%d", n)
}
return id
}
// defaultResolver represents the default resolver.
func defaultResolver(req *http.Request, code int, err error) Error {
return NewErrorView(req, code, err)
}
// defaultLogger represents the default logger.
func defaultLogger(req *http.Request, err error) {
message := err.Error()
perr, ok := err.(Panic)
if ok {
message += "\n" + perr.String()
}
log.Println(message)
}