forked from Openmesh-Network/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (81 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
package main
import (
"context"
_ "embed"
"os"
"os/signal"
"syscall"
"github.com/openmesh-network/core/collector"
"github.com/openmesh-network/core/internal/bft"
"github.com/openmesh-network/core/internal/config"
"github.com/openmesh-network/core/internal/core"
"github.com/openmesh-network/core/internal/database"
"github.com/openmesh-network/core/internal/logger"
"github.com/openmesh-network/core/networking/p2p"
"github.com/openmesh-network/core/updater"
"net/http"
_ "net/http/pprof"
)
const (
allowLoadConfigAtRuntime = true
)
var (
// These are the public keys trusted to sign new updates.
TrustedKeys = []updater.PublicKey{
// XXX: THESE ARE NOT THE FINAL KEYS, CHANGE BEFORE DEPLOYING TO PRODUCTION!!!
updater.PublicKeyFromBase64("HJOvRAmk3tYFvs2uFm+06T6kU9MC2oT+8s1Scwqf224"),
// updater.PublicKeyFromBase64("jt1/Mb2xWnd7z6pn21iTb9EU4wycdZhT6Zgb3xf+h6k"),
// updater.PublicKeyFromBase64("+8rZEcO928jPGlkn0CZKbXxi11twmZbj9KxxBvTa15Q"),
// Fake key
// updater.PublicKeyFromBase64("JZlpAGC7aYXIupMUQN48daT/tYRulWiOC0sXFNEXFNE"),
}
//go:embed config.yml
configCompileValue string
)
func main() {
go http.ListenAndServe("localhost:8080", nil)
if allowLoadConfigAtRuntime {
config.ParseFlags()
}
config.ParseConfig(configCompileValue, allowLoadConfigAtRuntime)
// Initialise logger after parsing configuration
logger.InitLogger()
defer logger.SyncAll()
// Initialise graceful shutdown.
cancelCtx, cancel := context.WithCancel(context.Background())
defer cancel()
// Initialise p2p instance.
p2pInstance, err := p2p.NewInstance(cancelCtx, config.Config.P2P).Build()
if err != nil {
logger.Fatalf("Failed to initialise p2p instance: %s", err.Error())
}
// Initialise BadgerDB connection
dbInstance, err := database.NewInstance()
if err != nil {
logger.Fatalf("Failed to establish BadgerDB connection: %s", err.Error())
}
// Need collector before bft.
collectorInstance := collector.New()
collectorInstance.Start(cancelCtx)
// Initialise CometBFT instance
bftInstance, err := bft.NewInstance(dbInstance.Conn, collectorInstance)
if err != nil {
logger.Fatalf("Failed to initialise CometBFT instance: %s", err.Error())
}
// Run the updater.
// TODO: Maybe pass past CID versions to avoid redownloading old updates.
updater.NewInstance(TrustedKeys, p2pInstance).Start(cancelCtx)
// Build and start top-level instance.
ins := core.NewInstance().
SetP2pInstance(p2pInstance).
SetDBInstance(dbInstance).
SetBFTInstance(bftInstance)
ins.Start(cancelCtx)
logger.Infof("Openmesh Core started successfully.")
defer ins.Stop()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
// Stop here!
sig := <-sigChan
logger.Infof("Termination signal received: %v", sig)
}