-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
150 lines (122 loc) · 4.16 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
package main
import (
"context"
"errors"
"os"
"os/signal"
"time"
cacheHelper "github.com/ONSdigital/dp-frontend-cache-helper/pkg/navigation/helper"
"github.com/ONSdigital/dp-frontend-feedback-controller/assets"
"github.com/ONSdigital/dp-frontend-feedback-controller/config"
"github.com/ONSdigital/dp-frontend-feedback-controller/routes"
health "github.com/ONSdigital/dp-healthcheck/healthcheck"
dpotelgo "github.com/ONSdigital/dp-otel-go"
render "github.com/ONSdigital/dp-renderer/v2"
"github.com/ONSdigital/dp-renderer/v2/middleware/renderror"
"github.com/ONSdigital/go-ns/server"
"github.com/ONSdigital/log.go/v2/log"
"github.com/gorilla/mux"
"github.com/justinas/alice"
"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)
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() {
log.Namespace = "dp-frontend-feedback-controller"
cfg, err := config.Get()
ctx := context.Background()
if err != nil {
log.Error(ctx, "unable to retrieve service configuration", err)
os.Exit(1)
}
log.Info(ctx, "got service configuration", log.Data{"config": cfg})
if cfg.OtelEnabled {
// Set up OpenTelemetry
otelConfig := dpotelgo.Config{
OtelServiceName: cfg.OTServiceName,
OtelExporterOtlpEndpoint: cfg.OTExporterOTLPEndpoint,
OtelBatchTimeout: cfg.OTBatchTimeout,
}
otelShutdown, err := dpotelgo.SetupOTelSDK(ctx, otelConfig)
if err != nil {
log.Error(ctx, "error setting up OpenTelemetry - hint: ensure OTEL_EXPORTER_OTLP_ENDPOINT is set", err)
}
// Handle shutdown properly so nothing leaks.
defer func() {
err = errors.Join(err, otelShutdown(context.Background()))
}()
}
versionInfo, healthErr := health.NewVersionInfo(
BuildTime,
GitCommit,
Version,
)
if healthErr != nil {
log.Error(ctx, "failed to retrieve health check version", healthErr)
return
}
r := mux.NewRouter()
if cfg.OtelEnabled {
r.Use(otelmux.Middleware(cfg.OTServiceName))
}
healthcheck := health.New(versionInfo, cfg.HealthCheckCriticalTimeout, cfg.HealthCheckInterval)
if err = registerCheckers(ctx, &healthcheck); err != nil {
os.Exit(1)
}
cacheConfig := cacheHelper.Config{
APIRouterURL: cfg.APIRouterURL,
CacheUpdateInterval: cfg.CacheUpdateInterval,
EnableNewNavBar: cfg.EnableNewNavBar,
EnableCensusTopicSubsection: cfg.EnableCensusTopicSubsection,
CensusTopicID: cfg.CensusTopicID,
IsPublishingMode: cfg.IsPublishing,
Languages: cfg.SupportedLanguages,
ServiceAuthToken: cfg.ServiceAuthToken,
}
svcErrors := make(chan error, 0)
cacheService, err := cacheHelper.Init(ctx, cacheConfig)
cacheService.RunUpdates(ctx, svcErrors)
//nolint:typecheck
rend := render.NewWithDefaultClient(assets.Asset, assets.AssetNames, cfg.PatternLibraryAssetsPath, cfg.SiteDomain)
middleware := []alice.Constructor{
renderror.Handler(rend),
}
newAlice := alice.New(middleware...).Then(r)
routes.Setup(ctx, r, cfg, rend, healthcheck, cacheService)
healthcheck.Start(ctx)
var s *server.Server
if cfg.OtelEnabled {
otelHandler := otelhttp.NewHandler(newAlice, "/")
s = server.New(cfg.BindAddr, otelHandler)
} else {
s = server.New(cfg.BindAddr, newAlice)
}
s.HandleOSSignals = false
log.Info(ctx, "Starting server", log.Data{"config": cfg})
go func() {
if err := s.ListenAndServe(); err != nil {
log.Error(ctx, "failed to start http listen and serve", err)
return
}
}()
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, os.Kill)
<-stop
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
log.Info(ctx, "shutting service down gracefully")
defer cancel()
if err := s.Server.Shutdown(ctx); err != nil {
log.Error(ctx, "failed to shutdown http server", err)
}
}
func registerCheckers(ctx context.Context, h *health.HealthCheck) (err error) {
// TODO ADD HEALTH CHECKS HERE
return
}