-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
63 lines (49 loc) · 1.29 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
package main
import (
"context"
"errors"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
const (
shutdownTimeout = 5 * time.Second
readHeaderTimeout = 20 * time.Second
)
func main() {
cfg, err := newConfig()
if err != nil {
log.Fatalf("error getting the config: %s", err)
}
router := http.NewServeMux()
server := &http.Server{
Addr: cfg.address,
Handler: router,
ReadHeaderTimeout: readHeaderTimeout,
}
appCtx, appCancel := context.WithCancel(context.Background())
router.Handle("/", newHandler(appCtx, cfg.webhookSecret, cfg.nodeAPI, cfg.walletPassword))
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
shutdownComplete := make(chan struct{}, 1)
go func() {
<-signals
log.Println("shutting down the server")
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatalf("error shutting down the server: %s", err)
}
close(shutdownComplete)
}()
log.Printf("starting the server on %s", cfg.address)
if err := server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("error starting the server: %s", err)
}
appCancel()
<-shutdownComplete
log.Println("server shutdown successfully")
}