-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
83 lines (70 loc) · 1.44 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
package main
import (
"fmt"
"github.com/lonelycode/yzma/api"
"github.com/lonelycode/yzma/logger"
"github.com/lonelycode/yzma/oplog"
"github.com/lonelycode/yzma/peering"
"github.com/lonelycode/yzma/server"
"os"
"os/signal"
"sync"
"time"
)
var log = logger.GetLogger("yzma")
func info() {
msg := fmt.Sprintf("\nYzmaDB %v copyright Martin Buhr %v", Version, time.Now().Year())
line := ""
for i := 0; i < len(msg)-1; i++ {
line += "="
}
fmt.Println(msg)
fmt.Println(line)
fmt.Println()
}
func doJoin(svr *server.Server) {
for {
if svr.Ready() {
err := svr.Join([]string{*joinPtr})
if err != nil {
log.Error("join failed: ", err)
}
break
}
time.Sleep(1 * time.Second)
}
}
func main() {
info()
peeringConf := peering.GetConf()
peeringConf.ReplicaChan = make(chan *oplog.OpLog)
svrConf := server.GetConf()
stopChan := make(chan struct{})
svr := &server.Server{}
svr.SetConfig(svrConf)
go svr.Start("dat.db", peeringConf, stopChan)
webCfg := api.GetConf()
web := api.WebAPI{}
go web.Start(svr, webCfg)
if *joinPtr != "" {
go doJoin(svr)
}
// Wait to quit
log.Info("Press Ctrl+C to end")
waitForCtrlC()
fmt.Printf("\n")
log.Info("exiting...")
svr.Stop()
}
func waitForCtrlC() {
var endWaiter sync.WaitGroup
endWaiter.Add(1)
var sigChannel chan os.Signal
sigChannel = make(chan os.Signal, 1)
signal.Notify(sigChannel, os.Interrupt)
go func() {
<-sigChannel
endWaiter.Done()
}()
endWaiter.Wait()
}