Use chi render, improvements
This commit is contained in:
parent
cecbeb78fe
commit
759c274950
2 changed files with 68 additions and 43 deletions
|
@ -1,13 +1,13 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"dynatron.me/x/stillbox/pkg/talkgroups"
|
"dynatron.me/x/stillbox/pkg/talkgroups"
|
||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
|
"github.com/go-chi/render"
|
||||||
"github.com/go-viper/mapstructure/v2"
|
"github.com/go-viper/mapstructure/v2"
|
||||||
"github.com/jackc/pgx/v5"
|
"github.com/jackc/pgx/v5"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
@ -29,57 +29,81 @@ func New() API {
|
||||||
func (a *api) Subrouter() http.Handler {
|
func (a *api) Subrouter() http.Handler {
|
||||||
r := chi.NewMux()
|
r := chi.NewMux()
|
||||||
|
|
||||||
r.Mount("/talkgroup", new(talkgroupAPI).routes())
|
r.Mount("/talkgroup", new(talkgroupAPI).Subrouter())
|
||||||
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
type errResponse struct {
|
type errResponse struct {
|
||||||
text string
|
Err error `json:"-"`
|
||||||
code int
|
Code int `json:"-"`
|
||||||
|
Error string `json:"error"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var statusMapping = map[error]errResponse{
|
func (e *errResponse) Render(w http.ResponseWriter, r *http.Request) error {
|
||||||
talkgroups.ErrNotFound: {talkgroups.ErrNotFound.Error(), http.StatusNotFound},
|
switch e.Code {
|
||||||
pgx.ErrNoRows: {"no such record", http.StatusNotFound},
|
case http.StatusNotFound:
|
||||||
|
case http.StatusBadRequest:
|
||||||
|
default:
|
||||||
|
log.Error().Str("path", r.URL.Path).Err(e.Err).Int("code", e.Code).Str("msg", e.Error).Msg("request failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
render.Status(r, e.Code)
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func httpCode(err error) (string, int) {
|
func badRequest(err error) render.Renderer {
|
||||||
|
return &errResponse{
|
||||||
|
Err: err,
|
||||||
|
Code: http.StatusBadRequest,
|
||||||
|
Error: "Bad request",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func recordNotFound(err error) render.Renderer {
|
||||||
|
return &errResponse{
|
||||||
|
Err: err,
|
||||||
|
Code: http.StatusNotFound,
|
||||||
|
Error: "Record not found",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func internalError(err error) render.Renderer {
|
||||||
|
return &errResponse{
|
||||||
|
Err: err,
|
||||||
|
Code: http.StatusNotFound,
|
||||||
|
Error: "Internal server error",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type errResponder func(error) render.Renderer
|
||||||
|
|
||||||
|
var statusMapping = map[error]errResponder{
|
||||||
|
talkgroups.ErrNotFound: recordNotFound,
|
||||||
|
pgx.ErrNoRows: recordNotFound,
|
||||||
|
}
|
||||||
|
|
||||||
|
func autoError(err error) render.Renderer {
|
||||||
c, ok := statusMapping[err]
|
c, ok := statusMapping[err]
|
||||||
if ok {
|
if ok {
|
||||||
return c.text, c.code
|
c(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for e, c := range statusMapping { // check if err wraps an error we know about
|
for e, c := range statusMapping { // check if err wraps an error we know about
|
||||||
if errors.Is(err, e) {
|
if errors.Is(err, e) {
|
||||||
return c.text, c.code
|
return c(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return err.Error(), http.StatusInternalServerError
|
return internalError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeResponse(w http.ResponseWriter, r *http.Request, data interface{}, err error) {
|
func wErr(w http.ResponseWriter, r *http.Request, v render.Renderer) {
|
||||||
|
err := render.Render(w, r, v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Str("path", r.URL.Path).Err(err).Msg("request failed")
|
log.Error().Err(err).Msg("wErr render error")
|
||||||
text, code := httpCode(err)
|
|
||||||
http.Error(w, text, code)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
enc := json.NewEncoder(w)
|
|
||||||
err = enc.Encode(data)
|
|
||||||
if err != nil {
|
|
||||||
log.Error().Str("path", r.URL.Path).Err(err).Msg("response marshal failed")
|
|
||||||
text, code := httpCode(err)
|
|
||||||
http.Error(w, text, code)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func reqErr(w http.ResponseWriter, err error, code int) {
|
|
||||||
http.Error(w, err.Error(), code)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func decodeParams(d interface{}, r *http.Request) error {
|
func decodeParams(d interface{}, r *http.Request) error {
|
||||||
|
@ -103,6 +127,6 @@ func decodeParams(d interface{}, r *http.Request) error {
|
||||||
return dec.Decode(m)
|
return dec.Decode(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
func badReq(w http.ResponseWriter, err error) {
|
func respond(w http.ResponseWriter, r *http.Request, v interface{}) {
|
||||||
reqErr(w, err, http.StatusBadRequest)
|
render.DefaultResponder(w, r, v)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"dynatron.me/x/stillbox/internal/forms"
|
"dynatron.me/x/stillbox/internal/forms"
|
||||||
|
@ -14,7 +13,7 @@ import (
|
||||||
type talkgroupAPI struct {
|
type talkgroupAPI struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tga *talkgroupAPI) routes() http.Handler {
|
func (tga *talkgroupAPI) Subrouter() http.Handler {
|
||||||
r := chi.NewMux()
|
r := chi.NewMux()
|
||||||
|
|
||||||
r.Get("/{system:\\d+}/{id:\\d+}", tga.talkgroup)
|
r.Get("/{system:\\d+}/{id:\\d+}", tga.talkgroup)
|
||||||
|
@ -57,7 +56,7 @@ func (tga *talkgroupAPI) talkgroup(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
err := decodeParams(&p, r)
|
err := decodeParams(&p, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
badReq(w, err)
|
wErr(w, r, badRequest(err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,14 +70,19 @@ func (tga *talkgroupAPI) talkgroup(w http.ResponseWriter, r *http.Request) {
|
||||||
res, err = tgs.TGs(ctx, nil)
|
res, err = tgs.TGs(ctx, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
writeResponse(w, r, res, err)
|
if err != nil {
|
||||||
|
wErr(w, r, autoError(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
respond(w, r, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tga *talkgroupAPI) putTalkgroup(w http.ResponseWriter, r *http.Request) {
|
func (tga *talkgroupAPI) putTalkgroup(w http.ResponseWriter, r *http.Request) {
|
||||||
var id tgParams
|
var id tgParams
|
||||||
err := decodeParams(&id, r)
|
err := decodeParams(&id, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
badReq(w, err)
|
wErr(w, r, badRequest(err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,19 +93,16 @@ func (tga *talkgroupAPI) putTalkgroup(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
err = forms.Unmarshal(r, &input, forms.WithTag("json"), forms.WithAcceptBlank(), forms.WithOmitEmpty())
|
err = forms.Unmarshal(r, &input, forms.WithTag("json"), forms.WithAcceptBlank(), forms.WithOmitEmpty())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeResponse(w, r, nil, err)
|
wErr(w, r, badRequest(err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
input.ID = id.ToID().Pack()
|
input.ID = id.ToID().Pack()
|
||||||
|
|
||||||
record, err := tgs.UpdateTG(ctx, input)
|
record, err := tgs.UpdateTG(ctx, input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeResponse(w, r, nil, err)
|
wErr(w, r, autoError(err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.NewEncoder(w).Encode(record)
|
respond(w, r, record)
|
||||||
if err != nil {
|
|
||||||
writeResponse(w, r, nil, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue