2018-12-15 15:33:54 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2018-12-21 06:48:58 +00:00
|
|
|
"fmt"
|
2018-12-15 15:33:54 +00:00
|
|
|
"net/http"
|
2019-01-07 04:12:41 +00:00
|
|
|
"os"
|
|
|
|
"runtime/debug"
|
|
|
|
"sync"
|
2018-12-15 15:33:54 +00:00
|
|
|
|
2019-11-06 18:16:52 +00:00
|
|
|
"github.com/go-chi/chi"
|
2019-12-09 23:54:16 +00:00
|
|
|
"github.com/go-chi/chi/middleware"
|
2020-06-15 23:50:29 +00:00
|
|
|
"github.com/go-stack/stack"
|
2019-11-25 14:22:19 +00:00
|
|
|
"github.com/influxdata/httprouter"
|
2020-04-03 17:39:20 +00:00
|
|
|
platform "github.com/influxdata/influxdb/v2"
|
|
|
|
kithttp "github.com/influxdata/influxdb/v2/kit/transport/http"
|
|
|
|
influxlogger "github.com/influxdata/influxdb/v2/logger"
|
2019-01-07 04:12:41 +00:00
|
|
|
"go.uber.org/zap"
|
2019-04-26 20:06:41 +00:00
|
|
|
"go.uber.org/zap/zapcore"
|
2018-12-15 15:33:54 +00:00
|
|
|
)
|
|
|
|
|
2018-12-23 07:53:11 +00:00
|
|
|
// NewRouter returns a new router with a 404 handler, a 405 handler, and a panic handler.
|
2019-06-27 01:33:20 +00:00
|
|
|
func NewRouter(h platform.HTTPErrorHandler) *httprouter.Router {
|
|
|
|
b := baseHandler{HTTPErrorHandler: h}
|
2018-12-15 15:33:54 +00:00
|
|
|
router := httprouter.New()
|
2019-06-27 01:33:20 +00:00
|
|
|
router.NotFound = http.HandlerFunc(b.notFound)
|
|
|
|
router.MethodNotAllowed = http.HandlerFunc(b.methodNotAllowed)
|
|
|
|
router.PanicHandler = b.panic
|
2019-11-25 14:22:19 +00:00
|
|
|
router.AddMatchedRouteToContext = true
|
2018-12-15 15:33:54 +00:00
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
2020-06-15 22:18:13 +00:00
|
|
|
// NewBaseChiRouter returns a new chi router with a 404 handler, a 405 handler, and a panic handler.
|
2020-06-15 23:50:29 +00:00
|
|
|
func NewBaseChiRouter(api *kithttp.API) chi.Router {
|
2019-11-06 18:16:52 +00:00
|
|
|
router := chi.NewRouter()
|
2020-06-15 23:50:29 +00:00
|
|
|
router.NotFound(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
api.Err(w, r, &platform.Error{
|
|
|
|
Code: platform.ENotFound,
|
|
|
|
Msg: "path not found",
|
|
|
|
})
|
|
|
|
})
|
|
|
|
router.MethodNotAllowed(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
api.Err(w, r, &platform.Error{
|
|
|
|
Code: platform.EMethodNotAllowed,
|
|
|
|
Msg: fmt.Sprintf("allow: %s", w.Header().Get("Allow")),
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
2020-06-09 22:22:39 +00:00
|
|
|
router.Use(
|
2020-06-15 23:50:29 +00:00
|
|
|
panicMW(api),
|
2020-06-09 22:22:39 +00:00
|
|
|
kithttp.SkipOptions,
|
|
|
|
middleware.StripSlashes,
|
|
|
|
kithttp.SetCORS,
|
|
|
|
)
|
2019-11-06 18:16:52 +00:00
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
2019-06-27 01:33:20 +00:00
|
|
|
type baseHandler struct {
|
|
|
|
platform.HTTPErrorHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
// notFound represents a 404 handler that return a JSON response.
|
|
|
|
func (h baseHandler) notFound(w http.ResponseWriter, r *http.Request) {
|
2018-12-15 15:33:54 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
pe := &platform.Error{
|
|
|
|
Code: platform.ENotFound,
|
|
|
|
Msg: "path not found",
|
|
|
|
}
|
|
|
|
|
2019-06-27 01:33:20 +00:00
|
|
|
h.HandleHTTPError(ctx, pe, w)
|
2018-12-15 15:33:54 +00:00
|
|
|
}
|
2018-12-21 06:48:58 +00:00
|
|
|
|
2019-06-27 01:33:20 +00:00
|
|
|
// methodNotAllowed represents a 405 handler that return a JSON response.
|
|
|
|
func (h baseHandler) methodNotAllowed(w http.ResponseWriter, r *http.Request) {
|
2018-12-23 07:53:11 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
allow := w.Header().Get("Allow")
|
|
|
|
pe := &platform.Error{
|
|
|
|
Code: platform.EMethodNotAllowed,
|
|
|
|
Msg: fmt.Sprintf("allow: %s", allow),
|
|
|
|
}
|
|
|
|
|
2019-06-27 01:33:20 +00:00
|
|
|
h.HandleHTTPError(ctx, pe, w)
|
2018-12-23 07:53:11 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 01:33:20 +00:00
|
|
|
// panic handles panics recovered from http handlers.
|
2018-12-21 06:48:58 +00:00
|
|
|
// It returns a json response with http status code 500 and the recovered error message.
|
2019-06-27 01:33:20 +00:00
|
|
|
func (h baseHandler) panic(w http.ResponseWriter, r *http.Request, rcv interface{}) {
|
2018-12-21 06:48:58 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
pe := &platform.Error{
|
|
|
|
Code: platform.EInternal,
|
|
|
|
Msg: "a panic has occurred",
|
2020-06-09 23:24:14 +00:00
|
|
|
Err: fmt.Errorf("%s: %v", r.URL.String(), rcv),
|
2018-12-21 06:48:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 04:12:41 +00:00
|
|
|
l := getPanicLogger()
|
2019-04-26 20:06:41 +00:00
|
|
|
if entry := l.Check(zapcore.ErrorLevel, pe.Msg); entry != nil {
|
|
|
|
entry.Stack = string(debug.Stack())
|
|
|
|
entry.Write(zap.Error(pe.Err))
|
|
|
|
}
|
2019-01-07 04:12:41 +00:00
|
|
|
|
2019-06-27 01:33:20 +00:00
|
|
|
h.HandleHTTPError(ctx, pe, w)
|
2018-12-21 06:48:58 +00:00
|
|
|
}
|
2019-01-07 04:12:41 +00:00
|
|
|
|
2020-06-15 23:50:29 +00:00
|
|
|
func panicMW(api *kithttp.API) func(http.Handler) http.Handler {
|
2020-06-09 22:22:39 +00:00
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer func() {
|
2020-06-15 23:50:29 +00:00
|
|
|
panicErr := recover()
|
|
|
|
if panicErr == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pe := &platform.Error{
|
|
|
|
Code: platform.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))
|
2020-06-09 22:22:39 +00:00
|
|
|
}
|
2020-06-15 23:50:29 +00:00
|
|
|
|
|
|
|
api.Err(w, r, pe)
|
2020-06-09 22:22:39 +00:00
|
|
|
}()
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
return http.HandlerFunc(fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-07 04:12:41 +00:00
|
|
|
var panicLogger *zap.Logger
|
|
|
|
var panicLoggerOnce sync.Once
|
|
|
|
|
|
|
|
// getPanicLogger returns a logger for panicHandler.
|
|
|
|
func getPanicLogger() *zap.Logger {
|
|
|
|
panicLoggerOnce.Do(func() {
|
|
|
|
panicLogger = influxlogger.New(os.Stderr)
|
|
|
|
panicLogger = panicLogger.With(zap.String("handler", "panic"))
|
|
|
|
})
|
|
|
|
|
|
|
|
return panicLogger
|
|
|
|
}
|