-
Notifications
You must be signed in to change notification settings - Fork 2
/
gatego.go
68 lines (58 loc) · 1.62 KB
/
gatego.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
package gatego
import (
"context"
"fmt"
"time"
"github.com/hvuhsg/gatego/internal/config"
"github.com/hvuhsg/gatego/internal/contextvalues"
"github.com/hvuhsg/gatego/pkg/monitor"
)
const serviceName = "gatego"
type GateGo struct {
config config.Config
monitor *monitor.Monitor
ctx context.Context
}
func New(ctx context.Context, config config.Config, version string) *GateGo {
ctx = contextvalues.AddVersionToContext(ctx, version)
return &GateGo{config: config, ctx: ctx}
}
func (gg GateGo) Run() error {
useOtel := gg.config.OTEL != nil
if useOtel {
otelConfig := otelConfig{
ServiceName: serviceName,
SampleRatio: gg.config.OTEL.SampleRatio,
CollectorTimeout: time.Second * 5, // TODO: Add to config
TraceCollectorEndpoint: gg.config.OTEL.Endpoint,
MetricCollectorEndpoint: gg.config.OTEL.Endpoint,
LogsCollectorEndpoint: gg.config.OTEL.Endpoint,
}
shutdown, err := setupOTelSDK(gg.ctx, otelConfig)
if err != nil {
return err
}
defer shutdown(context.Background())
}
// Create checks start monitoring
healthChecks := createMonitorChecks(gg.config.Services)
gg.monitor = monitor.New(time.Second*5, healthChecks...)
gg.monitor.Start()
server, err := newServer(gg.ctx, gg.config, useOtel)
if err != nil {
return err
}
defer server.Shutdown(gg.ctx)
serveErrChan, err := server.serve(gg.config.TLS.CertFile, gg.config.TLS.KeyFile)
if err != nil {
return err
}
// Wait for interruption.
select {
case err = <-serveErrChan:
return err
case <-gg.ctx.Done():
fmt.Println("\nShutting down...")
return server.Shutdown(context.Background())
}
}