-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
76 lines (64 loc) · 1.32 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
package main
import (
"github.com/gofiber/fiber/v2"
"log"
"nubedb/api/proto/protoserver"
"nubedb/api/rest/middleware"
"nubedb/api/rest/route"
"nubedb/discover/mdnsdiscover"
"nubedb/internal/app"
"nubedb/internal/config"
"runtime"
"sync"
)
func init() {
if runtime.GOOS == "windows" {
log.Fatalln("nubedb is only compatible with Mac and Linux")
}
}
func main() {
cfg, errCfg := config.New()
if errCfg != nil {
log.Fatalln(errCfg)
}
start(app.NewApp(cfg))
}
func start(a *app.App) {
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
startApiRest(a)
}()
wg.Add(1)
go func() {
defer wg.Done()
startApiProto(a)
}()
if a.Config.Cluster.DiscoverStrategy == config.DiscoverDefault {
wg.Add(1)
go func() {
defer wg.Done()
mdnsdiscover.ServeAndBlock(a.Config.CurrentNode.Host, 8001)
}()
}
wg.Wait()
}
func startApiProto(a *app.App) {
log.Println("[proto] Starting proto server...")
err := protoserver.Start(a)
if err != nil {
log.Fatalln("proto api can't be started:", err)
}
}
func startApiRest(a *app.App) {
errListen := newApiRest(a).Listen(a.Config.CurrentNode.ApiAddress)
if errListen != nil {
log.Fatalln("api can't be started:", errListen)
}
}
func newApiRest(a *app.App) *fiber.App {
middleware.InitMiddlewares(a.HttpServer)
route.Register(a)
return a.HttpServer
}