mirror of
https://github.com/hay-kot/homebox.git
synced 2024-11-22 16:45:43 +00:00
db80f8a159
* 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
62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
package adapters
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/hay-kot/safeserve/errchain"
|
|
"github.com/hay-kot/safeserve/server"
|
|
)
|
|
|
|
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 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(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) errchain.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
|
res, err := f(r)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return server.JSON(w, ok, res)
|
|
}
|
|
}
|
|
|
|
// CommandID is the same as the Command adapter but it accepts a UUID as a parameter
|
|
// in the URL. The parameter name is passed as the first argument.
|
|
//
|
|
// Example:
|
|
//
|
|
// 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) errchain.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
|
ID, err := RouteUUID(r, param)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
res, err := f(r, ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return server.JSON(w, ok, res)
|
|
}
|
|
}
|