/
server.go
64 lines (56 loc) · 1.21 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
package server
import (
"net/http"
"github.com/alde/ale/db"
"github.com/alde/ale/config"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
)
// NewRouter is used to create a new HTTP router
func NewRouter(cfg *config.Config, db db.Database) *mux.Router {
router := mux.NewRouter().StrictSlash(true)
h := NewHandler(cfg, db)
for _, route := range routes(h) {
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(prometheus.InstrumentHandler(route.Name, route.Handler))
}
return router
}
// Route enforces the structure of a route
type route struct {
Name string
Method string
Pattern string
Handler http.Handler
}
func routes(h *Handler) []route {
return []route{
{
Name: "PostBuildOptions",
Method: "OPTIONS",
Pattern: "/api/v1/process",
Handler: h.ProcessOptions(),
},
{
Name: "PostBuild",
Method: "POST",
Pattern: "/api/v1/process",
Handler: h.ProcessBuild(),
},
{
Name: "GetBuild",
Method: "GET",
Pattern: "/api/v1/build/{id}",
Handler: h.GetJenkinsBuild(),
},
{
Name: "ServiceMetadata",
Method: "GET",
Pattern: "/service-metadata",
Handler: h.ServiceMetadata(),
},
}
}