-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmain.go
56 lines (44 loc) · 1.1 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"
"database/sql"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/kkgo-software-engineering/workshop/config"
"github.com/kkgo-software-engineering/workshop/router"
"go.uber.org/zap"
_ "github.com/lib/pq"
)
func main() {
cfg := config.New().All()
logger, err := zap.NewProduction()
if err != nil {
log.Fatal(err)
}
sql, err := sql.Open("postgres", cfg.DBConnection)
if err != nil {
logger.Fatal("unable to configure database", zap.Error(err))
}
e := router.RegRoute(cfg, logger, sql)
addr := fmt.Sprintf("%s:%d", cfg.Server.Hostname, cfg.Server.Port)
go func() {
err := e.Start(addr)
if err != nil && err != http.ErrServerClosed {
logger.Fatal("unexpected shutdown the server", zap.Error(err))
}
logger.Info("gracefully shutdown the server")
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
gCtx := context.Background()
ctx, cancel := context.WithTimeout(gCtx, 10*time.Second)
defer cancel()
if err := e.Shutdown(ctx); err != nil {
logger.Fatal("unexpected shutdown the server", zap.Error(err))
}
}