forked from cert-lv/graphoscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
143 lines (121 loc) · 2.77 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
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
package main
import (
"crypto/tls"
"fmt"
"net/http"
"os"
"time"
//_ "net/http/pprof"
"github.com/rs/zerolog"
)
var (
// Holder all service's configuration
config *Config
// Instance of the global logger
log zerolog.Logger
// Current service's version
version string
)
/*
* Use recommended TLS settings
*/
func setupTLSserver() *http.Server {
cfg := &tls.Config{
MinVersion: tls.VersionTLS12, // At least TLS v1.2 is recommended
}
// Enable secure ciphers only
for _, cipherSuite := range tls.CipherSuites() {
cfg.CipherSuites = append(cfg.CipherSuites, cipherSuite.ID)
}
return &http.Server{
Addr: config.Server.Host + ":" + config.Server.Port,
TLSConfig: cfg,
ReadTimeout: time.Duration(config.Server.ReadTimeout) * time.Second,
ReadHeaderTimeout: time.Duration(config.Server.ReadHeaderTimeout) * time.Second,
}
}
func main() {
/*
* Parse configuration file
*/
err := loadConfig()
if err != nil {
fmt.Fprintf(os.Stderr, "Can't load configuration: %s", err.Error())
os.Exit(1)
}
/*
* Setup a global logger to the file or stdout
*/
err = setupLogger()
if err != nil {
fmt.Fprintf(os.Stderr, "Can't setup a logger: %s", err.Error())
os.Exit(1)
}
/*
* Setup a database
*/
err = setupDatabase()
if err != nil {
log.Fatal().Msg("Can't setup a database: " + err.Error())
}
/*
* Setup Web GUI handlers
*/
err = setupGUI()
if err != nil {
log.Fatal().Msg("Can't start Web GUI components: " + err.Error())
}
/*
* Load plugins
*/
err = loadPlugins()
if err != nil {
log.Fatal().Msg("Can't load plugins: " + err.Error())
}
/*
* Setup collectors for the predefined data sources
*/
err = setupCollectors()
if err != nil {
log.Fatal().Msg("Can't load collectors: " + err.Error())
}
/*
* Setup processors of the data sources received data
*/
err = setupProcessors()
if err != nil {
log.Fatal().Msg("Can't load processors: " + err.Error())
}
/*
* Stop collectors on service exit
*/
defer func() {
for name, collector := range collectors {
err := collector.Stop()
if err != nil {
log.Error().
Str("source", name).
Msg("Can't stop the collector: " + err.Error())
} else {
log.Debug().
Str("source", name).
Msg("Collector stopped")
}
}
}()
// Load service's version
err = loadVersion()
if err != nil {
log.Fatal().Msg("Can't load version: " + err.Error())
}
/*
* Start an HTTPS server
*/
http.HandleFunc("/api", apiHandler)
log.Info().Msgf("Graphoscope v%s. Starting the service listening on %s:%s", version, config.Server.Host, config.Server.Port)
server := setupTLSserver()
err = server.ListenAndServeTLS(config.Server.CertFile, config.Server.KeyFile)
if err != nil {
log.Fatal().Msg("Can't ListenAndServeTLS: " + err.Error())
}
}