-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
81 lines (70 loc) · 2.15 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"context"
"flag"
"net/http"
"os"
"os/signal"
"syscall"
"time"
log "github.com/sirupsen/logrus"
"github.com/yasszu/go-jwt-auth/infrastructure/jwt"
"github.com/yasszu/go-jwt-auth/infrastructure/persistence"
"github.com/yasszu/go-jwt-auth/interfaces/handler"
"github.com/yasszu/go-jwt-auth/interfaces/router"
"github.com/yasszu/go-jwt-auth/pkg/conf"
"github.com/yasszu/go-jwt-auth/pkg/postgres"
)
func init() {
log.SetLevel(log.DebugLevel)
}
func main() {
var wait time.Duration
flag.DurationVar(&wait, "graceful-timeout", time.Second*15, "the duration for which the server gracefully wait for existing connections to finish - e.g. 15s or 1m")
flag.Parse()
// Establish DB connection
conn, err := postgres.NewConn()
if err != nil {
panic(err)
}
accountRepository := persistence.NewAccountRepository(conn)
jwtService := jwt.NewService()
h := handler.NewHandler(conn, accountRepository, jwtService)
r := router.NewRouter(h)
srv := &http.Server{
Addr: conf.Server.Addr(),
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: r,
}
// Run our server in a goroutine so that it doesn't block.
go func() {
log.Infof(" ⇨ http server started on %s", conf.Server.Addr())
log.Infof(" ⇨ graceful timeout: %s", wait)
if err = srv.ListenAndServe(); err != nil {
panic(err)
}
}()
c := make(chan os.Signal, 1)
// We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C)`
// SIGKILL, SIGQUIT or SIGTERM (Ctrl+/) will not be caught.
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
// Block until we receive our signal.
<-c
log.Info("received stop signal")
// Create a deadline to wait for.
ctx, cancel := context.WithTimeout(context.Background(), wait)
defer func() {
log.Info("cancel")
cancel()
}()
// Doesn't block if no connections, but will otherwise wait
// until the timeout deadline.
_ = srv.Shutdown(ctx)
// Optionally, you could run srv.Shutdown in a goroutine and block on
// <-ctx.Done() if your application should wait for other services
// to finalize based on context cancellation.
log.Infof(" ⇨ shutting down")
os.Exit(0)
}