-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
84 lines (73 loc) · 1.96 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
package main
import (
"encoding/json"
"etc-pool-admin/controller"
"etc-pool-admin/routers"
"etc-pool-admin/rpc"
"etc-pool-admin/storage"
"github.com/cihub/seelog"
"github.com/yvasiyarov/gorelic"
"net/http"
"os"
"path/filepath"
"runtime"
)
func readConfig(cfg *controller.Config) {
configFileName := "manage.json"
if len(os.Args) > 1 {
configFileName = os.Args[1]
}
configFileName, _ = filepath.Abs(configFileName)
seelog.Infof("Loading config: %v", configFileName)
configFile, err := os.Open(configFileName)
if err != nil {
seelog.Critical("File error: ", err.Error())
panic(err.Error())
}
defer configFile.Close()
jsonParser := json.NewDecoder(configFile)
if err := jsonParser.Decode(&cfg); err != nil {
seelog.Critical("Config error: ", err.Error())
panic(err.Error())
}
}
func startNewrelic(cfg controller.Config) {
if cfg.NewrelicEnabled {
nr := gorelic.NewAgent()
nr.Verbose = cfg.NewrelicVerbose
nr.NewrelicLicense = cfg.NewrelicKey
nr.NewrelicName = cfg.NewrelicName
nr.Run()
}
}
//TODO make a session manage util package
func main() {
//recover function
defer func() {
if r := recover(); r != nil {
seelog.Critical("Critical error, recover:", r)
}
}()
//load log config
logger, err := seelog.LoggerFromConfigAsFile("./logconfig.xml")
if err != nil {
seelog.Critical("load log config fail:", err)
panic(err)
}
seelog.ReplaceLogger(logger)
defer seelog.Flush()
readConfig(&controller.Conf)
if controller.Conf.Threads > 0 {
runtime.GOMAXPROCS(controller.Conf.Threads)
seelog.Infof("Running with %v threads", controller.Conf.Threads)
}
startNewrelic(controller.Conf)
controller.Backend = storage.NewRedisClient(&controller.Conf.Redis, controller.Conf.Coin)
controller.RpcClient = rpc.NewRPCClient(controller.Conf.Upstream.Url, controller.Conf.Upstream.Timeout)
//main function
err = http.ListenAndServe(controller.Conf.Listen, router.Routes)
if err != nil {
seelog.Critical("listen error:", err)
panic(err)
}
}