-
Notifications
You must be signed in to change notification settings - Fork 10
/
routes.go
183 lines (153 loc) · 4.71 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// SPDX-FileCopyrightText: 2017 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
import (
"errors"
"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/xhttp/xhttpserver/pprof"
"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
HandlerJWK key.HandlerJWK
}
func BuildKeyRoutes(in KeyRoutesIn) {
if in.Router != nil {
keys := in.Router.PathPrefix("/keys/{kid}").Methods("GET").Subrouter()
keys.Headers("Accept", key.ContentTypePEM).Handler(in.Handler)
keys.Headers("Accept", key.ContentTypeJWK).Handler(in.HandlerJWK)
keys.Path("").Handler(in.Handler) // default
keys.Path("/key.pem").Handler(in.Handler)
keys.Path("/key.json").Handler(in.HandlerJWK)
}
}
type IssuerRoutesIn struct {
fx.In
Router *mux.Router `name:"servers.issuer"`
Handler token.IssueHandler
}
func BuildIssuerRoutes(in IssuerRoutesIn) {
if in.Router != nil && in.Handler != 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.Handler != nil {
in.Router.Handle("/claims", in.Handler).Methods("GET")
}
}
// CheckServerRequirements is an fx.Invoke function that does post-configuration verification
// that we have required servers. The valid server configurations are:
//
// Both keys and issuer present. Claims is optional in this case
// Neither keys or issuer present. Claims is required in this case
//
// Any other arrangements results in an error.
func CheckServerRequirements(k KeyRoutesIn, i IssuerRoutesIn, c ClaimsRoutesIn) error {
if k.Router != nil && i.Router != nil {
// all good ... no need to check anything else
return nil
}
if k.Router == nil && i.Router == nil {
if c.Router == nil {
return errors.New("A claims server is required if no keys or issuer server is configured")
}
// Only a claims server is allowed
return nil
}
if k.Router != nil {
return errors.New("If a keys server is configured, an issuer server must be configured")
}
if i.Router != nil {
return errors.New("If an issuer server is configured, a keys server must be configured")
}
return nil
}
type MetricsRoutesIn struct {
fx.In
Router *mux.Router `name:"servers.metrics"`
Handler xmetricshttp.Handler
}
func BuildMetricsRoutes(in MetricsRoutesIn) {
if in.Router != nil && in.Handler != nil {
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) {
if in.Router != nil && in.Handler != nil {
in.Router.Handle("/health", in.Handler).Methods("GET")
}
}
type PprofRoutesIn struct {
fx.In
Router *mux.Router `name:"servers.pprof"`
}
func BuildPprofRoutes(in PprofRoutesIn) {
if in.Router != nil {
pprof.BuildRoutes(in.Router)
}
}