1package knotserver
2
3import (
4 "encoding/json"
5 "net/http"
6)
7
8func writeJSON(w http.ResponseWriter, data interface{}) {
9 w.Header().Set("Content-Type", "application/json")
10 w.WriteHeader(http.StatusOK)
11 json.NewEncoder(w).Encode(data)
12}
13
14func writeError(w http.ResponseWriter, msg string, status int) {
15 w.Header().Set("Content-Type", "application/json")
16 w.WriteHeader(status)
17 json.NewEncoder(w).Encode(map[string]string{"error": msg})
18}
19
20func notFound(w http.ResponseWriter) {
21 writeError(w, "not found", http.StatusNotFound)
22}
23
24func writeMsg(w http.ResponseWriter, msg string) {
25 writeJSON(w, map[string]string{"msg": msg})
26}
27
28func writeConflict(w http.ResponseWriter, data interface{}) {
29 w.Header().Set("Content-Type", "application/json")
30 w.WriteHeader(http.StatusConflict)
31 json.NewEncoder(w).Encode(data)
32}