-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (69 loc) · 2.21 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
package main
import (
"github.com/marian-craciunescu/urlenricher/cachestore"
"github.com/marian-craciunescu/urlenricher/config"
"github.com/marian-craciunescu/urlenricher/connector"
"github.com/marian-craciunescu/urlenricher/metrics"
"github.com/marian-craciunescu/urlenricher/rest"
log "github.com/sirupsen/logrus"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
log.WithField("time", time.Now().Format(time.RFC3339)).Info("Starting urlenricher")
activeProfile := config.Dev
conf, err := config.ReadConfig(activeProfile.PropertyFile())
if err != nil {
panic("Could not read config fail.Exiting")
}
initLogging(conf.LogLevel, conf.ElkLogging)
log.WithFields(log.Fields{
"key": conf.ApiKey,
"secret": conf.ApiSecret,
}).Info("Starting with")
metricManager := metrics.NewMetricManager()
metricEndpoint := metrics.NewMetricEnpoint(metricManager)
brigthcloud, err := connector.NewBrightCloudConnector(conf.ApiKey, conf.ApiSecret)
if err != nil {
panic("Could not create connector.Exiting")
}
if err = brigthcloud.Start(); err != nil {
panic("Could not start brigthcloud connector")
}
urlCache, err := cachestore.NewURLCacheStore(50000, 2600, brigthcloud, conf.DataPath, metricManager)
if err != nil {
panic("Could not create cache store.Exiting")
}
if err := urlCache.Start(); err != nil {
logger.WithError(err).Error("Failed to load cache from disk")
}
cacheEndpoint := cachestore.NewURLEndpoint(urlCache)
apiServer := rest.NewAPIServer(conf, cacheEndpoint, metricEndpoint)
err = apiServer.Start()
if err != nil {
log.WithError(err).Error("Error starting rest server")
os.Exit(-2)
}
waitForTerminationAndExit(func() {
d, err := urlCache.Dump()
if err != nil {
log.WithError(err).Error("Failed to correctly dump cache")
}
log.WithField("dump_size", d).Info("Wrote on disk a dump with")
err = apiServer.Stop()
if err != nil {
log.WithError(err).Error("Failed to correctly stop api server")
}
})
}
func waitForTerminationAndExit(callback func()) {
signalC := make(chan os.Signal)
signal.Notify(signalC, syscall.SIGINT, syscall.SIGTERM)
sig := <-signalC
log.Infof("Got signal '%v' .. exiting gracefully now", sig)
callback()
log.Info("Exit gracefully now")
os.Exit(0)
}