-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (48 loc) · 1.03 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
package main
import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/boichique/avito-test-task/internal/config"
"github.com/boichique/avito-test-task/internal/server"
"golang.org/x/exp/slog"
)
const (
gracefulTimeout = 10 * time.Second
)
func main() {
cfg, err := config.NewConfig()
failOnError(err, "parse config")
srv, err := server.New(context.Background(), cfg)
failOnError(err, "create server")
go func() {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT)
<-sigCh
slog.Info("received interrupt signal. Shutting down...")
ctx, cancel := context.WithTimeout(context.Background(), gracefulTimeout)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
slog.Error(
"shutdown server",
"error", err,
)
}
}()
if err := srv.Start(); err != http.ErrServerClosed {
slog.Error(
"server stopped",
"error", err,
)
os.Exit(1)
}
}
func failOnError(err error, message string) {
if err != nil {
slog.Error("%s: %s", message, err)
os.Exit(1)
}
}