forked from statping/statping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
190 lines (172 loc) · 4.35 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package handlers
import (
"fmt"
"github.com/statping/statping/types/checkins"
"github.com/statping/statping/types/core"
"github.com/statping/statping/types/errors"
"github.com/statping/statping/types/groups"
"github.com/statping/statping/types/incidents"
"github.com/statping/statping/types/messages"
"github.com/statping/statping/types/notifications"
"github.com/statping/statping/types/null"
"github.com/statping/statping/types/services"
"github.com/statping/statping/types/users"
"github.com/statping/statping/utils"
"net/http"
"time"
)
type apiResponse struct {
Status string `json:"status"`
Object string `json:"type,omitempty"`
Method string `json:"method,omitempty"`
Error error `json:"error,omitempty"`
Id int64 `json:"id,omitempty"`
Output interface{} `json:"output,omitempty"`
}
func apiIndexHandler(r *http.Request) interface{} {
return core.App
}
func apiRenewHandler(w http.ResponseWriter, r *http.Request) {
var err error
core.App.ApiSecret = utils.NewSHA256Hash()
err = core.App.Update()
if err != nil {
sendErrorJson(err, w, r)
return
}
output := apiResponse{
Status: "success",
}
returnJson(output, w, r)
}
func apiUpdateOAuthHandler(w http.ResponseWriter, r *http.Request) {
var oauth core.OAuth
if err := DecodeJSON(r, &oauth); err != nil {
sendErrorJson(err, w, r)
return
}
core.App.OAuth = oauth
if err := core.App.Update(); err != nil {
sendErrorJson(err, w, r)
return
}
sendJsonAction(core.App.OAuth, "update", w, r)
}
func apiOAuthHandler(r *http.Request) interface{} {
app := core.App
return app.OAuth
}
func apiCoreHandler(w http.ResponseWriter, r *http.Request) {
var c *core.Core
err := DecodeJSON(r, &c)
if err != nil {
sendErrorJson(err, w, r)
return
}
app := core.App
if c.Name != "" {
app.Name = c.Name
}
if c.Description != app.Description {
app.Description = c.Description
}
if c.Style != app.Style {
app.Style = c.Style
}
if c.Footer.String != app.Footer.String {
app.Footer = c.Footer
}
if c.Domain != app.Domain {
app.Domain = c.Domain
}
app.UseCdn = null.NewNullBool(c.UseCdn.Bool)
app.AllowReports = null.NewNullBool(c.AllowReports.Bool)
utils.SentryInit(nil, app.AllowReports.Bool)
err = app.Update()
returnJson(core.App, w, r)
}
type cacheJson struct {
URL string `json:"url"`
Expiration time.Time `json:"expiration"`
Size int `json:"size"`
}
func apiCacheHandler(w http.ResponseWriter, r *http.Request) {
var cacheList []cacheJson
for k, v := range CacheStorage.List() {
cacheList = append(cacheList, cacheJson{
URL: k,
Expiration: time.Unix(0, v.Expiration).UTC(),
Size: len(v.Content),
})
}
returnJson(cacheList, w, r)
}
func apiClearCacheHandler(w http.ResponseWriter, r *http.Request) {
CacheStorage.StopRoutine()
CacheStorage = NewStorage()
output := apiResponse{
Status: "success",
}
returnJson(output, w, r)
}
func sendErrorJson(err error, w http.ResponseWriter, r *http.Request) {
errCode := 0
e, ok := err.(errors.Error)
if ok {
errCode = e.Status()
}
log.WithField("url", r.URL.String()).
WithField("method", r.Method).
WithField("code", errCode).
Errorln(fmt.Errorf("sending error response for %s: %s", r.URL.String(), err.Error()))
returnJson(err, w, r)
}
func sendJsonAction(obj interface{}, method string, w http.ResponseWriter, r *http.Request) {
var objName string
var objId int64
switch v := obj.(type) {
case *services.Service:
objName = "service"
objId = v.Id
case *notifications.Notification:
objName = "notifier"
objId = v.Id
case *core.Core:
objName = "core"
case *users.User:
objName = "user"
objId = v.Id
case *groups.Group:
objName = "group"
objId = v.Id
case *checkins.Checkin:
objName = "checkin"
objId = v.Id
case *checkins.CheckinHit:
objName = "checkin_hit"
objId = v.Id
case *messages.Message:
objName = "message"
objId = v.Id
case *incidents.Incident:
objName = "incident"
objId = v.Id
case *incidents.IncidentUpdate:
objName = "incident_update"
objId = v.Id
default:
objName = fmt.Sprintf("%T", v)
}
output := apiResponse{
Object: objName,
Method: method,
Id: objId,
Status: "success",
Output: obj,
}
returnJson(output, w, r)
}
func sendUnauthorizedJson(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
returnJson(errors.NotAuthenticated, w, r)
}