-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
97 lines (83 loc) · 2.09 KB
/
main.go
File metadata and controls
97 lines (83 loc) · 2.09 KB
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
package main
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"time"
"github.com/deezer/groroti/internal/middlewares"
"github.com/deezer/groroti/internal/model"
"github.com/deezer/groroti/internal/services"
"github.com/deezer/groroti/internal/staticEmbed"
"github.com/rs/zerolog/log"
)
var (
Version string
otelShutdown func(context.Context) error
)
func main() {
if err := run(); err != nil {
log.Fatal().Err(err).Msg("")
}
}
func run() (err error) {
// Get config first
configRepository, err := services.GetConfig()
if err != nil {
return err
}
// Handle SIGINT (CTRL+C) gracefully.
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
// Set up OpenTelemetry.
otelShutdown, err = middlewares.SetupOTelSDK(ctx, configRepository)
if err != nil {
return
}
// Handle shutdown properly so nothing leaks.
defer func() {
if otelShutdown != nil {
err = errors.Join(err, otelShutdown(context.Background()))
}
}()
services.Version = Version
log.Info().Msgf("GroROTI version v%s", Version)
sqliteDatabase := model.InitDatabase()
defer sqliteDatabase.Close()
// load embedded gotemplates in embed.Templates
err = staticEmbed.LoadTemplates()
if err != nil {
return fmt.Errorf("couldn't load templates : %s", err.Error())
}
services.Register()
addr := configRepository.BuildServerAddr()
log.Info().Msgf("Start listening on %s", addr)
// Start HTTP server.
srv := &http.Server{
Addr: addr,
BaseContext: func(_ net.Listener) context.Context { return ctx },
ReadTimeout: 10 * time.Second,
WriteTimeout: 30 * time.Second,
Handler: nil, // uses http.DefaultServeMux
}
srvErr := make(chan error, 1)
go func() {
srvErr <- srv.ListenAndServe()
}()
// Wait for interruption.
select {
case err = <-srvErr:
// Error when starting HTTP server.
return
case <-ctx.Done():
// Wait for first CTRL+C.
// Stop receiving signal notifications as soon as possible.
stop()
}
// When Shutdown is called, ListenAndServe immediately returns ErrServerClosed.
err = srv.Shutdown(context.Background())
return
}