-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
93 lines (77 loc) · 1.93 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
package main
import (
"encoding/json"
"flag"
"io/ioutil"
"os"
"os/signal"
"strconv"
"syscall"
"time"
log "github.com/Sirupsen/logrus"
)
type Configuration struct {
Port int `json:"port"`
Address string `json:"address"`
PgUsers []string `json:"pgUsers"`
Debug bool `json:"debug"`
Cleartext bool `json:"cleartext"`
TcpTimeout int `json:"server_timeout"`
HpFeedsConfig `json:"hpfeedsConfig"`
}
func init() {
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stdout)
log.SetLevel(log.InfoLevel)
}
func configurationFrom(configFile string) Configuration {
var config Configuration
jsonConfig, err := ioutil.ReadFile(configFile)
if err != nil {
log.Fatalf("Couldn't %s", err)
}
err = json.Unmarshal(jsonConfig, &config)
if err != nil {
log.Fatalf("Couldn't parse JSON in %s: %s", configFile, err)
}
return config
}
func main() {
configFile := flag.String("config", "pghoney.conf", "JSON configuration file")
flag.Parse()
config := configurationFrom(*configFile)
port := strconv.Itoa(config.Port)
addr := config.Address
pgUsers := config.PgUsers
debug := config.Debug
cleartext := config.Cleartext
hpFeedsConfig := config.HpFeedsConfig
tcpTimeout := time.Duration(config.TcpTimeout) * time.Second
if debug {
log.SetLevel(log.DebugLevel)
}
hpfeedsChannel := make(chan []byte, 1024)
if hpFeedsConfig.Enabled {
go hpfeedsConnect(&hpFeedsConfig, hpfeedsChannel)
}
postgresServer := NewPostgresServer(
port,
addr,
pgUsers,
cleartext,
tcpTimeout,
hpfeedsChannel,
hpFeedsConfig.Enabled,
)
// Capture 'shutdown' signals and shutdown gracefully.
shutdownSignal := make(chan os.Signal)
signal.Notify(shutdownSignal, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
sig := <-shutdownSignal
log.Infof("Process got signal: %s", sig)
log.Infof("Shutting down...")
postgresServer.Close()
os.Exit(0)
}()
postgresServer.Listen()
}