/
main.go
110 lines (89 loc) · 2.75 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
108
109
110
package main
import (
// "github.com/tomdionysus/trinity/schema"
// "github.com/tomdionysus/trinity/sql"
"github.com/tomdionysus/trinity/config"
"github.com/tomdionysus/trinity/kvstore"
"github.com/tomdionysus/trinity/network"
"github.com/tomdionysus/trinity/util"
// "github.com/tomdionysus/trinity/packets"
"os"
)
func main() {
// Load Configuration
config := config.NewConfig()
// Start logging
logger := util.NewLogger(*config.LogLevel)
// Validate Configuration
if i, errs := config.Validate(); !i {
for _, err := range errs {
logger.Error("Config", err.Error())
}
os.Exit(-1)
logger.Fatal("Main", "Bad Configuration, Exiting")
}
// Key/Value Store
kv := kvstore.NewKVStore(logger)
kv.Init()
kv.Start()
// Banner
logger.Raw("Main", "---------------------------------------")
logger.Raw("Main", "Trinity DB - v%s", VERSION)
logger.Raw("Main", "---------------------------------------")
// Config Debug
logger.Debug("Config", "Nodes: %s", config.Nodes.String())
logger.Debug("Config", "Certificate: %s", *config.Certificate)
logger.Debug("Config", "Port: %d", *config.Port)
logger.Debug("Config", "Advertise: %s", *config.HostAddr)
logger.Debug("Config", "LogLevel: %s (%d)", *config.LogLevel, logger.LogLevel)
if *config.DisableHeartbeat {
logger.Debug("Config", "Heartbeat disabled on this instance, be carefull with system reliability")
}
// CA
capool := network.NewCAPool(logger)
err := capool.LoadPEM(*config.CA)
if err != nil {
logger.Error("Main", "Cannot Load CA '%s': %s", *config.CA, err.Error())
os.Exit(-1)
}
logger.Debug("Main", "CA Certiticate Loaded")
// Server
svr := network.NewTLSServer(logger, capool, kv, *config.HostAddr, *config.DisableHeartbeat)
logger.Info("Main", "Trinity Node ID %02X", svr.ServerNode.ID)
// Certificate
err = svr.LoadPEMCert(*config.Certificate, *config.Certificate)
if err != nil {
logger.Error("Main", "Cannot Load Certificate '%s': %s", *config.Certificate, err.Error())
os.Exit(-1)
}
logger.Debug("Main", "Cert Loaded")
// Listen
err = svr.Listen(uint16(*config.Port))
if err != nil {
logger.Error("Main", "Cannot Start Server: %s", err.Error())
os.Exit(-1)
}
var memcache *network.MemcacheServer
// Memcache
if *config.MemcacheEnabled {
memcache = network.NewMemcacheServer(logger, *config.MemcachePort, svr)
memcache.Init()
memcache.Start()
}
for _, remoteAddr := range config.Nodes {
logger.Info("Main", "Attempting Connection to Peer (%s)", remoteAddr)
svr.ConnectTo(remoteAddr)
}
TrinityMainLoop(svr, logger)
// Shutdown Memcache
if memcache != nil {
memcache.Stop()
}
// Shutdown Server and wait for close
svr.Stop()
_ = <-svr.StatusChannel
// Shutdown KV Store
kv.Stop()
logger.Info("Main", "Shutdown Complete, exiting")
os.Exit(0)
}