cleanups and code fixes incl templ
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2026-03-20 13:21:15 +11:00
parent 4fbb2582e3
commit 9a561f3b07
24 changed files with 425 additions and 141 deletions

View File

@@ -2,6 +2,9 @@ package handler
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"time"
)
@@ -10,6 +13,7 @@ const (
defaultRequestTimeout = 2 * time.Minute
reportRequestTimeout = 10 * time.Minute
longRunningRequestTimeout = 2 * time.Hour
defaultJSONBodyLimitBytes = 1 << 20 // 1 MiB
)
func withRequestTimeout(r *http.Request, timeout time.Duration) (context.Context, context.CancelFunc) {
@@ -22,3 +26,21 @@ func withRequestTimeout(r *http.Request, timeout time.Duration) (context.Context
}
return context.WithTimeout(base, timeout)
}
func decodeJSONBody(w http.ResponseWriter, r *http.Request, dst any) error {
if r == nil || r.Body == nil {
return errors.New("request body is required")
}
decoder := json.NewDecoder(http.MaxBytesReader(w, r.Body, defaultJSONBodyLimitBytes))
if err := decoder.Decode(dst); err != nil {
return err
}
var trailing any
if err := decoder.Decode(&trailing); err != io.EOF {
if err == nil {
return errors.New("request body must contain only one JSON object")
}
return err
}
return nil
}