influxdb/http/router.go

80 lines
2.0 KiB
Go
Raw Normal View History

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
platform "github.com/influxdata/influxdb"
2019-01-07 04:12:41 +00:00
influxlogger "github.com/influxdata/influxdb/logger"
2018-12-15 15:33:54 +00:00
"github.com/julienschmidt/httprouter"
2019-01-07 04:12:41 +00:00
"go.uber.org/zap"
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.
2018-12-15 15:33:54 +00:00
func NewRouter() *httprouter.Router {
router := httprouter.New()
router.NotFound = http.HandlerFunc(notFoundHandler)
2018-12-23 07:53:11 +00:00
router.MethodNotAllowed = http.HandlerFunc(methodNotAllowedHandler)
2018-12-21 06:48:58 +00:00
router.PanicHandler = panicHandler
2018-12-15 15:33:54 +00:00
return router
}
2018-12-21 06:48:58 +00:00
// notFoundHandler represents a 404 handler that return a JSON response.
2018-12-15 15:33:54 +00:00
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
pe := &platform.Error{
Code: platform.ENotFound,
Msg: "path not found",
}
EncodeError(ctx, pe, w)
}
2018-12-21 06:48:58 +00:00
2018-12-23 07:53:11 +00:00
// methodNotAllowedHandler represents a 405 handler that return a JSON response.
func methodNotAllowedHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
allow := w.Header().Get("Allow")
pe := &platform.Error{
Code: platform.EMethodNotAllowed,
Msg: fmt.Sprintf("allow: %s", allow),
}
EncodeError(ctx, pe, w)
}
2018-12-21 06:48:58 +00:00
// panicHandler handles panics recovered from http handlers.
// It returns a json response with http status code 500 and the recovered error message.
func panicHandler(w http.ResponseWriter, r *http.Request, rcv interface{}) {
ctx := r.Context()
pe := &platform.Error{
Code: platform.EInternal,
Msg: "a panic has occurred",
Err: fmt.Errorf("%v", rcv),
}
2019-01-07 04:12:41 +00:00
l := getPanicLogger()
l.Error(
pe.Msg,
zap.String("err", pe.Err.Error()),
zap.String("stack", fmt.Sprintf("%s", debug.Stack())),
)
2018-12-21 06:48:58 +00:00
EncodeError(ctx, pe, w)
}
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
}