This repository has been archived on 2025-03-30. You can view files and clone it, but cannot push or open issues or pull requests.
bactdb/api/helpers.go
Matthew Dillon be9e6481d0 Roughing in JWT-based authentication.
Todo: Actually utilize this stuff somewhere.
2014-12-10 13:52:26 -09:00

37 lines
802 B
Go

package api
import (
"encoding/json"
"log"
"net/http"
)
// writeJSON writes a JSON Content-Type header and a JSON-encoded object to
// the http.ResponseWriter.
func writeJSON(w http.ResponseWriter, v interface{}) error {
data, err := json.MarshalIndent(v, "", " ")
if err != nil {
return err
}
w.Header().Set("content-type", "application/json; charset=utf-8")
_, err = w.Write(data)
return err
}
// Message is for returning simple message payloads to the user
type Message struct {
Message string `json:"message"`
}
// Error is for returning simple error payloads to the user, as well as logging
type Error struct {
Error error
}
func (e Error) MarshalJSON() ([]byte, error) {
log.Println(e.Error)
return json.Marshal(struct {
Error string `json:"error"`
}{e.Error.Error()})
}