39 lines
1008 B
Go
39 lines
1008 B
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
func (rest *REST) respOK(w http.ResponseWriter) {
|
|
rest.respMap(w, "ok", true)
|
|
}
|
|
|
|
func (rest *REST) respConflict(w http.ResponseWriter) {
|
|
rest.respMapStatus(w, "error", "collision found", http.StatusConflict)
|
|
}
|
|
|
|
func (rest *REST) respError(w http.ResponseWriter, err error) {
|
|
rest.respMapStatus(w, "error", fmt.Sprint(err), http.StatusInternalServerError)
|
|
}
|
|
|
|
func (rest *REST) respBadRequest(w http.ResponseWriter, msg string) {
|
|
rest.respMapStatus(w, "error", msg, http.StatusBadRequest)
|
|
}
|
|
|
|
func (rest *REST) respMapStatus(w http.ResponseWriter, key string, value interface{}, status int) {
|
|
w.WriteHeader(status)
|
|
rest.resp(w, map[string]interface{}{key: value})
|
|
}
|
|
|
|
func (rest *REST) respMap(w http.ResponseWriter, key string, value interface{}) {
|
|
rest.resp(w, map[string]interface{}{key: value})
|
|
}
|
|
|
|
func (rest *REST) resp(w http.ResponseWriter, body interface{}) {
|
|
enc := json.NewEncoder(w)
|
|
enc.SetIndent("", " ")
|
|
enc.Encode(body)
|
|
}
|