src/api/utils.go (view raw)
1package api
2
3import (
4 "encoding/json"
5 "net/http"
6 "strconv"
7 "strings"
8)
9
10func jsonResponse(w http.ResponseWriter, status int, data interface{}) {
11 w.Header().Set("Content-Type", "application/json")
12 w.WriteHeader(status)
13 json.NewEncoder(w).Encode(data)
14}
15
16func jsonError(w http.ResponseWriter, status int, messages ...string) {
17 jsonResponse(w, status, map[string]string{"error": strings.Join(messages, " ")})
18}
19
20func getIDFromPath(r *http.Request) (uint, error) {
21 id, err := strconv.Atoi(r.PathValue("id"))
22 return uint(id), err
23}