forked from d6o/HttpHealth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.go
63 lines (50 loc) · 1.23 KB
/
routes.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"
"fmt"
"net/http"
"github.com/asaskevich/govalidator"
"github.com/disiqueira/itinerary"
)
func (a *App) serveList(w http.ResponseWriter, r *http.Request) {
pingList := make(map[string]PingResponse)
for _, e := range a.URLList {
p := NewHeadPing(e)
status, err := p.Ping()
if err != nil {
panic(err)
}
pingList[p.URL()] = status
}
response := NewResponse(w)
response.Write(http.StatusOK, pingList)
}
func (a *App) serveURLList(w http.ResponseWriter, r *http.Request) {
res, _ := json.Marshal(&a.URLList)
w.Write(res)
}
func (a *App) serveURLCreate(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
if !govalidator.IsURL(r.FormValue("url")) {
w.WriteHeader(http.StatusBadRequest)
return
}
a.AddURL(r.FormValue("url"))
w.WriteHeader(http.StatusOK)
}
func (a *App) serveURLDelete(w http.ResponseWriter, r *http.Request) {
itinerary.RouteToQuery(r)
fmt.Println(r.URL.Query())
url := r.URL.Query()["itinerary"][0]
if !govalidator.IsURL(url) {
w.WriteHeader(http.StatusBadRequest)
return
}
for i := 0; i < len(a.URLList); i++ {
u := a.URLList[i]
if u == url {
a.URLList = append(a.URLList[:i], a.URLList[i+1:]...)
}
}
w.WriteHeader(http.StatusNoContent)
}