/
server.go
122 lines (98 loc) · 3.25 KB
/
server.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package server
import (
"encoding/json"
"github.com/julienschmidt/httprouter"
"github.com/odinsplasmarifle/statter/app"
"gopkg.in/guregu/null.v3"
"log"
"net/http"
)
type Server struct {
*app.Env
}
func (srv Server) Serve() {
router := httprouter.New()
router.GET("/services/", srv.listServices)
router.GET("/responses/", srv.listResponses)
log.Fatal(http.ListenAndServe(":"+srv.Conf.Port, router))
}
// Service struct for custom built outputs.
type service struct {
Name string `json:"name"`
Label string `json:"label"`
Description string `json:"description"`
TotalRequests null.Int `json:"totalRequests" db:"total"`
TotalFailedRequests null.Int `json:"totalFailedRequests" db:"total_failed"`
LastFailedRequestDate null.Time `json:"lastFailedRequestDate" db:"last_failed"`
StatusCode null.Int `json:"statusCode" db:"status_code"`
}
// Response struct for data stored in the database.
type response struct {
Id string `db:"id" json:"id"`
Name string `db:"name" json:"name"`
Url string `db:"url" json:"url"`
StatusCode int `db:"status_code" json:"statusCode"`
Error null.String `db:"error" json:"error"`
Created null.Time `db:"created" json:"created"`
}
// List services and filter services by name.
func (srv Server) listServices(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
db, err := srv.ConnectDb()
if err != nil {
panic(err)
}
var ss []service
filters := r.URL.Query()
for _, confService := range srv.Conf.Services {
mustAppend := true
if filters["name"] != nil {
if filters["name"][0] != "" && confService.Name == filters["name"][0] {
mustAppend = true
} else {
mustAppend = false
}
}
if mustAppend {
s := service{}
err := db.Get(&s,
`SELECT status_code, count(*) AS total,
(SELECT count(*) FROM responses tf
WHERE tf.name=$1 AND (tf.status_code >= 300 OR tf.status_code < 200)) AS total_failed,
(SELECT created FROM responses tf2
WHERE tf2.name=$1 AND (tf2.status_code >= 300 OR tf2.status_code < 200) ORDER BY id DESC LIMIT 1) AS last_failed
FROM responses WHERE name=$1 ORDER BY id DESC`, confService.Name)
if err != nil {
panic(err)
}
s.Name = confService.Name
s.Label = confService.Label
s.Description = confService.Description
ss = append(ss, s)
}
}
responseJson, _ := json.MarshalIndent(ss, "", " ")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(responseJson)
}
// List services and filter responses by service name.
func (srv Server) listResponses(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
db, err := srv.ConnectDb()
if err != nil {
panic(err)
}
rs := []*response{}
filters := r.URL.Query()
if filters["name"] != nil && filters["name"][0] != "" {
err = db.Select(&rs, "SELECT * FROM responses WHERE name=$1 ORDER BY id DESC LIMIT 100", filters["name"][0])
} else {
err = db.Select(&rs, "SELECT * FROM responses ORDER BY id DESC LIMIT 100")
}
if err != nil {
panic(err)
}
responseJson, _ := json.MarshalIndent(rs, "", " ")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(responseJson)
}