package web import ( "encoding/json" "net/http" "strconv" ) func pathID(r *http.Request, name string) (int64, error) { return strconv.ParseInt(r.PathValue(name), 10, 64) } // queryOptionalID parses a query parameter as *int64; an empty or missing // value returns nil (meaning "top level" / "unfiled" in folder/asset // contexts). func queryOptionalID(r *http.Request, name string) (*int64, error) { v := r.URL.Query().Get(name) if v == "" { return nil, nil } id, err := strconv.ParseInt(v, 10, 64) if err != nil { return nil, err } return &id, nil } func queryInt64(r *http.Request, name string) int64 { v := r.URL.Query().Get(name) if v == "" { return 0 } id, _ := strconv.ParseInt(v, 10, 64) return id } func queryInt(r *http.Request, name string, def int) int { v := r.URL.Query().Get(name) if v == "" { return def } n, err := strconv.Atoi(v) if err != nil { return def } return n } type jsonEnvelope struct { OK bool `json:"ok"` Error string `json:"error,omitempty"` Message string `json:"message,omitempty"` Data any `json:"data,omitempty"` } func writeJSON(w http.ResponseWriter, status int, v jsonEnvelope) { w.Header().Set("Content-Type", "application/json; charset=utf-8") w.WriteHeader(status) _ = json.NewEncoder(w).Encode(v) } func jsonOK(w http.ResponseWriter, data any) { writeJSON(w, http.StatusOK, jsonEnvelope{OK: true, Data: data}) } func jsonErr(w http.ResponseWriter, status int, err error) { writeJSON(w, status, jsonEnvelope{OK: false, Error: err.Error()}) } func readJSON(r *http.Request, v any) error { dec := json.NewDecoder(r.Body) dec.DisallowUnknownFields() return dec.Decode(v) }