-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (76 loc) · 2.29 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
92
93
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/mcoder2014/home_server/domain/service"
"github.com/mcoder2014/home_server/utils/log"
"github.com/mcoder2014/home_server/utils/routine"
"github.com/mcoder2014/home_server/config"
"github.com/mcoder2014/home_server/domain/db"
"github.com/mcoder2014/home_server/route"
"github.com/sirupsen/logrus"
)
func main() {
if err := log.Init(); err != nil {
panic(fmt.Errorf("log init error: %w", err))
}
cliConfig := GetConfig()
if err := config.InitGlobalConfig(cliConfig.ConfigPath); err != nil {
panic(fmt.Errorf("load Global config from:%v failed, err:%w", cliConfig.ConfigPath, err))
}
logrus.Infof("Load Config: %+v", config.Global())
routine.Init()
// 监听 ctrl c 信号
exitChan := make(chan os.Signal)
signal.Notify(exitChan, os.Interrupt, os.Kill, syscall.SIGTERM)
go exitHandle(exitChan)
// 链接数据库
if err := db.InitDatabase(config.Global().Mysql.MasterDB); err != nil {
panic(fmt.Errorf("connect to mysql failed. dsn is %v, err:%w ", config.Global().Mysql.MasterDB, err))
}
r := route.InitRoute()
port := cliConfig.Port
if port == -1 {
port = config.Global().Server.Port
}
logrus.Infof("will bind http server port on %v", port)
// init service
if err := service.Init(config.ConfigPtr(config.Global())); err != nil {
panic(err)
}
if err := r.Run(":" + strconv.Itoa(port)); err != nil {
panic(err)
}
defer routine.Wait()
}
type CliArgsConfig struct {
// 配置端口号
Port int
// 配置文件路径
ConfigPath string
}
// GetConfig 读取命令行配置
func GetConfig() *CliArgsConfig {
conf := &CliArgsConfig{}
flag.IntVar(&conf.Port, "port", -1, "http server 端口号,默认为空")
flag.StringVar(&conf.ConfigPath, "conf", "/etc/home_server/conf.yaml", "配置文件路径")
// 从arguments中解析注册的flag。必须在所有flag都注册好而未访问其值时执行。未注册却使用flag -help时,会返回ErrHelp。
flag.Parse()
logrus.Infof("Got Cli Args: %v", *conf)
return conf
}
func exitHandle(exitChan chan os.Signal) {
for {
select {
case sig := <-exitChan:
logrus.Infof("Get Signal: %v from sys, stop program", sig)
time.Sleep(1 * time.Second)
os.Exit(1) //如果ctrl+c 关不掉程序,使用os.Exit强行关掉
}
}
}