Initial backend import

This commit is contained in:
TerryM
2026-05-16 00:18:22 +08:00
commit 141d92dc15
22 changed files with 2028 additions and 0 deletions

32
internal/handlers/util.go Normal file
View File

@@ -0,0 +1,32 @@
package handlers
import (
"encoding/json"
"io"
"net/http"
"strconv"
)
func writeJSON(w http.ResponseWriter, v any) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
enc := json.NewEncoder(w)
enc.SetEscapeHTML(false)
_ = enc.Encode(v)
}
func atoiDef(s string, def int) int {
if s == "" {
return def
}
n, err := strconv.Atoi(s)
if err != nil || n < 1 {
return def
}
return n
}
func jsonDecode(r *http.Request, v any) error {
defer r.Body.Close()
dec := json.NewDecoder(io.LimitReader(r.Body, 1<<20))
return dec.Decode(v)
}