-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
52 lines (46 loc) · 1.3 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
package main
import (
"os"
"os/signal"
"syscall"
"github.com/sirupsen/logrus"
"github.com/xcoulon/go-url-shortener/configuration"
"github.com/xcoulon/go-url-shortener/connection"
"github.com/xcoulon/go-url-shortener/server"
"github.com/xcoulon/go-url-shortener/storage"
"github.com/jinzhu/gorm"
)
func main() {
// load the logging configuration and init the logger
// logrus.SetFormatter(&logrus.JSONFormatter{})
config := configuration.New()
// load the configuration and init the storage
err := connection.SetupUUIDExtension(config)
if err != nil {
logrus.Fatalf("failed to start: %s", err.Error())
}
// load the configuration and init the storage
db, err := connection.NewUserConnection(config)
if err != nil {
logrus.Fatalf("failed to start: %s", err.Error())
}
repository := storage.New(db)
// handle shutdown
go handleShutdown(db)
s := server.New(repository)
// listen and serve on 0.0.0.0:8080
s.Start(":8080")
}
func handleShutdown(db *gorm.DB) {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
// handle ctrl+c event here
// for example, close database
logrus.Warn("Closing DB connection before complete shutdown")
err := db.Close()
if err != nil {
logrus.Errorf("error while closing the connection to the database: %v", err)
}
os.Exit(0)
}