33 lines
564 B
Go
33 lines
564 B
Go
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)
|
|
}
|