-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathrouter.go
95 lines (83 loc) · 1.81 KB
/
router.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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package fleet
import (
"net/http"
"github.com/elastic/fleet-server/v7/internal/pkg/bulk"
"github.com/elastic/fleet-server/v7/internal/pkg/logger"
"github.com/elastic/fleet-server/v7/internal/pkg/policy"
"github.com/julienschmidt/httprouter"
"github.com/rs/zerolog/log"
)
const (
ROUTE_STATUS = "/api/status"
ROUTE_ENROLL = "/api/fleet/agents/:id"
ROUTE_CHECKIN = "/api/fleet/agents/:id/checkin"
ROUTE_ACKS = "/api/fleet/agents/:id/acks"
ROUTE_ARTIFACTS = "/api/fleet/artifacts/:id/:sha2"
)
type Router struct {
bulker bulk.Bulk
ver string
ct *CheckinT
et *EnrollerT
at *ArtifactT
ack *AckT
sm policy.SelfMonitor
}
func NewRouter(bulker bulk.Bulk, ct *CheckinT, et *EnrollerT, at *ArtifactT, ack *AckT, sm policy.SelfMonitor) *httprouter.Router {
r := Router{
bulker: bulker,
ct: ct,
et: et,
sm: sm,
at: at,
ack: ack,
}
routes := []struct {
method string
path string
handler httprouter.Handle
}{
{
http.MethodGet,
ROUTE_STATUS,
r.handleStatus,
},
{
http.MethodPost,
ROUTE_ENROLL,
r.handleEnroll,
},
{
http.MethodPost,
ROUTE_CHECKIN,
r.handleCheckin,
},
{
http.MethodPost,
ROUTE_ACKS,
r.handleAcks,
},
{
http.MethodGet,
ROUTE_ARTIFACTS,
r.handleArtifacts,
},
}
router := httprouter.New()
// Install routes
for _, rte := range routes {
log.Info().
Str("method", rte.method).
Str("path", rte.path).
Msg("Server install route")
router.Handle(
rte.method,
rte.path,
logger.HttpHandler(rte.handler),
)
}
return router
}