-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
135 lines (124 loc) · 3.43 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
// SPDX-FileCopyrightText: 2024 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package skeleton
import (
"net/http"
"strings"
"github.com/go-chi/chi/v5"
"github.com/xmidt-org/arrange/arrangehttp"
"github.com/xmidt-org/arrange/arrangepprof"
"github.com/xmidt-org/httpaux"
"github.com/xmidt-org/skeleton/internal/apiauth"
"github.com/xmidt-org/skeleton/internal/oker"
"github.com/xmidt-org/touchstone/touchhttp"
"go.uber.org/fx"
)
type RoutesIn struct {
fx.In
PrimaryMetrics touchhttp.ServerInstrumenter `name:"servers.primary.metrics"`
AlternateMetrics touchhttp.ServerInstrumenter `name:"servers.alternate.metrics"`
Routes Routes
Oker *oker.Server
ApiAuth *apiauth.Auth
}
type RoutesOut struct {
fx.Out
Primary arrangehttp.Option[http.Server] `group:"servers.primary.options"`
Alternate arrangehttp.Option[http.Server] `group:"servers.alternate.options"`
}
// The name should be 'primary' or 'alternate'.
func provideCoreEndpoints() fx.Option {
return fx.Provide(
fx.Annotated{
Name: "servers.primary.metrics",
Target: touchhttp.ServerBundle{}.NewInstrumenter(
touchhttp.ServerLabel, "primary",
),
},
fx.Annotated{
Name: "servers.alternate.metrics",
Target: touchhttp.ServerBundle{}.NewInstrumenter(
touchhttp.ServerLabel, "alternate",
),
},
func(in RoutesIn) RoutesOut {
return RoutesOut{
Primary: provideCoreOption("primary", in),
Alternate: provideCoreOption("alternate", in),
}
},
)
}
func provideCoreOption(server string, in RoutesIn) arrangehttp.Option[http.Server] {
return arrangehttp.AsOption[http.Server](
func(s *http.Server) {
mux := chi.NewMux()
if strings.ToLower(in.Routes.Oker.Server) == server {
mux.Method("GET", in.Routes.Oker.Path,
in.ApiAuth.Then(in.Oker.ServeHTTP))
}
if server == "primary" {
s.Handler = in.PrimaryMetrics.Then(mux)
} else {
s.Handler = in.AlternateMetrics.Then(mux)
}
},
)
}
func provideHealthCheck() fx.Option {
return fx.Provide(
fx.Annotated{
Name: "servers.health.metrics",
Target: touchhttp.ServerBundle{}.NewInstrumenter(
touchhttp.ServerLabel, "health",
),
},
fx.Annotate(
func(metrics touchhttp.ServerInstrumenter, path HealthPath) arrangehttp.Option[http.Server] {
return arrangehttp.AsOption[http.Server](
func(s *http.Server) {
mux := chi.NewMux()
mux.Method("GET", string(path), httpaux.ConstantHandler{
StatusCode: http.StatusOK,
})
s.Handler = metrics.Then(mux)
},
)
},
fx.ParamTags(`name:"servers.health.metrics"`),
fx.ResultTags(`group:"servers.health.options"`),
),
)
}
func provideMetricEndpoint() fx.Option {
return fx.Provide(
fx.Annotate(
func(metrics touchhttp.Handler, path MetricsPath) arrangehttp.Option[http.Server] {
return arrangehttp.AsOption[http.Server](
func(s *http.Server) {
mux := chi.NewMux()
mux.Method("GET", string(path), metrics)
s.Handler = mux
},
)
},
fx.ResultTags(`group:"servers.metrics.options"`),
),
)
}
func providePprofEndpoint() fx.Option {
return fx.Provide(
fx.Annotate(
func(pathPrefix PprofPathPrefix) arrangehttp.Option[http.Server] {
return arrangehttp.AsOption[http.Server](
func(s *http.Server) {
s.Handler = arrangepprof.HTTP{
PathPrefix: string(pathPrefix),
}.New()
},
)
},
fx.ResultTags(`group:"servers.pprof.options"`),
),
)
}