-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
311 lines (250 loc) · 10.8 KB
/
main.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package main
import (
"context"
goErrors "errors"
"fmt"
"net/http"
"net/smtp"
"os"
"os/signal"
"github.com/ONSdigital/dp-api-clients-go/v2/files"
"github.com/ONSdigital/dp-api-clients-go/v2/population"
"github.com/ONSdigital/dp-frontend-dataset-controller/cache"
cachePublic "github.com/ONSdigital/dp-frontend-dataset-controller/cache/public"
topic "github.com/ONSdigital/dp-topic-api/sdk"
"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"github.com/ONSdigital/dp-api-clients-go/v2/dataset"
"github.com/ONSdigital/dp-api-clients-go/v2/filter"
apihealthcheck "github.com/ONSdigital/dp-api-clients-go/v2/health"
"github.com/ONSdigital/dp-api-clients-go/v2/zebedee"
"github.com/ONSdigital/dp-frontend-dataset-controller/assets"
"github.com/ONSdigital/dp-frontend-dataset-controller/config"
"github.com/ONSdigital/dp-frontend-dataset-controller/handlers"
"github.com/ONSdigital/dp-frontend-dataset-controller/helpers"
health "github.com/ONSdigital/dp-healthcheck/healthcheck"
render "github.com/ONSdigital/dp-renderer/v2"
"github.com/ONSdigital/dp-renderer/v2/middleware/renderror"
"github.com/ONSdigital/log.go/v2/log"
"github.com/gorilla/mux"
"github.com/justinas/alice"
"github.com/pkg/errors"
dpnethandlers "github.com/ONSdigital/dp-net/v2/handlers"
dpnethttp "github.com/ONSdigital/dp-net/v2/http"
dpotelgo "github.com/ONSdigital/dp-otel-go"
_ "net/http/pprof"
)
type unencryptedAuth struct {
smtp.Auth
}
func (a unencryptedAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
s := *server
s.TLS = true
return a.Auth.Start(&s)
}
// App version informaton retrieved on runtime
var (
// BuildTime represents the time in which the service was built
BuildTime string
// GitCommit represents the commit (SHA-1) hash of the service that is running
GitCommit string
// Version represents the version of the service that is running
Version string
)
func main() {
ctx := context.Background()
log.Namespace = "dp-frontend-dataset-controller"
if err := run(ctx); err != nil {
log.Error(ctx, "application unexpectedly failed", err)
os.Exit(1)
}
os.Exit(0)
}
func run(ctx context.Context) error {
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, os.Kill)
// Get config
cfg, err := config.Get()
if err != nil {
log.Error(ctx, "unable to retrieve service configuration", err)
return err
}
log.Info(ctx, "got service configuration", log.Data{"config": cfg})
if cfg.OtelEnabled {
otelConfig := dpotelgo.Config{
OtelServiceName: cfg.OTServiceName,
OtelExporterOtlpEndpoint: cfg.OTExporterOTLPEndpoint,
OtelBatchTimeout: cfg.OTBatchTimeout,
}
otelShutdown, oErr := dpotelgo.SetupOTelSDK(ctx, otelConfig)
if oErr != nil {
log.Fatal(ctx, "error setting up OpenTelemetry - hint: ensure OTEL_EXPORTER_OTLP_ENDPOINT is set", oErr)
return oErr
}
// Handle shutdown properly so nothing leaks.
defer func() {
err = goErrors.Join(err, otelShutdown(context.Background()))
}()
}
// Get API version from its URL
apiRouterVersion, err := helpers.GetAPIRouterVersion(cfg.APIRouterURL)
if err != nil {
return err
}
// Healthcheck version Info
versionInfo, err := health.NewVersionInfo(
BuildTime,
GitCommit,
Version,
)
if err != nil {
log.Error(ctx, "failed to create service version information", err)
return err
}
router := mux.NewRouter().StrictSlash(true)
if cfg.OtelEnabled {
router.Use(otelmux.Middleware(cfg.OTServiceName))
}
apiRouterCli := apihealthcheck.NewClient("api-router", cfg.APIRouterURL)
populationClient, err := population.NewWithHealthClient(apiRouterCli)
if err != nil {
return fmt.Errorf("failed to create population API client: %w", err)
}
f := filter.NewWithHealthClient(apiRouterCli)
zc := zebedee.NewWithHealthClient(apiRouterCli)
dc := dataset.NewWithHealthClient(apiRouterCli)
fc := files.NewWithHealthClient(apiRouterCli)
tc := topic.NewWithHealthClient(apiRouterCli)
pc := populationClient
fc.Version = "v1"
healthcheck := health.New(versionInfo, cfg.HealthCheckCriticalTimeout, cfg.HealthCheckInterval)
if err = registerCheckers(ctx, &healthcheck, apiRouterCli); err != nil {
os.Exit(1)
}
// Initialise render client, routes and initialise localisations bundles
rend := render.NewWithDefaultClient(assets.Asset, assets.AssetNames, cfg.PatternLibraryAssetsPath, cfg.SiteDomain)
// Enable profiling endpoint for authorised users
if cfg.EnableProfiler {
middlewareChain := alice.New(profileMiddleware(cfg.PprofToken)).Then(http.DefaultServeMux)
router.PathPrefix("/debug").Handler(middlewareChain)
}
// Initialise caching
cacheList := &cache.List{}
cacheList.Navigation, err = cache.NewNavigationCache(ctx, &cfg.CacheNavigationUpdateInterval)
if err != nil {
log.Error(ctx, "failed to create navigation cache", err, log.Data{"update_interval": cfg.CacheNavigationUpdateInterval})
return err
}
for _, lang := range cfg.SupportedLanguages {
navigationlangKey := cacheList.Navigation.GetCachingKeyForNavigationLanguage(lang)
cacheList.Navigation.AddUpdateFunc(navigationlangKey, cachePublic.UpdateNavigationData(ctx, cfg, lang, tc))
}
router.Path("/health").HandlerFunc(healthcheck.Handler)
if cfg.EnableMultivariate {
router.Path("/datasets/create").Methods("GET").HandlerFunc(handlers.CreateCustomDataset(pc, zc, rend, *cfg, apiRouterVersion))
router.Path("/datasets/create").Methods("POST").HandlerFunc(handlers.PostCreateCustomDataset(f))
router.Path("/datasets/create/filter-outputs/{filterOutputID}").Methods("GET").HandlerFunc(handlers.FilterOutput(zc, f, pc, dc, rend, *cfg, apiRouterVersion))
router.Path("/datasets/create/filter-outputs/{filterOutputID}").Methods("POST").HandlerFunc(handlers.CreateFilterFlexIDFromOutput(f))
}
router.Path("/datasets/{datasetID}").Methods("GET").HandlerFunc(handlers.EditionsList(dc, zc, rend, *cfg, apiRouterVersion))
router.Path("/datasets/{datasetID}/editions").Methods("GET").HandlerFunc(handlers.EditionsList(dc, zc, rend, *cfg, apiRouterVersion))
router.Path("/datasets/{datasetID}/editions/{editionID}").Methods("GET").HandlerFunc(handlers.FilterableLanding(dc, pc, rend, zc, *cfg, apiRouterVersion))
router.Path("/datasets/{datasetID}/editions/{edition}/versions").Methods("GET").HandlerFunc(handlers.VersionsList(dc, zc, rend, *cfg))
router.Path("/datasets/{datasetID}/editions/{editionID}/versions/{versionID}").Methods("GET").HandlerFunc(handlers.FilterableLanding(dc, pc, rend, zc, *cfg, apiRouterVersion))
router.Path("/datasets/{datasetID}/editions/{editionID}/versions/{versionID}").Methods("POST").HandlerFunc(handlers.CreateFilterFlexID(f, dc))
router.Path("/datasets/{datasetID}/editions/{editionID}/versions/{versionID}/filter").Methods("POST").HandlerFunc(handlers.CreateFilterID(f, dc))
router.Path("/datasets/{datasetID}/editions/{editionID}/versions/{versionID}/filter-outputs/{filterOutputID}").Methods("GET").HandlerFunc(handlers.FilterOutput(zc, f, pc, dc, rend, *cfg, apiRouterVersion))
router.Path("/datasets/{datasetID}/editions/{editionID}/versions/{versionID}/filter-outputs/{filterOutputID}").Methods("POST").HandlerFunc(handlers.CreateFilterFlexIDFromOutput(f))
router.Path("/datasets/{datasetID}/editions/{edition}/versions/{version}/metadata.txt").Methods("GET").HandlerFunc(handlers.MetadataText(dc, *cfg))
router.PathPrefix("/dataset/").Methods("GET").Handler(http.StripPrefix("/dataset/", handlers.DatasetPage(zc, rend, fc, cacheList)))
router.HandleFunc("/{uri:.*}", handlers.LegacyLanding(zc, dc, fc, rend, cacheList, *cfg))
log.Info(ctx, "Starting server", log.Data{"config": cfg})
// Start healthcheck tickers
healthcheck.Start(ctx)
// Start caching
go cacheList.Navigation.StartUpdates(ctx, make(chan error))
collectionIDMiddleware := dpnethandlers.CheckCookie(dpnethandlers.CollectionID)
accessTokenMiddleware := dpnethandlers.CheckCookie(dpnethandlers.UserAccess)
localeMiddleware := dpnethandlers.CheckHeader(dpnethandlers.Locale)
renderrorMiddleware := renderror.Handler(rend)
var middlewareChain http.Handler
if cfg.OtelEnabled {
otelMiddleware := otelhttp.NewMiddleware(cfg.OTServiceName)
middlewareChain = alice.New(collectionIDMiddleware, accessTokenMiddleware, localeMiddleware, renderrorMiddleware, otelMiddleware).Then(router)
} else {
middlewareChain = alice.New(collectionIDMiddleware, accessTokenMiddleware, localeMiddleware, renderrorMiddleware).Then(router)
}
s := dpnethttp.NewServer(cfg.BindAddr, middlewareChain)
s.HandleOSSignals = false
svcErrors := make(chan error, 1)
go func() {
if err := s.ListenAndServe(); err != nil {
svcErrors <- errors.Wrap(err, "failure in http listen and serve")
}
}()
// Block until a signal is called to shutdown application
select {
case err := <-svcErrors:
log.Error(ctx, "service error received", err)
case osSignal := <-signals:
log.Info(ctx, "quitting after os signal received", log.Data{"signal": osSignal})
}
log.Info(ctx, fmt.Sprintf("shutdown with timeout: %s", cfg.GracefulShutdownTimeout))
ctx, cancel := context.WithTimeout(context.Background(), cfg.GracefulShutdownTimeout)
var gracefulShutdown bool
go func() {
defer cancel()
var hasShutdownErrs bool
log.Info(ctx, "stop health checkers")
healthcheck.Stop()
// Stop caching
cacheList.Navigation.Close()
if err := s.Shutdown(ctx); err != nil {
log.Error(ctx, "failed to gracefully shutdown http server", err)
hasShutdownErrs = true
}
if !hasShutdownErrs {
gracefulShutdown = true
}
}()
// wait for timeout or success (via cancel)
<-ctx.Done()
if ctx.Err() == context.DeadlineExceeded {
log.Warn(ctx, "context deadline exceeded", log.FormatErrors([]error{ctx.Err()}))
return err
}
if !gracefulShutdown {
err = errors.New("failed to shutdown gracefully")
log.Error(ctx, "failed to shutdown gracefully ", err)
return err
}
log.Info(ctx, "graceful shutdown complete", log.Data{"context": ctx.Err()})
return nil
}
func registerCheckers(ctx context.Context, h *health.HealthCheck, apiRouterCli *apihealthcheck.Client) (err error) {
hasErrors := false
if err = h.AddCheck("API router", apiRouterCli.Checker); err != nil {
hasErrors = true
log.Error(ctx, "failed to add API router health checker", err)
}
if hasErrors {
return errors.New("Error(s) registering checkers for healthcheck")
}
return nil
}
// profileMiddleware to validate auth token before accessing endpoint
func profileMiddleware(token string) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
pprofToken := req.Header.Get("Authorization")
if pprofToken == "Bearer " || pprofToken != "Bearer "+token {
log.Error(ctx, "invalid auth token", errors.New("invalid auth token"))
w.WriteHeader(404)
return
}
log.Info(ctx, "accessing profiling endpoint")
h.ServeHTTP(w, req)
})
}
}