forked from mirrors/homebox
refactor: http interfaces (#114)
* implement custom http handler interface * implement trace_id * normalize http method spacing for consistent logs * fix failing test * fix linter errors * cleanup old dead code * more route cleanup * cleanup some inconsistent errors * update and generate code * make taskfile more consistent * update task calls * run tidy * drop `@` tag for version * use relative paths * tidy * fix auto-setting variables * update build paths * add contributing guide * tidy
This commit is contained in:
parent
e2d93f8523
commit
6529549289
40 changed files with 984 additions and 808 deletions
70
backend/internal/web/mid/errors.go
Normal file
70
backend/internal/web/mid/errors.go
Normal file
|
@ -0,0 +1,70 @@
|
|||
package mid
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/hay-kot/homebox/backend/ent"
|
||||
"github.com/hay-kot/homebox/backend/internal/sys/validate"
|
||||
"github.com/hay-kot/homebox/backend/pkgs/server"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
func Errors(log zerolog.Logger) server.Middleware {
|
||||
return func(h server.Handler) server.Handler {
|
||||
return server.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
||||
err := h.ServeHTTP(w, r)
|
||||
|
||||
if err != nil {
|
||||
var resp server.ErrorResponse
|
||||
var code int
|
||||
|
||||
log.Err(err).
|
||||
Str("trace_id", server.GetTraceID(r.Context())).
|
||||
Msg("ERROR occurred")
|
||||
|
||||
switch {
|
||||
case validate.IsUnauthorizedError(err):
|
||||
code = http.StatusUnauthorized
|
||||
resp = server.ErrorResponse{
|
||||
Error: "unauthorized",
|
||||
}
|
||||
case validate.IsInvalidRouteKeyError(err):
|
||||
code = http.StatusBadRequest
|
||||
resp = server.ErrorResponse{
|
||||
Error: err.Error(),
|
||||
}
|
||||
case validate.IsFieldError(err):
|
||||
fieldErrors := err.(validate.FieldErrors)
|
||||
resp.Error = "Validation Error"
|
||||
resp.Fields = map[string]string{}
|
||||
|
||||
for _, fieldError := range fieldErrors {
|
||||
resp.Fields[fieldError.Field] = fieldError.Error
|
||||
}
|
||||
case validate.IsRequestError(err):
|
||||
requestError := err.(*validate.RequestError)
|
||||
resp.Error = requestError.Error()
|
||||
code = requestError.Status
|
||||
case ent.IsNotFound(err):
|
||||
resp.Error = "Not Found"
|
||||
code = http.StatusNotFound
|
||||
default:
|
||||
resp.Error = "Unknown Error"
|
||||
code = http.StatusInternalServerError
|
||||
|
||||
}
|
||||
|
||||
if err := server.Respond(w, code, resp); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// If Showdown error, return error
|
||||
if server.IsShutdownError(err) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
}
|
97
backend/internal/web/mid/logger.go
Normal file
97
backend/internal/web/mid/logger.go
Normal file
|
@ -0,0 +1,97 @@
|
|||
package mid
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/hay-kot/homebox/backend/pkgs/server"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
type statusRecorder struct {
|
||||
http.ResponseWriter
|
||||
Status int
|
||||
}
|
||||
|
||||
func (r *statusRecorder) WriteHeader(status int) {
|
||||
r.Status = status
|
||||
r.ResponseWriter.WriteHeader(status)
|
||||
}
|
||||
|
||||
func Logger(log zerolog.Logger) server.Middleware {
|
||||
return func(next server.Handler) server.Handler {
|
||||
return server.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
||||
traceId := server.GetTraceID(r.Context())
|
||||
|
||||
log.Info().
|
||||
Str("trace_id", traceId).
|
||||
Str("method", r.Method).
|
||||
Str("path", r.URL.Path).
|
||||
Str("remove_address", r.RemoteAddr).
|
||||
Msg("request started")
|
||||
|
||||
record := &statusRecorder{ResponseWriter: w, Status: http.StatusOK}
|
||||
|
||||
err := next.ServeHTTP(record, r)
|
||||
|
||||
log.Info().
|
||||
Str("trace_id", traceId).
|
||||
Str("method", r.Method).
|
||||
Str("url", r.URL.Path).
|
||||
Str("remote_address", r.RemoteAddr).
|
||||
Int("status_code", record.Status).
|
||||
Msg("request completed")
|
||||
|
||||
return err
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func SugarLogger(log zerolog.Logger) server.Middleware {
|
||||
orange := func(s string) string { return "\033[33m" + s + "\033[0m" }
|
||||
aqua := func(s string) string { return "\033[36m" + s + "\033[0m" }
|
||||
red := func(s string) string { return "\033[31m" + s + "\033[0m" }
|
||||
green := func(s string) string { return "\033[32m" + s + "\033[0m" }
|
||||
|
||||
fmtCode := func(code int) string {
|
||||
switch {
|
||||
case code >= 500:
|
||||
return red(fmt.Sprintf("%d", code))
|
||||
case code >= 400:
|
||||
return orange(fmt.Sprintf("%d", code))
|
||||
case code >= 300:
|
||||
return aqua(fmt.Sprintf("%d", code))
|
||||
default:
|
||||
return green(fmt.Sprintf("%d", code))
|
||||
}
|
||||
}
|
||||
bold := func(s string) string { return "\033[1m" + s + "\033[0m" }
|
||||
|
||||
atLeast6 := func(s string) string {
|
||||
for len(s) <= 6 {
|
||||
s += " "
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
return func(next server.Handler) server.Handler {
|
||||
return server.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
||||
|
||||
record := &statusRecorder{ResponseWriter: w, Status: http.StatusOK}
|
||||
|
||||
err := next.ServeHTTP(record, r) // Blocks until the next handler returns.
|
||||
|
||||
url := fmt.Sprintf("%s %s", r.RequestURI, r.Proto)
|
||||
|
||||
log.Info().
|
||||
Str("trace_id", server.GetTraceID(r.Context())).
|
||||
Msgf("%s %s %s",
|
||||
bold(fmtCode(record.Status)),
|
||||
bold(orange(atLeast6(r.Method))),
|
||||
aqua(url),
|
||||
)
|
||||
|
||||
return err
|
||||
})
|
||||
}
|
||||
}
|
33
backend/internal/web/mid/panic.go
Normal file
33
backend/internal/web/mid/panic.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package mid
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"runtime/debug"
|
||||
|
||||
"github.com/hay-kot/homebox/backend/pkgs/server"
|
||||
)
|
||||
|
||||
// Panic is a middleware that recovers from panics anywhere in the chain and wraps the error.
|
||||
// and returns it up the middleware chain.
|
||||
func Panic(develop bool) server.Middleware {
|
||||
return func(h server.Handler) server.Handler {
|
||||
return server.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (err error) {
|
||||
defer func() {
|
||||
if rec := recover(); rec != nil {
|
||||
trace := debug.Stack()
|
||||
|
||||
if develop {
|
||||
err = fmt.Errorf("PANIC [%v]", rec)
|
||||
fmt.Printf("%s", string(trace))
|
||||
} else {
|
||||
err = fmt.Errorf("PANIC [%v] TRACE[%s]", rec, string(trace))
|
||||
}
|
||||
|
||||
}
|
||||
}()
|
||||
|
||||
return h.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue