mirror of
https://github.com/hay-kot/homebox.git
synced 2025-08-05 09:10:26 +00:00
refactor context adapters to use requests directly
This commit is contained in:
parent
7440b9ffca
commit
d6823e4e61
5 changed files with 37 additions and 36 deletions
|
@ -16,7 +16,7 @@ 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
|
||||
// }
|
||||
|
@ -24,12 +24,12 @@ import (
|
|||
// r.Post("/foo", adapters.Action(fn, http.StatusCreated))
|
||||
func Action[T any, Y any](f AdapterFunc[T, Y], ok int) server.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
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ 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
|
||||
// }
|
||||
|
@ -54,17 +54,17 @@ func Action[T any, Y any](f AdapterFunc[T, Y], ok int) server.HandlerFunc {
|
|||
// 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 {
|
||||
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
|
||||
}
|
||||
|
|
|
@ -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,15 +1,14 @@
|
|||
package adapters
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/hay-kot/homebox/backend/pkgs/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
|
||||
// The command adapters are used to handle commands that do not accept a body
|
||||
|
@ -17,15 +16,15 @@ type CommandIDFunc[T any] func(context.Context, uuid.UUID) (T, error)
|
|||
//
|
||||
// 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))
|
||||
// r.Get("/foo", adapters.Command(fn, http.NoContent))
|
||||
func Command[T any](f CommandFunc[T], ok int) server.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) error {
|
||||
res, err := f(r.Context())
|
||||
res, err := f(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -39,7 +38,7 @@ 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
|
||||
// }
|
||||
|
@ -47,12 +46,12 @@ func Command[T any](f CommandFunc[T], ok int) server.HandlerFunc {
|
|||
// r.Get("/foo/{id}", adapters.CommandID("id", fn, http.NoContent))
|
||||
func CommandID[T any](param string, f CommandIDFunc[T], ok int) server.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
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@ package adapters
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/google/uuid"
|
||||
"github.com/gorilla/schema"
|
||||
|
@ -12,38 +14,38 @@ import (
|
|||
|
||||
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)
|
||||
|
|
|
@ -14,7 +14,7 @@ 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
|
||||
// }
|
||||
|
@ -22,12 +22,12 @@ import (
|
|||
// r.Get("/foo", adapters.Query(fn, http.StatusOK))
|
||||
func Query[T any, Y any](f AdapterFunc[T, Y], ok int) server.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
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ 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
|
||||
// }
|
||||
|
@ -52,17 +52,17 @@ func Query[T any, Y any](f AdapterFunc[T, Y], ok int) server.HandlerFunc {
|
|||
// 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 {
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue