-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
52 lines (41 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
package main
import (
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
log "github.com/sirupsen/logrus"
"adrianfinantyo.com/adrianfinantyo/go-shorter/model"
"adrianfinantyo.com/adrianfinantyo/go-shorter/router"
"adrianfinantyo.com/adrianfinantyo/go-shorter/util"
)
func main() {
config := util.LoadConfig()
dbConn := util.InitDBConnection(config)
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stdout)
// Handle system interrupt
channel := make(chan os.Signal, 1)
signal.Notify(channel, os.Interrupt, syscall.SIGTERM)
go systemInterruptHandler(channel, dbConn)
// Run on exit
defer runOnExit(dbConn)
// Initialize router and server
routes := router.InitRouter(config, dbConn)
server := &http.Server{
Addr: config.AppHost + ":" + config.AppPort,
Handler: routes,
}
server.ListenAndServe()
}
func systemInterruptHandler(channel chan os.Signal, dbConn *model.DatabaseConnection) {
<-channel
fmt.Println("Interrupt signal received.")
runOnExit(dbConn)
os.Exit(0)
}
func runOnExit(dbConn *model.DatabaseConnection) {
util.RunOnExit(dbConn)
fmt.Println("Server is shutting down...")
}