-
Notifications
You must be signed in to change notification settings - Fork 2
/
fyleaf.go
103 lines (73 loc) · 1.81 KB
/
fyleaf.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
94
95
96
97
98
99
package fyleaf
import (
"github.com/wudaoluo/fyleaf/module"
"github.com/wudaoluo/fyleaf/glog"
"github.com/wudaoluo/fyleaf/utils"
"github.com/wudaoluo/fyleaf/console"
"github.com/wudaoluo/fyleaf/conf"
"os"
"os/signal"
"syscall"
"flag"
"fmt"
)
type argStruct struct {
version bool
configfile string
configtype string
logv int
}
var arg = new(argStruct)
func init() {
flag.BoolVar(&arg.version,"version",false,"print version")
flag.StringVar(&arg.configfile,"c","server.ini","specify config file")
flag.StringVar(&arg.configtype,"type","ini","ini|json")
flag.IntVar(&arg.logv,"V",3,"glog.V的级别")
}
//负责整个框架初始化
func Run(mods ...module.Module) {
if !flag.Parsed() {
panic("flag还没有初始化,请在入口添加flag.Parse()")
}
if arg.version {
utils.PrintVersion()
os.Exit(0)
}
// 配置文件初始化
cfg := conf.GetInstance()
cfg.ParseConf(arg.configfile,arg.configtype)
//日志初始化
glog.Init(cfg.Cfg.LogLevel,cfg.Cfg.LogPath,glog.Level(arg.logv))
defer glog.Flush()
//start
glog.Info(errstr.I_StartInfo)
//日志清理初始化 NewGlogClear(path string,t ...time.Duration)
//TODO 以后改成Init形式
utils.NewGlogClear(cfg.Cfg.LogPath)
// module
for i := 0; i < len(mods); i++ {
module.Register(mods[i])
}
module.Init()
// console
console.Init()
waitSignal()
// close
console.Destroy()
module.Destroy()
}
//阻塞,只有执行信号才执行
func waitSignal() {
osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM)
sig := <-osSignals
glog.Info(errstr.I_StopInfo, sig)
}
var errstr struct{
I_StartInfo string
I_StopInfo string
}
func init() {
errstr.I_StartInfo = fmt.Sprintf("fyleaf %v starting up",utils.ReturnVersion())
errstr.I_StopInfo = "Leaf closing down signal:"
}