-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
76 lines (60 loc) · 1.62 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
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/zhanben/go_site/app"
"github.com/zhanben/go_site/tool/config"
"github.com/zhanben/go_site/tool/db"
"github.com/zhanben/go_site/tool/log"
"go.uber.org/zap"
)
// @title Go-site Example API
// @version 1.0
// @description This is a sample server Petstore server.
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email [email protected]
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host 127.0.0.1
// @BasePath ""
func main() {
//Read config file
err := config.ParseConfig()
if err != nil {
panic(fmt.Errorf("Failed to read config file: %s \n", err))
}
//Init log
log.InitLog()
//Init db connection
dbConn, err := db.InitDbConn()
if err != nil {
fmt.Printf("connect db error:%s", err)
panic("connect db error!")
}
log.Logger.Info("Db init successful!")
exit := make(chan os.Signal,10) //初始化一个channel
signal.Notify(exit, syscall.SIGINT, syscall.SIGTERM) //notify方法用来监听收到的信号
//start http sever
go func() {
startServer(dbConn)
}()
sig := <-exit
log.Logger.Info("main function return, %s", sig.String())
}
func startServer(dbConn *db.DbConn) error {
exit := make(chan os.Signal, 10)
signal.Notify(exit, syscall.SIGINT, syscall.SIGTERM)
s, err := app.NewServer(dbConn, log.Logger)
if err != nil {
log.Logger.Panic("init http server failed.")
}
err = s.HttpServer.ListenAndServe()
if err != nil {
log.Logger.Panic("start http server error:%s", zap.Error(err))
}
return nil
}