-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
56 lines (47 loc) · 1.16 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
package main
import (
"context"
"flag"
"log"
"os"
"os/signal"
"pismo/connection"
"pismo/utils"
"pismo/webservice"
"syscall"
"time"
// Used pg drive on sqlx
_ "github.com/lib/pq"
)
func init() {
utils.Load()
}
func main() {
var wait time.Duration
flag.DurationVar(&wait, "graceful-timeout", time.Second*3, "the duration for which the server gracefully wait for existing connections to finish - e.g. 3s or 1m")
flag.Parse()
cancel := start()
defer shutdown(cancel, wait)
waitShutdown()
}
func start() (cancel context.CancelFunc) {
ctx, cancel := context.WithCancel(context.Background())
webservice.NewRouter()
connection.Load(ctx)
return
}
func shutdown(cancel context.CancelFunc, wait time.Duration) {
cancel()
// Create a deadline to wait for.
ctx, cancel := context.WithTimeout(context.Background(), wait)
defer cancel()
connection.Close()
webservice.Shutdown(ctx)
}
// waitShutdown waits until is going to die
func waitShutdown() {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
s := <-sigc
log.Printf("[ WaitShutdown ] signal received [%v] canceling everything", s)
}