-
Notifications
You must be signed in to change notification settings - Fork 7
/
api.go
63 lines (51 loc) · 1.44 KB
/
api.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"encoding/json"
"net/http"
"time"
)
type apiResponse struct {
Status string `json:"status"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
type apiRowData struct {
RowCount int `json:"rowCount"`
Rows []preRow `json:"rows"`
Offset int `json:"offset"`
ReqCount int `json:"reqCount"`
Total int `json:"total"`
Time float64 `json:"time"`
}
type apiStatsData struct {
Total int `json:"total"`
Date time.Time `json:"date"`
Time float64 `json:"time"`
}
type apiTeamsData struct {
RowCount int `json:"rowCount"`
Rows []teamRow `json:"rows"`
Offset int `json:"offset"`
ReqCount int `json:"reqCount"`
Total int `json:"total"`
Time float64 `json:"time"`
}
const indent = " "
func apiSuccess(w http.ResponseWriter, o interface{}) error {
return apiSend(w, apiResponse{"success", "", o})
}
func apiFail(w http.ResponseWriter, o interface{}) error {
return apiSend(w, apiResponse{"error", "", o})
}
func apiErr(w http.ResponseWriter, err error) error {
return apiSend(w, apiResponse{"error", err.Error(), nil})
}
func apiSend(w http.ResponseWriter, o interface{}) error {
headers := w.Header()
headers.Set("Content-Type", "application/json; charset=utf-8")
headers.Set("Access-Control-Allow-Origin", "*")
w.WriteHeader(http.StatusOK)
enc := json.NewEncoder(w)
enc.SetIndent("", indent)
return enc.Encode(o)
}