This repository has been archived by the owner on Mar 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
93 lines (81 loc) · 2.53 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
// percona-everest-backend
// Copyright (C) 2023 Percona LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package main is the entry point of the service.
package main
import (
"context"
"errors"
"net/http"
"os"
"os/signal"
"time"
"github.com/go-logr/zapr"
"go.uber.org/zap"
ctrlruntimelog "sigs.k8s.io/controller-runtime/pkg/log"
"github.com/percona/percona-everest-backend/api"
"github.com/percona/percona-everest-backend/cmd/config"
"github.com/percona/percona-everest-backend/pkg/logger"
)
func main() {
logger := logger.MustInitLogger()
defer logger.Sync() //nolint:errcheck
l := logger.Sugar()
// This is required because controller-runtime requires a logger
// to be set within 30 seconds of the program initialization.
log := zapr.NewLogger(logger)
ctrlruntimelog.SetLogger(log)
c, err := config.ParseConfig()
if err != nil {
l.Fatalf("Failed parsing config: %+v", err)
}
if !c.Verbose {
logger = logger.WithOptions(zap.IncreaseLevel(zap.InfoLevel))
l = logger.Sugar()
}
l.Debug("Debug logging enabled")
server, err := api.NewEverestServer(c, l)
if err != nil {
l.Fatalf("Error creating Everest Server\n: %s", err)
}
go func() {
err := server.Start()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
l.Fatal(err)
}
}()
tCtx, tCancel := context.WithCancel(context.Background())
if !c.DisableTelemetry {
// To prevent leaking test data to prod,
// the prod TelemetryURL is set for the release builds during the build time.
// The dev TelemetryURL is set only when running `make run-debug`.
if c.TelemetryURL != "" {
go server.RunTelemetryJob(tCtx, c)
} else {
l.Info("Telemetry is not running, the TELEMETRY_URL is not set")
}
}
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
tCancel()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
l.Error(errors.Join(err, errors.New("could not shut down Everest")))
} else {
l.Info("Everest shut down")
}
l.Info("Exiting")
}