mirror of
https://github.com/hay-kot/homebox.git
synced 2025-07-20 09:30:29 +00:00
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
103
backend/pkgs/server/mux.go
Normal file
103
backend/pkgs/server/mux.go
Normal file
|
@ -0,0 +1,103 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type vkey int
|
||||
|
||||
const (
|
||||
// Key is the key for the server in the request context.
|
||||
key vkey = 1
|
||||
)
|
||||
|
||||
type Values struct {
|
||||
TraceID string
|
||||
}
|
||||
|
||||
func GetTraceID(ctx context.Context) string {
|
||||
v, ok := ctx.Value(key).(Values)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
return v.TraceID
|
||||
}
|
||||
|
||||
func (s *Server) toHttpHandler(handler Handler, mw ...Middleware) http.HandlerFunc {
|
||||
handler = wrapMiddleware(mw, handler)
|
||||
|
||||
handler = wrapMiddleware(s.mw, handler)
|
||||
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
// Add the trace ID to the context
|
||||
ctx = context.WithValue(ctx, key, Values{
|
||||
TraceID: uuid.NewString(),
|
||||
})
|
||||
|
||||
err := handler.ServeHTTP(w, r.WithContext(ctx))
|
||||
|
||||
if err != nil {
|
||||
if IsShutdownError(err) {
|
||||
_ = s.Shutdown("SIGTERM")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) handle(method, pattern string, handler Handler, mw ...Middleware) {
|
||||
h := s.toHttpHandler(handler, mw...)
|
||||
|
||||
switch method {
|
||||
case http.MethodGet:
|
||||
s.mux.Get(pattern, h)
|
||||
case http.MethodPost:
|
||||
s.mux.Post(pattern, h)
|
||||
case http.MethodPut:
|
||||
s.mux.Put(pattern, h)
|
||||
case http.MethodDelete:
|
||||
s.mux.Delete(pattern, h)
|
||||
case http.MethodPatch:
|
||||
s.mux.Patch(pattern, h)
|
||||
case http.MethodHead:
|
||||
s.mux.Head(pattern, h)
|
||||
case http.MethodOptions:
|
||||
s.mux.Options(pattern, h)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) Get(pattern string, handler Handler, mw ...Middleware) {
|
||||
s.handle(http.MethodGet, pattern, handler, mw...)
|
||||
}
|
||||
|
||||
func (s *Server) Post(pattern string, handler Handler, mw ...Middleware) {
|
||||
s.handle(http.MethodPost, pattern, handler, mw...)
|
||||
}
|
||||
|
||||
func (s *Server) Put(pattern string, handler Handler, mw ...Middleware) {
|
||||
s.handle(http.MethodPut, pattern, handler, mw...)
|
||||
}
|
||||
|
||||
func (s *Server) Delete(pattern string, handler Handler, mw ...Middleware) {
|
||||
s.handle(http.MethodDelete, pattern, handler, mw...)
|
||||
}
|
||||
|
||||
func (s *Server) Patch(pattern string, handler Handler, mw ...Middleware) {
|
||||
s.handle(http.MethodPatch, pattern, handler, mw...)
|
||||
}
|
||||
|
||||
func (s *Server) Head(pattern string, handler Handler, mw ...Middleware) {
|
||||
s.handle(http.MethodHead, pattern, handler, mw...)
|
||||
}
|
||||
|
||||
func (s *Server) Options(pattern string, handler Handler, mw ...Middleware) {
|
||||
s.handle(http.MethodOptions, pattern, handler, mw...)
|
||||
}
|
||||
|
||||
func (s *Server) NotFound(handler Handler) {
|
||||
s.mux.NotFound(s.toHttpHandler(handler))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue