influxdb/http/router.go

55 lines
1.4 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"
platform "github.com/influxdata/influxdb"
2018-12-15 15:33:54 +00:00
"github.com/julienschmidt/httprouter"
)
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),
}
EncodeError(ctx, pe, w)
}