This repository has been archived by the owner on Feb 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
91 lines (71 loc) · 1.81 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
82
83
84
85
86
87
88
89
90
91
package main
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/gookit/color"
"github.com/inhere/go-gin-skeleton/web"
// boot and init some services(log, cache, eureka)
"github.com/inhere/go-gin-skeleton/app"
"github.com/inhere/go-gin-skeleton/model/mongo"
"github.com/inhere/go-gin-skeleton/model/myrds"
"github.com/inhere/go-gin-skeleton/model/mysql"
)
func init() {
var err error
app.Bootstrap("./config")
// - redis, mongo, mysql connection
err = myrds.InitRedis()
checkError("Rds init error:", err)
err = mysql.InitMysql()
checkError("Db init error:", err)
err = mongo.InitMongo()
checkError("Mgo init error:", err)
// initEurekaService()
web.InitServer()
}
func main() {
listenSignals()
// init services
log.Printf("======================== Begin Running(PID: %d) ========================", os.Getpid())
web.Run()
}
func checkError(prefix string, err error) {
if err != nil {
color.Error.Println(prefix, err.Error())
os.Exit(2)
}
}
// listenSignals Graceful start/stop server
func listenSignals() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
go handleSignals(sigChan)
}
// handleSignals handle process signal
func handleSignals(c chan os.Signal) {
log.Print("Notice: System signal monitoring is enabled(watch: SIGINT,SIGTERM,SIGQUIT)\n")
switch <-c {
case syscall.SIGINT:
fmt.Println("\nShutdown by Ctrl+C")
case syscall.SIGTERM: // by kill
fmt.Println("\nShutdown quickly")
case syscall.SIGQUIT:
fmt.Println("\nShutdown gracefully")
// do graceful shutdown
}
// sync logs
_ = app.Logger.Sync()
_ = mysql.Close()
_ = myrds.ClosePool()
mongo.Close()
// unregister from eureka
// erkServer.Unregister()
// 等待一秒
time.Sleep(1e9 / 2)
color.Info.Println("\n GoodBye...")
os.Exit(0)
}