-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
server.go
98 lines (84 loc) · 2.21 KB
/
server.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
package main
import (
"os"
"MirageNetwork/MirageServer/controller"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
const (
MirageDateTimeFormat = "2006-01-02 15:04:05"
SocketWritePermissions = 0o666
)
func main() {
sysAddr, set := os.LookupEnv("MIRAGE_SYS_ADDR")
if !set {
sysAddr = ":8081"
}
zerolog.TimeFieldFormat = MirageDateTimeFormat
logger := zerolog.New(zerolog.MultiLevelWriter(os.Stdout)).With().Timestamp().Logger()
logger = logger.Level(zerolog.DebugLevel)
log.Logger = logger
datapool := controller.DataPool{}
err := datapool.OpenDB()
if err != nil {
log.Fatal().Caller().Err(err).Msg("Error opening database")
}
ctrlChn := make(chan controller.CtrlMsg)
msgChn := make(chan controller.CtrlMsg)
err = datapool.InitCockpitDB()
if err != nil {
log.Fatal().Caller().Err(err).Msg("Error initializing cockpit database")
}
cockpit, err := controller.NewCockpit(sysAddr, ctrlChn, msgChn, datapool.DB())
if err != nil {
log.Fatal().Caller().Err(err).Msg("Error initializing cockpit")
}
go cockpit.Run()
log.Info().Msg("Cockpit is ready on " + sysAddr + "")
/*
err = controller.LoadConfig()
if err != nil {
log.Fatal().Caller().Err(err).Msgf("Error loading config")
}
cfg, err := controller.GetMirageConfig(srvAddr, serverURL)
if err != nil {
log.Fatal().Caller().Err(err).Msgf("Error loading config")
}
*/
for {
select {
case cockpitMsg := <-ctrlChn:
log.Trace().Msgf("Cockpit message received: %s", cockpitMsg.Msg)
switch cockpitMsg.Msg {
case "start":
err = datapool.InitMirageDB()
if err != nil {
log.Error().Caller().Err(err).Msgf("Error initializing Mirage DB")
msgChn <- controller.CtrlMsg{
Msg: "error",
Err: err,
}
break
}
cockpit.App, err = controller.NewMirage(cockpitMsg.SysCfg, datapool.DB())
if err != nil {
log.Error().Caller().Err(err).Msg("Error initializing Mirage")
msgChn <- controller.CtrlMsg{
Msg: "error",
Err: err,
}
break
}
err = cockpit.App.Serve(ctrlChn)
if err != nil {
log.Error().Caller().Err(err).Msg("Error starting server")
msgChn <- controller.CtrlMsg{
Msg: "error",
Err: err,
}
break
}
}
}
}
}