-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
matrix-corporal.go
145 lines (119 loc) · 4.3 KB
/
matrix-corporal.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// matrix-corporal is a reconciliation and gateway program for Matrix servers
// Copyright (C) 2018 Slavi Pantaleev
//
// http://devture.com/
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"devture-matrix-corporal/corporal/configuration"
"devture-matrix-corporal/corporal/container"
"devture-matrix-corporal/corporal/httpapi"
"devture-matrix-corporal/corporal/httpgateway"
"devture-matrix-corporal/corporal/policy/provider"
"devture-matrix-corporal/corporal/reconciliation/reconciler"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/sirupsen/logrus"
)
// Following variables will be statically linked at the time of compiling
// Source: https://oddcode.daveamit.com/2018/08/17/embed-versioning-information-in-golang-binary/
// GitCommit holds short commit hash of source tree
var GitCommit string
// GitBranch holds current branch name the code is built off
var GitBranch string
// GitState shows whether there are uncommitted changes
var GitState string
// GitSummary holds output of git describe --tags --dirty --always
var GitSummary string
// BuildDate holds RFC3339 formatted UTC date (build time)
var BuildDate string
// Version holds contents of ./VERSION file, if exists, or the value passed via the -version option
var Version string
func main() {
fmt.Printf(`
_ _ _
_ __ ___ __ _| |_ _ __(_)_ __ ___ ___ _ __ _ __ ___ _ __ __ _| |
| '_ \ _ \ / _\ | __| '__| \ \/ /____ / __/ _ \| '__| '_ \ / _ \| '__/ _\ | |
| | | | | | (_| | |_| | | |> <_____| (_| (_) | | | |_) | (_) | | | (_| | |
|_| |_| |_|\__,_|\__|_| |_/_/\_\ \___\___/|_| | .__/ \___/|_| \__,_|_|
|_|
---------------------------------------------------------- [ Version: %s ]
GitCommit: %s
GitBranch: %s
GitState: %s
GitSummary: %s
BuildDate: %s
`, Version, GitCommit, GitBranch, GitState, GitSummary, BuildDate)
// Starting with a debug logger, but we may tone it down it below.
logger := logrus.New()
logger.Level = logrus.DebugLevel
configPath := flag.String("config", "config.json", "configuration file to use")
flag.Parse()
configuration, err := configuration.LoadConfiguration(*configPath, logger)
if err != nil {
panic(err)
}
if !configuration.Misc.Debug {
logger.Level = logrus.InfoLevel
}
container, shutdownHandler := container.BuildContainer(*configuration, logger)
httpGatewayServer := container.Get("httpgateway.server").(*httpgateway.Server)
err = httpGatewayServer.Start()
if err != nil {
panic(err)
}
if configuration.HttpApi.Enabled {
httpApiServer := container.Get("httpapi.server").(*httpapi.Server)
err = httpApiServer.Start()
if err != nil {
panic(err)
}
} else {
logger.Infof("Not starting HTTP API server: disabled by configuration")
}
// This needs to start before the policy provider,
// as it would listen for notifications from the policy store and we don't want it to miss any.
storeDrivenReconciler := container.Get("reconciliation.store_driven_reconciler").(*reconciler.StoreDrivenReconciler)
err = storeDrivenReconciler.Start()
if err != nil {
panic(err)
}
policyProvider := container.Get("policy.provider").(provider.Provider)
err = policyProvider.Start()
if err != nil {
panic(err)
}
channelComplete := make(chan bool)
setupSignalHandling(
channelComplete,
shutdownHandler,
)
<-channelComplete
}
func setupSignalHandling(
channelComplete chan bool,
shutdownHandler *container.ContainerShutdownHandler,
) {
signalChannel := make(chan os.Signal, 2)
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
go func() {
<-signalChannel
shutdownHandler.Shutdown()
channelComplete <- true
}()
}