/
main.go
73 lines (62 loc) · 1.59 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
package main
import (
// "github.com/twitchyliquid64/CNC/signaller"
"github.com/twitchyliquid64/CNC/messenger"
"github.com/twitchyliquid64/CNC/registry"
"github.com/twitchyliquid64/CNC/logging"
"github.com/twitchyliquid64/CNC/plugin"
"github.com/twitchyliquid64/CNC/config"
"github.com/twitchyliquid64/CNC/data"
"github.com/twitchyliquid64/CNC/web"
"os/signal"
"syscall"
"time"
"os"
)
func run(stopSignal chan bool) {
logging.Info("main", "Starting server")
if config.Load(getConfigPath()) != nil {
logging.Fatal("main", "Configuration error")
}
logging.Info("main", "Configuration read, now starting '", config.GetServerName(), "'")
registry.Initialise()
data.Initialise()
messenger.Initialise()
plugin.Initialise(true) //true = load and run plugins from database
web.Initialise()
go web.Run()
for {
select {
case <- stopSignal:
logging.Info("main", "Got stop signal, finalizing now")
//signaller.Stop()
return
default:
time.Sleep(time.Millisecond * 400)
}
}
}
func getConfigPath()string{
if len(os.Args) < 2{
return "config.json"
}
return os.Args[1]
}
func main() {
processSignal := make(chan os.Signal, 1)
signal.Notify(processSignal, os.Interrupt, os.Kill, syscall.SIGHUP)
chanStop := make(chan bool)
shouldRun := true
go func(){//goroutine to monitor OS signals
for{
s := <- processSignal //wait for signal from OS
logging.Info("main", "Got OS signal: ", s)
if s != syscall.SIGHUP{
shouldRun = false
}
chanStop <- true
}
}()
run(chanStop)//will run until signalled to stop from above goroutine
time.Sleep(time.Millisecond * 100)
}