-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
38 lines (31 loc) · 805 Bytes
/
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
package main
import (
"context"
"os"
"os/signal"
"github.com/rs/zerolog/log"
)
func contextWithLogger() context.Context {
subLogger := log.With().Logger()
return subLogger.WithContext(context.Background())
}
func main() {
ctx := contextWithLogger()
configuration := system.ConfigFromEnv()
server := system.ServerFromConfig(configuration)
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, os.Kill)
go func() {
s := <-signals
log.Ctx(ctx).Info().Str("signal", s.String()).Msg("signal received. Stop server")
err := server.Stop(ctx)
if err != nil {
log.Ctx(ctx).Err(err).Msg("stopping server failed")
}
}()
if err := server.Run(ctx); err != nil {
log.Ctx(ctx).Err(err).Msg("server stopped")
} else {
log.Ctx(ctx).Info().Msg("server stopped")
}
}