-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjopher.go
More file actions
33 lines (28 loc) · 946 Bytes
/
jopher.go
File metadata and controls
33 lines (28 loc) · 946 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package jopher
import (
"encoding/json"
"errors"
"net/http"
)
// M is a type of map[string]interface{}
type M map[string]interface{}
// Write transforms the passed message to a json object,
// then sends response with the status code passed
func Write(w http.ResponseWriter, status int, msg interface{}) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(status)
err := json.NewEncoder(w).Encode(msg)
// enc, err := json.Marshal(msg)
if err != nil {
InternalServerError(w, errors.New("Failed to encode response body to json"))
}
//w.Write([]byte(enc))
}
// Success return a 200 response with the passed message as the body of the response
func Success(w http.ResponseWriter, msg interface{}) {
Write(w, http.StatusOK, msg)
}
// Created return a 201 response with the passed message as the body of the response
func Created(w http.ResponseWriter, msg interface{}) {
Write(w, http.StatusOK, msg)
}