perkeep/blobserver/go/http_util.go

30 lines
607 B
Go

package main
import (
"fmt"
"http"
"json"
"os"
)
func badRequestError(conn *http.Conn, errorMessage string) {
conn.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(conn, "%s\n", errorMessage)
}
func serverError(conn *http.Conn, err os.Error) {
conn.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(conn, "Server error: %s\n", err)
}
func returnJson(conn *http.Conn, data interface{}) {
bytes, err := json.MarshalIndent(data, "", " ")
if err != nil {
badRequestError(conn, fmt.Sprintf(
"JSON serialization error: %v", err))
return
}
conn.Write(bytes)
conn.Write([]byte("\n"))
}