-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
83 lines (69 loc) · 1.94 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Package v1 implements the v1 of the k6's REST API
package v1
import (
"net/http"
)
// NewHandler returns the top handler for the v1 REST APIs
func NewHandler(cs *ControlSurface) http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/v1/status", func(rw http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
handleGetStatus(cs, rw, r)
case http.MethodPatch:
handlePatchStatus(cs, rw, r)
default:
rw.WriteHeader(http.StatusMethodNotAllowed)
}
})
mux.HandleFunc("/v1/metrics", func(rw http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
rw.WriteHeader(http.StatusMethodNotAllowed)
return
}
handleGetMetrics(cs, rw, r)
})
mux.HandleFunc("/v1/metrics/", func(rw http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
rw.WriteHeader(http.StatusMethodNotAllowed)
return
}
id := r.URL.Path[len("/v1/metrics/"):]
handleGetMetric(cs, rw, r, id)
})
mux.HandleFunc("/v1/groups", func(rw http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
rw.WriteHeader(http.StatusMethodNotAllowed)
return
}
handleGetGroups(cs, rw, r)
})
mux.HandleFunc("/v1/groups/", func(rw http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
rw.WriteHeader(http.StatusMethodNotAllowed)
return
}
id := r.URL.Path[len("/v1/groups/"):]
handleGetGroup(cs, rw, r, id)
})
mux.HandleFunc("/v1/setup", func(rw http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost:
handleRunSetup(cs, rw, r)
case http.MethodPut:
handleSetSetupData(cs, rw, r)
case http.MethodGet:
handleGetSetupData(cs, rw, r)
default:
rw.WriteHeader(http.StatusMethodNotAllowed)
}
})
mux.HandleFunc("/v1/teardown", func(rw http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
rw.WriteHeader(http.StatusMethodNotAllowed)
return
}
handleRunTeardown(cs, rw, r)
})
return mux
}