-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.go
112 lines (103 loc) · 3.35 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package bin
import (
"encoding/gob"
"fmt"
"github.com/griesbacher/Yarbes/Config"
"github.com/griesbacher/Yarbes/Logging/LogServer"
httpIncoming "github.com/griesbacher/Yarbes/NetworkInterfaces/HTTP/Incoming"
rpcIncoming "github.com/griesbacher/Yarbes/NetworkInterfaces/RPC/Incoming"
"github.com/griesbacher/Yarbes/RuleSystem"
"github.com/griesbacher/Yarbes/Tools/Strings"
"log"
"os"
"os/signal"
"reflect"
"runtime/pprof"
"syscall"
"time"
)
//Server start a server config depending on the config file
func Server(serverConfigPath, clientConfigPath, cpuProfile string) {
gob.Register(map[string]interface{}{})
if cpuProfile != "" {
f, err := os.Create(cpuProfile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
}
Config.InitServerConfig(serverConfigPath)
Config.InitClientConfig(clientConfigPath)
stoppables := []Stoppable{}
rpcInterfaces := []string{}
httpInterfaces := []string{}
if Config.GetServerConfig().LogServer.Enabled {
logServer := LogServer.NewLogServer()
logServer.Start()
stoppables = append(stoppables, logServer)
fmt.Println("Starting: LogServer")
if Config.GetServerConfig().LogServer.RPCInterface != "" {
fmt.Println("Starting: LogServer - RPC Interface")
logServerRPCI := rpcIncoming.NewLogServerRPCInterface(logServer.LogQueue)
logServerRPCI.Start()
stoppables = append(stoppables, logServerRPCI)
rpcInterfaces = append(rpcInterfaces, Config.GetServerConfig().LogServer.RPCInterface)
}
if Config.GetServerConfig().LogServer.HTTPInterface != "" {
fmt.Println("Starting: LogServer - HTTP Interface")
logServerRPCI := httpIncoming.NewLogServerHTTPInterface(logServer.InfluxClient)
logServerRPCI.Start()
stoppables = append(stoppables, logServerRPCI)
httpInterfaces = append(httpInterfaces, Config.GetServerConfig().LogServer.HTTPInterface)
}
time.Sleep(time.Duration(100) * time.Millisecond)
}
if Config.GetServerConfig().RuleSystem.Enabled {
ruleSystem := RuleSystem.NewRuleSystem()
ruleSystem.Start()
stoppables = append(stoppables, ruleSystem)
fmt.Println("Starting: RuleSystem")
if Config.GetServerConfig().RuleSystem.RPCInterface != "" {
fmt.Println("Starting: RuleSystem - RPC Interface")
ruleSystemRPCI := rpcIncoming.NewRuleSystemRPCInterface(ruleSystem)
if !Strings.Contains(rpcInterfaces, Config.GetServerConfig().RuleSystem.RPCInterface) {
fmt.Println("Starting: RPC")
ruleSystemRPCI.Start()
stoppables = append(stoppables, ruleSystemRPCI)
}
}
}
if Config.GetServerConfig().Proxy.Enabled {
fmt.Println("Starting: Proxy - RPC Interface")
proxyRPCI := rpcIncoming.NewProxyRPCInterface()
if !Strings.Contains(rpcInterfaces, Config.GetServerConfig().RuleSystem.RPCInterface) {
fmt.Println("Starting: RPC")
proxyRPCI.Start()
stoppables = append(stoppables, proxyRPCI)
}
}
interruptChannel := make(chan os.Signal, 1)
signal.Notify(interruptChannel, syscall.SIGINT)
signal.Notify(interruptChannel, syscall.SIGTERM)
quit := make(chan bool)
go func() {
<-interruptChannel
cleanUp(stoppables)
quit <- true
}()
fmt.Println("Everything's ready!")
//wait for the end to come
<-quit
if cpuProfile != "" {
pprof.StopCPUProfile()
}
fmt.Println("Bye")
}
func cleanUp(itemsToStop []Stoppable) {
for _, item := range itemsToStop {
if item != nil && item.IsRunning() {
fmt.Println("Stopping: ", reflect.TypeOf(item))
item.Stop()
}
}
}