forked from Sean-Der/fail2rest
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
76 lines (64 loc) · 1.68 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
package main
import (
"github.com/Strum355/log"
"github.com/go-chi/chi"
"github.com/spf13/viper"
"github.com/strangeman/fail2go"
"github.com/UCCNetsoc/fail2rest/api"
"github.com/UCCNetsoc/fail2rest/config"
"github.com/UCCNetsoc/fail2rest/services"
"fmt"
"net/http"
"os"
"time"
)
func main() {
// Load Config
config.Load()
// Initialise logger
if viper.GetBool("fail2rest.production") {
log.InitJSONLogger(&log.Config{})
} else {
log.InitSimpleLogger(&log.Config{})
}
// Print settings
config.PrintSettings()
// Initialise Fail2Ban connection
log.WithFields(log.Fields{
"fail2ban socket": viper.GetString("fail2ban.socket"),
}).Info("Initialising fail2ban connection")
conn := fail2go.Newfail2goConn(viper.GetString("fail2ban.socket"))
// Start HTTP Server
log.WithFields(log.Fields{
"port": viper.GetInt("http.port"),
}).Info("Initialising HTTP Server")
r := chi.NewRouter()
var err error
// Register service with Consul
if viper.GetBool("consul.enabled") {
consul := services.ConsulService{
ConsulHost: viper.GetString("consul.host"),
ConsulToken: viper.GetString("consul.token"),
ServiceAddr: "127.0.0.1",
Port: viper.GetInt("http.port"),
TTL: time.Second * 5,
}
err = consul.Setup()
if err != nil {
log.WithError(err).Error("Could not setup Consul service")
os.Exit(1)
}
err = consul.Register()
if err != nil {
log.WithError(err).Error("Could not register with Consul service")
os.Exit(1)
}
}
// Initialise API
api := api.API{Fail2Conn: conn}
api.Register(r)
err = http.ListenAndServe(":"+fmt.Sprint(viper.GetInt("http.port")), r)
if err != nil {
log.WithError(err).Error("Error serving HTTP")
}
}