forked from mirrors/homebox
chore: refactor api endpoints (#339)
* move typegen code * update taskfile to fix code-gen caches and use 'dir' attribute * enable dumping stack traces for errors * log request start and stop * set zerolog stack handler * fix routes function * refactor context adapters to use requests directly * change some method signatures to support GID * start requiring validation tags * first pass on updating handlers to use adapters * add errs package * code gen * tidy * rework API to use external server package
This commit is contained in:
parent
184b494fc3
commit
db80f8a159
56 changed files with 806 additions and 1947 deletions
|
@ -3,7 +3,8 @@ package adapters
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/hay-kot/homebox/backend/pkgs/server"
|
||||
"github.com/hay-kot/safeserve/errchain"
|
||||
"github.com/hay-kot/safeserve/server"
|
||||
)
|
||||
|
||||
// Action is a function that adapts a function to the server.Handler interface.
|
||||
|
@ -16,25 +17,25 @@ import (
|
|||
// Foo string `json:"foo"`
|
||||
// }
|
||||
//
|
||||
// fn := func(ctx context.Context, b Body) (any, error) {
|
||||
// fn := func(r *http.Request, b Body) (any, error) {
|
||||
// // do something with b
|
||||
// return nil, nil
|
||||
// }
|
||||
//
|
||||
// r.Post("/foo", adapters.Action(fn, http.StatusCreated))
|
||||
func Action[T any, Y any](f AdapterFunc[T, Y], ok int) server.HandlerFunc {
|
||||
func Action[T any, Y any](f AdapterFunc[T, Y], ok int) errchain.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) error {
|
||||
v, err := decode[T](r)
|
||||
v, err := DecodeBody[T](r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
res, err := f(r.Context(), v)
|
||||
res, err := f(r, v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return server.Respond(w, ok, res)
|
||||
return server.JSON(w, ok, res)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -46,29 +47,29 @@ func Action[T any, Y any](f AdapterFunc[T, Y], ok int) server.HandlerFunc {
|
|||
// Foo string `json:"foo"`
|
||||
// }
|
||||
//
|
||||
// fn := func(ctx context.Context, ID uuid.UUID, b Body) (any, error) {
|
||||
// fn := func(r *http.Request, ID uuid.UUID, b Body) (any, error) {
|
||||
// // do something with ID and b
|
||||
// return nil, nil
|
||||
// }
|
||||
//
|
||||
// r.Post("/foo/{id}", adapters.ActionID(fn, http.StatusCreated))
|
||||
func ActionID[T any, Y any](param string, f IDFunc[T, Y], ok int) server.HandlerFunc {
|
||||
func ActionID[T any, Y any](param string, f IDFunc[T, Y], ok int) errchain.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) error {
|
||||
ID, err := routeUUID(r, param)
|
||||
ID, err := RouteUUID(r, param)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
v, err := decode[T](r)
|
||||
v, err := DecodeBody[T](r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
res, err := f(r.Context(), ID, v)
|
||||
res, err := f(r, ID, v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return server.Respond(w, ok, res)
|
||||
return server.JSON(w, ok, res)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
package adapters
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type AdapterFunc[T any, Y any] func(context.Context, T) (Y, error)
|
||||
type IDFunc[T any, Y any] func(context.Context, uuid.UUID, T) (Y, error)
|
||||
type AdapterFunc[T any, Y any] func(*http.Request, T) (Y, error)
|
||||
type IDFunc[T any, Y any] func(*http.Request, uuid.UUID, T) (Y, error)
|
||||
|
|
|
@ -1,36 +1,36 @@
|
|||
package adapters
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/hay-kot/homebox/backend/pkgs/server"
|
||||
"github.com/hay-kot/safeserve/errchain"
|
||||
"github.com/hay-kot/safeserve/server"
|
||||
)
|
||||
|
||||
type CommandFunc[T any] func(context.Context) (T, error)
|
||||
type CommandIDFunc[T any] func(context.Context, uuid.UUID) (T, error)
|
||||
type CommandFunc[T any] func(*http.Request) (T, error)
|
||||
type CommandIDFunc[T any] func(*http.Request, uuid.UUID) (T, error)
|
||||
|
||||
// Command is an HandlerAdapter that returns a server.HandlerFunc that
|
||||
// Command is an HandlerAdapter that returns a errchain.HandlerFunc that
|
||||
// The command adapters are used to handle commands that do not accept a body
|
||||
// or a query. You can think of them as a way to handle RPC style Rest Endpoints.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// fn := func(ctx context.Context) (interface{}, error) {
|
||||
// // do something
|
||||
// return nil, nil
|
||||
// }
|
||||
// fn := func(r *http.Request) (interface{}, error) {
|
||||
// // do something
|
||||
// return nil, nil
|
||||
// }
|
||||
//
|
||||
// r.Get("/foo", adapters.Command(fn, http.NoContent))
|
||||
func Command[T any](f CommandFunc[T], ok int) server.HandlerFunc {
|
||||
// r.Get("/foo", adapters.Command(fn, http.NoContent))
|
||||
func Command[T any](f CommandFunc[T], ok int) errchain.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) error {
|
||||
res, err := f(r.Context())
|
||||
res, err := f(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return server.Respond(w, ok, res)
|
||||
return server.JSON(w, ok, res)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -39,24 +39,24 @@ func Command[T any](f CommandFunc[T], ok int) server.HandlerFunc {
|
|||
//
|
||||
// Example:
|
||||
//
|
||||
// fn := func(ctx context.Context, id uuid.UUID) (interface{}, error) {
|
||||
// fn := func(r *http.Request, id uuid.UUID) (interface{}, error) {
|
||||
// // do something
|
||||
// return nil, nil
|
||||
// }
|
||||
//
|
||||
// r.Get("/foo/{id}", adapters.CommandID("id", fn, http.NoContent))
|
||||
func CommandID[T any](param string, f CommandIDFunc[T], ok int) server.HandlerFunc {
|
||||
func CommandID[T any](param string, f CommandIDFunc[T], ok int) errchain.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) error {
|
||||
ID, err := routeUUID(r, param)
|
||||
ID, err := RouteUUID(r, param)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
res, err := f(r.Context(), ID)
|
||||
res, err := f(r, ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return server.Respond(w, ok, res)
|
||||
return server.JSON(w, ok, res)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,47 +3,49 @@ package adapters
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/google/uuid"
|
||||
"github.com/gorilla/schema"
|
||||
"github.com/hay-kot/homebox/backend/internal/sys/validate"
|
||||
"github.com/hay-kot/homebox/backend/pkgs/server"
|
||||
"github.com/hay-kot/safeserve/server"
|
||||
)
|
||||
|
||||
var queryDecoder = schema.NewDecoder()
|
||||
|
||||
func decodeQuery[T any](r *http.Request) (T, error) {
|
||||
func DecodeQuery[T any](r *http.Request) (T, error) {
|
||||
var v T
|
||||
err := queryDecoder.Decode(&v, r.URL.Query())
|
||||
if err != nil {
|
||||
return v, err
|
||||
return v, errors.Wrap(err, "decoding error")
|
||||
}
|
||||
|
||||
err = validate.Check(v)
|
||||
if err != nil {
|
||||
return v, err
|
||||
return v, errors.Wrap(err, "validation error")
|
||||
}
|
||||
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func decode[T any](r *http.Request) (T, error) {
|
||||
func DecodeBody[T any](r *http.Request) (T, error) {
|
||||
var v T
|
||||
|
||||
err := server.Decode(r, &v)
|
||||
if err != nil {
|
||||
return v, err
|
||||
return v, errors.Wrap(err, "body decoding error")
|
||||
}
|
||||
|
||||
err = validate.Check(v)
|
||||
if err != nil {
|
||||
return v, err
|
||||
return v, errors.Wrap(err, "validation error")
|
||||
}
|
||||
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func routeUUID(r *http.Request, key string) (uuid.UUID, error) {
|
||||
func RouteUUID(r *http.Request, key string) (uuid.UUID, error) {
|
||||
ID, err := uuid.Parse(chi.URLParam(r, key))
|
||||
if err != nil {
|
||||
return uuid.Nil, validate.NewRouteKeyError(key)
|
||||
|
|
|
@ -3,7 +3,8 @@ package adapters
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/hay-kot/homebox/backend/pkgs/server"
|
||||
"github.com/hay-kot/safeserve/errchain"
|
||||
"github.com/hay-kot/safeserve/server"
|
||||
)
|
||||
|
||||
// Query is a server.Handler that decodes a query from the request and calls the provided function.
|
||||
|
@ -14,25 +15,25 @@ import (
|
|||
// Foo string `schema:"foo"`
|
||||
// }
|
||||
//
|
||||
// fn := func(ctx context.Context, q Query) (any, error) {
|
||||
// fn := func(r *http.Request, q Query) (any, error) {
|
||||
// // do something with q
|
||||
// return nil, nil
|
||||
// }
|
||||
//
|
||||
// r.Get("/foo", adapters.Query(fn, http.StatusOK))
|
||||
func Query[T any, Y any](f AdapterFunc[T, Y], ok int) server.HandlerFunc {
|
||||
func Query[T any, Y any](f AdapterFunc[T, Y], ok int) errchain.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) error {
|
||||
q, err := decodeQuery[T](r)
|
||||
q, err := DecodeQuery[T](r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
res, err := f(r.Context(), q)
|
||||
res, err := f(r, q)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return server.Respond(w, ok, res)
|
||||
return server.JSON(w, ok, res)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -44,29 +45,29 @@ func Query[T any, Y any](f AdapterFunc[T, Y], ok int) server.HandlerFunc {
|
|||
// Foo string `schema:"foo"`
|
||||
// }
|
||||
//
|
||||
// fn := func(ctx context.Context, ID uuid.UUID, q Query) (any, error) {
|
||||
// fn := func(r *http.Request, ID uuid.UUID, q Query) (any, error) {
|
||||
// // do something with ID and q
|
||||
// return nil, nil
|
||||
// }
|
||||
//
|
||||
// r.Get("/foo/{id}", adapters.QueryID(fn, http.StatusOK))
|
||||
func QueryID[T any, Y any](param string, f IDFunc[T, Y], ok int) server.HandlerFunc {
|
||||
func QueryID[T any, Y any](param string, f IDFunc[T, Y], ok int) errchain.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) error {
|
||||
ID, err := routeUUID(r, param)
|
||||
ID, err := RouteUUID(r, param)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
q, err := decodeQuery[T](r)
|
||||
q, err := DecodeQuery[T](r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
res, err := f(r.Context(), ID, q)
|
||||
res, err := f(r, ID, q)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return server.Respond(w, ok, res)
|
||||
return server.JSON(w, ok, res)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue