generated from fun-stack/example
-
Notifications
You must be signed in to change notification settings - Fork 4
/
middleware.go
186 lines (157 loc) · 5.61 KB
/
middleware.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
package main
import (
"database/sql"
"encoding/json"
"net/http"
"reflect"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
"github.com/julienschmidt/httprouter"
"github.com/johnwarden/httperror"
"github.com/gorilla/schema"
"github.com/NYTimes/gziphandler"
)
// middleware converts a handler of type httperror.XHandlerFunc[P] into an
// httprouter.Handle. We use the former type for our http handler functions:
// this is a clean function signature that accepts parameters as a struct and
// returns an error. But we need to pass an httprouter.Handle to our router.
// So we wrap our httperror.XHandlerFunc[P], parsing the URL parameters to
// produce the parameter struct, passing it to the inner handler, then
// handling any errors that are returned.
func middleware[P any](routeName string, logger leveledLogger, onPanic func(error), h httperror.XHandlerFunc[P]) httprouter.Handle {
h = httperror.XPanicMiddleware[P](h)
h = prometheusMiddleware[P](routeName, h)
handleError := func(w http.ResponseWriter, err error) {
if errors.Is(err, httperror.Panic) {
// do this in a goroutine otherwise we get deadline if onPanic shutdowns the HTTP server
// because the http server shutdown function will wait for all requests to terminate,
// including this one!
go onPanic(err)
}
httperror.DefaultErrorHandler(w, err)
}
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var params P
err := unmarshalRouterRequest(r, ps, ¶ms)
if err != nil {
err = httperror.Wrap(err, http.StatusBadRequest)
logger.Error("unmarshalRouterRequest", err, "url", r.URL)
handleError(w, err)
return
}
err = h(w, r, params)
if err != nil {
if httperror.StatusCode(err) >= 500 {
logger.Error("executing handler", err, "url", r.URL)
requestErrorsTotal.Inc()
}
handleError(w, err)
}
}
}
var decoder = schema.NewDecoder()
func nullInt64Converter(value string) reflect.Value {
var result sql.NullInt64
if value != "" {
v, _ := strconv.ParseInt(value, 10, 64)
result = sql.NullInt64{Int64: v, Valid: true}
}
return reflect.ValueOf(result)
}
func nullFloat64Converter(value string) reflect.Value {
var result sql.NullFloat64
if value != "" {
v, _ := strconv.ParseFloat(value, 64)
result = sql.NullFloat64{Float64: v, Valid: true}
}
return reflect.ValueOf(result)
}
func init() {
decoder.RegisterConverter(sql.NullInt64{}, nullInt64Converter)
decoder.RegisterConverter(sql.NullFloat64{}, nullFloat64Converter)
}
// unmarshalRouterRequest is a generic request URL unmarshaler for use with
// httprouter. It unmarshals the request parameters parsed by httprouter, as
// well as any URL parameters, into a struct of any type, matching query
// names to struct field names.
func unmarshalRouterRequest(r *http.Request, ps httprouter.Params, params any) error {
if r.Method == "POST" {
err := json.NewDecoder(r.Body).Decode(params)
if err != nil {
return errors.Wrap(err, "decode json")
}
return nil
}
m := make(map[string][]string)
// First convert the httprouter.Params into a map
for _, p := range ps {
key := p.Key
if v, ok := m[key]; ok {
m[key] = append(v, p.Value)
} else {
m[key] = []string{p.Value}
}
}
// Then merge in the URL query parameters.
for key, values := range r.URL.Query() {
if v, ok := m[key]; ok {
m[key] = append(v, values...)
} else {
m[key] = values
}
}
// Then unmarshal.
err := decoder.Decode(params, m)
if err != nil {
if !strings.HasPrefix(err.Error(), "schema: invalid path") {
// ignore errors due to unrecognized parameters
return errors.Wrap(err, "decode parameters")
}
}
return nil
}
// preRouterMiddleware wraps the router itself. It is for middleware that does
// not need to know anything about the route (params, name, etc)
func (app app) preRouterMiddleware(handler http.Handler, writeTimeout time.Duration) http.Handler {
handler = app.cacheAndCompressMiddleware(handler)
handler = app.canonicalDomainMiddleware(handler) // redirects must happen before caching!
handler = app.timeoutMiddleware(handler, writeTimeout) // redirects must happen before caching!
return handler
}
// We could improve this middleware. Currently we cache before we
// compress, because the cache middleware we use here doesn't recognize the
// accept-encoding header, and if we compressed before we cache, cache
// entries would be randomly compressed or not, regardless of the
// accept-encoding header. Unfortunately by caching before we compress,
// requests are cached uncompressed. A compressed-cache middleware would be a
// nice improvement. Also our cache-control headers should be synced with the
// exact cache expiration time, which should be synced with the crawl. But
// what we have here is simple and probably good enough.
func (app app) cacheAndCompressMiddleware(handler http.Handler) http.Handler {
// if app.cacheSize > 0 {
// memorycached, err := memory.NewAdapter(
// memory.AdapterWithAlgorithm(memory.LRU),
// memory.AdapterWithCapacity(app.cacheSize),
// )
// if err != nil {
// LogFatal(app.logger, "memory.NewAdapater", err)
// }
// cacheClient, err := cache.NewClient(
// cache.ClientWithAdapter(memorycached),
// cache.ClientWithTTL(1*time.Minute),
// cache.ClientWithRefreshKey("opn"),
// )
// if err != nil {
// LogFatal(app.logger, "cache.NewClient", err)
// }
// var h http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// // since we update data only every minute, tell browsers to cache for one minute
// handler.ServeHTTP(w, r)
// })
// h = cacheClient.Middleware(h)
// }
h := handler
return gziphandler.GzipHandler(h)
}