-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
66 lines (58 loc) · 1.36 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
package main
import (
"context"
"log"
"os"
"os/signal"
"strconv"
"time"
"github.com/SignorMercurio/cncamp_homework/httpserver"
"github.com/SignorMercurio/cncamp_homework/logger"
"go.uber.org/zap"
)
func main() {
logger, err := logger.NewLogger()
if err != nil {
log.Fatalf("Failed to create logger: %s", err)
}
defer logger.Sync()
undo := zap.ReplaceGlobals(logger)
defer undo()
sugar := zap.S()
if len(os.Args) != 2 {
sugar.Fatalf("Usage: %s [listen address]", os.Args[0])
}
addr := os.Args[1]
sugar.Debugw("Creating new server...", "address", addr)
srv := httpserver.NewServer(addr)
go func() {
if err := srv.ListenAndServe(); err != nil {
sugar.Error(err)
}
}()
// Graceful termination
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
sugar.Debug("Received SIGTERM, gracefully terminating...")
// from ConfigMap httpserver-config
graceTimeout, err := strconv.Atoi(os.Getenv("GRACE_TIMEOUT"))
if err != nil {
graceTimeout = 30
sugar.Warnw(
"Failed to read GRACE_TIMEOUT from env, using default",
"default",
graceTimeout,
)
}
sugar.Debugw(
"Set graceful termination timeout successfully",
"timeout",
graceTimeout,
)
timeout := time.Second * time.Duration(graceTimeout)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
srv.Shutdown(ctx)
sugar.Info("Shutting down...")
}