neko/internal/http/router.go

128 lines
3.0 KiB
Go
Raw Normal View History

2021-09-17 10:24:33 +12:00
package http
import (
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
2021-09-18 05:27:21 +12:00
"github.com/rs/zerolog"
2022-03-20 23:43:00 +13:00
"gitlab.com/demodesk/neko/server/pkg/auth"
"gitlab.com/demodesk/neko/server/pkg/types"
"gitlab.com/demodesk/neko/server/pkg/utils"
2021-09-17 10:24:33 +12:00
)
2021-09-18 05:23:04 +12:00
type router struct {
2021-09-17 10:24:33 +12:00
chi chi.Router
}
2021-09-18 05:27:21 +12:00
func newRouter(logger zerolog.Logger) *router {
2021-09-18 05:23:04 +12:00
r := chi.NewRouter()
r.Use(middleware.RequestID) // Create a request ID for each request
2021-09-18 05:27:21 +12:00
r.Use(middleware.RequestLogger(&logFormatter{logger}))
2021-09-18 05:23:04 +12:00
r.Use(middleware.Recoverer) // Recover from panics without crashing server
return &router{r}
2021-09-17 10:24:33 +12:00
}
2021-09-18 05:23:04 +12:00
func (r *router) Group(fn func(types.Router)) {
2021-09-17 10:24:33 +12:00
r.chi.Group(func(c chi.Router) {
2021-09-18 05:23:04 +12:00
fn(&router{c})
2021-09-17 10:24:33 +12:00
})
}
2021-09-18 05:23:04 +12:00
func (r *router) Route(pattern string, fn func(types.Router)) {
2021-09-17 10:24:33 +12:00
r.chi.Route(pattern, func(c chi.Router) {
2021-09-18 05:23:04 +12:00
fn(&router{c})
2021-09-17 10:24:33 +12:00
})
}
2021-09-18 05:23:04 +12:00
func (r *router) Get(pattern string, fn types.RouterHandler) {
2021-09-17 10:24:33 +12:00
r.chi.Get(pattern, routeHandler(fn))
}
2021-09-20 10:19:10 +12:00
2021-09-18 05:23:04 +12:00
func (r *router) Post(pattern string, fn types.RouterHandler) {
2021-09-17 10:24:33 +12:00
r.chi.Post(pattern, routeHandler(fn))
}
2021-09-20 10:19:10 +12:00
2021-09-18 05:23:04 +12:00
func (r *router) Put(pattern string, fn types.RouterHandler) {
2021-09-17 10:24:33 +12:00
r.chi.Put(pattern, routeHandler(fn))
}
2021-09-20 10:19:10 +12:00
2021-09-18 05:23:04 +12:00
func (r *router) Delete(pattern string, fn types.RouterHandler) {
2021-09-17 10:24:33 +12:00
r.chi.Delete(pattern, routeHandler(fn))
}
2021-09-18 05:23:04 +12:00
func (r *router) With(fn types.MiddlewareHandler) types.Router {
2021-09-17 10:24:33 +12:00
c := r.chi.With(middlewareHandler(fn))
2021-09-18 05:23:04 +12:00
return &router{c}
2021-09-17 10:24:33 +12:00
}
2021-09-18 05:23:04 +12:00
func (r *router) WithBypass(fn func(next http.Handler) http.Handler) types.Router {
2021-09-17 10:24:33 +12:00
c := r.chi.With(fn)
2021-09-18 05:23:04 +12:00
return &router{c}
2021-09-17 10:24:33 +12:00
}
2021-09-18 05:23:04 +12:00
func (r *router) Use(fn types.MiddlewareHandler) {
2021-09-17 10:24:33 +12:00
r.chi.Use(middlewareHandler(fn))
}
2021-09-20 10:19:10 +12:00
2021-09-18 05:23:04 +12:00
func (r *router) UseBypass(fn func(next http.Handler) http.Handler) {
2021-09-17 10:24:33 +12:00
r.chi.Use(fn)
}
2021-09-18 05:23:04 +12:00
func (r *router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
2021-09-17 10:24:33 +12:00
r.chi.ServeHTTP(w, req)
}
func errorHandler(err error, w http.ResponseWriter, r *http.Request) {
httpErr, ok := err.(*utils.HTTPError)
if !ok {
httpErr = utils.HttpInternalServerError().WithInternalErr(err)
}
utils.HttpJsonResponse(w, httpErr.Code, httpErr)
}
func routeHandler(fn types.RouterHandler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2021-09-18 05:22:49 +12:00
// get custom log entry pointer from context
logEntry, _ := r.Context().Value(middleware.LogEntryCtxKey).(*logEntry)
2021-09-17 10:24:33 +12:00
if err := fn(w, r); err != nil {
2021-09-18 06:20:10 +12:00
logEntry.Error(err)
2021-09-17 10:24:33 +12:00
errorHandler(err, w, r)
}
2021-09-18 05:22:49 +12:00
// set session if exits
if session, ok := auth.GetSession(r); ok {
logEntry.SetSession(session)
}
2021-09-17 10:24:33 +12:00
}
}
func middlewareHandler(fn types.MiddlewareHandler) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2021-09-18 05:22:49 +12:00
// get custom log entry pointer from context
logEntry, _ := r.Context().Value(middleware.LogEntryCtxKey).(*logEntry)
2021-09-17 10:24:33 +12:00
ctx, err := fn(w, r)
if err != nil {
2021-09-18 06:20:10 +12:00
logEntry.Error(err)
2021-09-17 10:24:33 +12:00
errorHandler(err, w, r)
2021-09-18 05:22:49 +12:00
// set session if exits
if session, ok := auth.GetSession(r); ok {
logEntry.SetSession(session)
}
2021-09-17 10:24:33 +12:00
return
}
if ctx != nil {
r = r.WithContext(ctx)
}
next.ServeHTTP(w, r)
})
}
}