-
Notifications
You must be signed in to change notification settings - Fork 1
/
status.go
44 lines (39 loc) · 1.08 KB
/
status.go
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
34
35
36
37
38
39
40
41
42
43
44
package goserv
import (
"bytes"
"encoding/json"
"net/http"
"time"
)
// Status is used for the output of all status items
type Status struct {
Time time.Time
Data interface{}
}
// Callable is a function that takes no input and returns on item
type Callable func() interface{}
// StructHandler takes an interface and bool to note if the results
// should be pretty and turns it into Status output in JSON
func StructHandler(item interface{}, pretty bool) http.HandlerFunc {
status := func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
status := Status{
Time: time.Now().UTC(),
Data: item,
}
output, _ := json.Marshal(status)
if pretty {
var buf bytes.Buffer
json.Indent(&buf, output, "", "\t")
output = buf.Bytes()
}
w.Write(output)
return
}
return status
}
// FuncHandler takes a Callable and bool to note if the results should be pretty
// and passes the resulting interface go StatusHandler.StructHandler
func FuncHandler(callable Callable, pretty bool) http.HandlerFunc {
return StructHandler(callable(), pretty)
}