26 lines
629 B
1
package knotserver
2
3
import (
4
"encoding/json"
5
"net/http"
6
)
7
8
func 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
14
func 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
20
func notFound(w http.ResponseWriter) {
21
writeError(w, "not found", http.StatusNotFound)
22
}
23
24
func writeMsg(w http.ResponseWriter, msg string) {
25
writeJSON(w, map[string]string{"msg": msg})
26
}
27