forked from xmidt-org/themis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.go
123 lines (103 loc) · 2.92 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
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
package main
import (
"github.com/xmidt-org/themis/key"
"github.com/xmidt-org/themis/token"
"github.com/xmidt-org/themis/xhealth"
"github.com/xmidt-org/themis/xhttp/xhttpserver"
"github.com/xmidt-org/themis/xmetrics"
"github.com/xmidt-org/themis/xmetrics/xmetricshttp"
"github.com/gorilla/mux"
"github.com/justinas/alice"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/fx"
)
type ServerChainIn struct {
fx.In
RequestCount *prometheus.CounterVec `name:"server_request_count"`
RequestDuration *prometheus.HistogramVec `name:"server_request_duration_ms"`
RequestsInFlight *prometheus.GaugeVec `name:"server_requests_in_flight"`
}
func provideServerChainFactory(in ServerChainIn) xhttpserver.ChainFactory {
return xhttpserver.ChainFactoryFunc(func(name string, o xhttpserver.Options) (alice.Chain, error) {
var (
curryLabel = prometheus.Labels{
ServerLabel: name,
}
serverLabellers = xmetricshttp.NewServerLabellers(
xmetricshttp.CodeLabeller{},
xmetricshttp.MethodLabeller{},
)
)
requestCount, err := in.RequestCount.CurryWith(curryLabel)
if err != nil {
return alice.Chain{}, err
}
requestDuration, err := in.RequestDuration.CurryWith(curryLabel)
if err != nil {
return alice.Chain{}, err
}
requestsInFlight, err := in.RequestsInFlight.CurryWith(curryLabel)
if err != nil {
return alice.Chain{}, err
}
return alice.New(
xmetricshttp.HandlerCounter{
Metric: xmetrics.LabelledCounterVec{CounterVec: requestCount},
Labeller: serverLabellers,
}.Then,
xmetricshttp.HandlerDuration{
Metric: xmetrics.LabelledObserverVec{ObserverVec: requestDuration},
Labeller: serverLabellers,
}.Then,
xmetricshttp.HandlerInFlight{
Metric: xmetrics.LabelledGaugeVec{GaugeVec: requestsInFlight},
}.Then,
), nil
})
}
type KeyRoutesIn struct {
fx.In
Router *mux.Router `name:"servers.key"`
Handler key.Handler
}
func BuildKeyRoutes(in KeyRoutesIn) {
if in.Router != nil {
in.Router.Handle("/keys/{kid}", in.Handler).Methods("GET")
}
}
type IssuerRoutesIn struct {
fx.In
Router *mux.Router `name:"servers.issuer"`
Handler token.IssueHandler
}
func BuildIssuerRoutes(in IssuerRoutesIn) {
if in.Router != nil {
in.Router.Handle("/issue", in.Handler).Methods("GET")
}
}
type ClaimsRoutesIn struct {
fx.In
Router *mux.Router `name:"servers.claims"`
Handler token.ClaimsHandler
}
func BuildClaimsRoutes(in ClaimsRoutesIn) {
if in.Router != nil {
in.Router.Handle("/claims", in.Handler).Methods("GET")
}
}
type MetricsRoutesIn struct {
fx.In
Router *mux.Router `name:"servers.metrics"`
Handler xmetricshttp.Handler
}
func BuildMetricsRoutes(in MetricsRoutesIn) {
in.Router.Handle("/metrics", in.Handler).Methods("GET")
}
type HealthRoutesIn struct {
fx.In
Router *mux.Router `name:"servers.health"`
Handler xhealth.Handler
}
func BuildHealthRoutes(in HealthRoutesIn) {
in.Router.Handle("/health", in.Handler).Methods("GET")
}