neko/internal/http/router.go

122 lines
3.0 KiB
Go
Raw Normal View History

2021-09-17 10:24:33 +12:00
package http
import (
2021-09-18 05:22:49 +12:00
"demodesk/neko/internal/http/auth"
2021-09-17 10:24:33 +12:00
"demodesk/neko/internal/types"
"demodesk/neko/internal/utils"
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
)
type RouterCtx struct {
chi chi.Router
}
func newRouter() *RouterCtx {
router := chi.NewRouter()
router.Use(middleware.RequestID) // Create a request ID for each request
2021-09-18 05:22:49 +12:00
router.Use(middleware.RequestLogger(&logFormatter{}))
router.Use(middleware.Recoverer) // Recover from panics without crashing server
2021-09-17 10:24:33 +12:00
return &RouterCtx{router}
}
func (r *RouterCtx) Group(fn func(types.Router)) {
r.chi.Group(func(c chi.Router) {
fn(&RouterCtx{c})
})
}
func (r *RouterCtx) Route(pattern string, fn func(types.Router)) {
r.chi.Route(pattern, func(c chi.Router) {
fn(&RouterCtx{c})
})
}
func (r *RouterCtx) Get(pattern string, fn types.RouterHandler) {
r.chi.Get(pattern, routeHandler(fn))
}
func (r *RouterCtx) Post(pattern string, fn types.RouterHandler) {
r.chi.Post(pattern, routeHandler(fn))
}
func (r *RouterCtx) Put(pattern string, fn types.RouterHandler) {
r.chi.Put(pattern, routeHandler(fn))
}
func (r *RouterCtx) Delete(pattern string, fn types.RouterHandler) {
r.chi.Delete(pattern, routeHandler(fn))
}
func (r *RouterCtx) With(fn types.MiddlewareHandler) types.Router {
c := r.chi.With(middlewareHandler(fn))
return &RouterCtx{c}
}
func (r *RouterCtx) WithBypass(fn func(next http.Handler) http.Handler) types.Router {
c := r.chi.With(fn)
return &RouterCtx{c}
}
func (r *RouterCtx) Use(fn types.MiddlewareHandler) {
r.chi.Use(middlewareHandler(fn))
}
func (r *RouterCtx) UseBypass(fn func(next http.Handler) http.Handler) {
r.chi.Use(fn)
}
func (r *RouterCtx) ServeHTTP(w http.ResponseWriter, req *http.Request) {
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 {
logEntry.SetError(err)
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 {
logEntry.SetError(err)
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)
})
}
}