forked from happyxie/open-moac-pool
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
107 lines (90 loc) · 2.06 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
// +build go1.9
package main
import (
"encoding/json"
"log"
"math/rand"
"os"
"path/filepath"
"runtime"
"time"
"github.com/yvasiyarov/gorelic"
"github.com/happyxie/open-moac-pool/api"
"github.com/happyxie/open-moac-pool/payouts"
"github.com/happyxie/open-moac-pool/proxy"
"github.com/happyxie/open-moac-pool/storage"
)
var cfg proxy.Config
var backend *storage.RedisClient
func startProxy() {
s := proxy.NewProxy(&cfg, backend)
s.Start()
}
func startApi() {
s := api.NewApiServer(&cfg.Api, backend)
s.Start()
}
func startBlockUnlocker() {
u := payouts.NewBlockUnlocker(&cfg.BlockUnlocker, backend)
u.Start()
}
func startPayoutsProcessor() {
u := payouts.NewPayoutsProcessor(&cfg.Payouts, backend)
u.Start()
}
func startNewrelic() {
if cfg.NewrelicEnabled {
nr := gorelic.NewAgent()
nr.Verbose = cfg.NewrelicVerbose
nr.NewrelicLicense = cfg.NewrelicKey
nr.NewrelicName = cfg.NewrelicName
nr.Run()
}
}
func readConfig(cfg *proxy.Config) {
configFileName := "config.json"
if len(os.Args) > 1 {
configFileName = os.Args[1]
}
configFileName, _ = filepath.Abs(configFileName)
log.Printf("Loading config: %v", configFileName)
configFile, err := os.Open(configFileName)
if err != nil {
log.Fatal("File error: ", err.Error())
}
defer configFile.Close()
jsonParser := json.NewDecoder(configFile)
if err := jsonParser.Decode(&cfg); err != nil {
log.Fatal("Config error: ", err.Error())
}
}
func main() {
readConfig(&cfg)
rand.Seed(time.Now().UnixNano())
if cfg.Threads > 0 {
runtime.GOMAXPROCS(cfg.Threads)
log.Printf("Running with %v threads", cfg.Threads)
}
startNewrelic()
backend = storage.NewRedisClient(&cfg.Redis, cfg.Coin)
pong, err := backend.Check()
if err != nil {
log.Printf("Can't establish connection to backend: %v", err)
} else {
log.Printf("Backend check reply: %v", pong)
}
if cfg.Proxy.Enabled {
go startProxy()
}
if cfg.Api.Enabled {
go startApi()
}
if cfg.BlockUnlocker.Enabled {
go startBlockUnlocker()
}
if cfg.Payouts.Enabled {
go startPayoutsProcessor()
}
quit := make(chan bool)
<-quit
}