-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
router.go
146 lines (126 loc) · 3.73 KB
/
router.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
package http
import (
"fmt"
"net/http"
"os"
"runtime/debug"
"sync"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-stack/stack"
"github.com/influxdata/httprouter"
"github.com/influxdata/influxdb/v2/kit/platform/errors"
kithttp "github.com/influxdata/influxdb/v2/kit/transport/http"
influxlogger "github.com/influxdata/influxdb/v2/logger"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// NewRouter returns a new router with a 404 handler, a 405 handler, and a panic handler.
func NewRouter(h errors.HTTPErrorHandler) *httprouter.Router {
b := baseHandler{HTTPErrorHandler: h}
router := httprouter.New()
router.NotFound = http.HandlerFunc(b.notFound)
router.MethodNotAllowed = http.HandlerFunc(b.methodNotAllowed)
router.PanicHandler = b.panic
router.AddMatchedRouteToContext = true
return router
}
// NewBaseChiRouter returns a new chi router with a 404 handler, a 405 handler, and a panic handler.
func NewBaseChiRouter(api *kithttp.API) chi.Router {
router := chi.NewRouter()
router.NotFound(func(w http.ResponseWriter, r *http.Request) {
api.Err(w, r, &errors.Error{
Code: errors.ENotFound,
Msg: "path not found",
})
})
router.MethodNotAllowed(func(w http.ResponseWriter, r *http.Request) {
api.Err(w, r, &errors.Error{
Code: errors.EMethodNotAllowed,
Msg: fmt.Sprintf("allow: %s", w.Header().Get("Allow")),
})
})
router.Use(
panicMW(api),
kithttp.SkipOptions,
middleware.StripSlashes,
kithttp.SetCORS,
)
return router
}
type baseHandler struct {
errors.HTTPErrorHandler
}
// notFound represents a 404 handler that return a JSON response.
func (h baseHandler) notFound(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
pe := &errors.Error{
Code: errors.ENotFound,
Msg: "path not found",
}
h.HandleHTTPError(ctx, pe, w)
}
// methodNotAllowed represents a 405 handler that return a JSON response.
func (h baseHandler) methodNotAllowed(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
allow := w.Header().Get("Allow")
pe := &errors.Error{
Code: errors.EMethodNotAllowed,
Msg: fmt.Sprintf("allow: %s", allow),
}
h.HandleHTTPError(ctx, pe, w)
}
// panic handles panics recovered from http handlers.
// It returns a json response with http status code 500 and the recovered error message.
func (h baseHandler) panic(w http.ResponseWriter, r *http.Request, rcv interface{}) {
ctx := r.Context()
pe := &errors.Error{
Code: errors.EInternal,
Msg: "a panic has occurred",
Err: fmt.Errorf("%s: %v", r.URL.String(), rcv),
}
l := getPanicLogger()
if entry := l.Check(zapcore.ErrorLevel, pe.Msg); entry != nil {
entry.Stack = string(debug.Stack())
entry.Write(zap.Error(pe.Err))
}
h.HandleHTTPError(ctx, pe, w)
}
func panicMW(api *kithttp.API) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
defer func() {
panicErr := recover()
if panicErr == nil {
return
}
pe := &errors.Error{
Code: errors.EInternal,
Msg: "a panic has occurred",
Err: fmt.Errorf("%s: %v", r.URL.String(), panicErr),
}
l := getPanicLogger()
if entry := l.Check(zapcore.ErrorLevel, pe.Msg); entry != nil {
entry.Stack = fmt.Sprintf("%+v", stack.Trace())
entry.Write(zap.Error(pe.Err))
}
api.Err(w, r, pe)
}()
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
}
var panicLogger = zap.NewNop()
var panicLoggerOnce sync.Once
// getPanicLogger returns a logger for panicHandler.
func getPanicLogger() *zap.Logger {
panicLoggerOnce.Do(func() {
conf := influxlogger.NewConfig()
logger, err := conf.New(os.Stderr)
if err == nil {
panicLogger = logger.With(zap.String("handler", "panic"))
}
})
return panicLogger
}