forked from laohanlinux/riot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
riot.go
87 lines (75 loc) · 1.65 KB
/
riot.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
package main
import (
"flag"
"io/ioutil"
"net"
"net/http"
_ "net/http/pprof"
"runtime"
"sync"
"syscall"
"github.com/laohanlinux/riot/api"
"github.com/laohanlinux/riot/cluster"
"github.com/laohanlinux/riot/config"
"github.com/laohanlinux/riot/platform"
"github.com/hashicorp/raft"
log "github.com/laohanlinux/utils/gokitlog"
"github.com/laohanlinux/utils/netrpc"
)
func main() {
var (
cfgPath string
err error
data []byte
cfg *config.Configure
rc = raft.DefaultConfig()
c *cluster.Cluster
)
flag.StringVar(&cfgPath, "c", "", "configure path")
flag.Parse()
var action = flag.Arg(0)
runtime.GOMAXPROCS(runtime.NumCPU())
if cfgPath == "" {
panic("No config path")
}
if data, err = ioutil.ReadFile(cfgPath); err != nil {
panic(err)
}
if cfg, err = config.NewConfig(string(data)); err != nil {
panic(err)
}
var gGroup sync.WaitGroup
// Init raft server
// set snapshot
if action == "dev" {
rc.SnapshotThreshold = 10
rc.TrailingLogs = 10
}
c = cluster.NewCluster(cfg, rc)
// register monitor
if action == "dev" {
go func() {
http.ListenAndServe(cfg.SMC.Addr+":"+cfg.SMC.Port, nil)
}()
}
// Init rpc server
gGroup.Add(1)
go func() {
defer gGroup.Done()
addr := cfg.RpcC.Addr + ":" + cfg.RpcC.Port
service := api.NewAPIService(api.NewMiniAPI(c), api.NewAdmAPI(c))
l, err := net.Listen("tcp", addr)
if err != nil {
panic(err)
}
defer l.Close()
srv := netrpc.NewServer()
srv.Register(service)
srv.Register(&netrpc.HealthCheck{})
log.Info("Start rpc server successfully")
srv.Accept(l)
}()
// regist the signal
platform.RegistSignal(syscall.SIGINT)
gGroup.Wait()
}