-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (92 loc) · 1.93 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/signal"
"syscall"
"github.com/coinsky/trade-server/server"
"github.com/coinsky/trade-server/utils"
toml "github.com/pelletier/go-toml"
log "github.com/sirupsen/logrus"
)
var (
newFlag *flag.FlagSet
help bool
cfgFile string
version bool
)
var ReleaseVersion = "v0.2.3-alpha"
func init() {
newFlag = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
newFlag.BoolVar(&help, "h", false, "display this help")
newFlag.StringVar(&cfgFile, "c", "config.toml", "config file")
newFlag.BoolVar(&version, "v", false, "show the trade-server version")
newFlag.Usage = usage
}
func main() {
if !isBeginService() {
return
}
if svrConfig := initConfigAndLog(); svrConfig != nil {
startService(svrConfig)
}
log.Info("trade-server exit")
}
func isBeginService() bool {
if err := newFlag.Parse(os.Args[1:]); err != nil {
return false
}
if help {
newFlag.Usage()
return false
}
if version {
fmt.Println(ReleaseVersion)
return false
}
return true
}
func initConfigAndLog() *toml.Tree {
svrConfig, err := loadConfigFile(cfgFile)
if err != nil {
fmt.Printf("Load config file fail:%v\n", err)
return nil
}
if err = utils.InitLog(svrConfig); err != nil {
fmt.Printf("Init log fail:%v\n", err)
os.Exit(1)
}
return svrConfig
}
func loadConfigFile(cfgFile string) (*toml.Tree, error) {
if _, err := os.Stat(cfgFile); err != nil {
return nil, err
}
bz, err := ioutil.ReadFile(cfgFile)
if err != nil {
return nil, err
}
tree, err := toml.LoadBytes(bz)
if err != nil {
return nil, err
}
return tree, nil
}
func startService(svrConfig *toml.Tree) {
if svr := server.NewServer(svrConfig, nil); svr != nil {
svr.Start(svrConfig)
waitForSignal()
svr.Stop()
}
}
func waitForSignal() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
<-c
}
func usage() {
_, _ = fmt.Println("Options:")
newFlag.PrintDefaults()
}