-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
109 lines (91 loc) · 2.4 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
package main
import (
"flag"
"fmt"
"net/http"
"path"
"strings"
log "github.com/sirupsen/logrus"
"github.com/fleaz/CptHook/input"
"github.com/spf13/viper"
)
var inputChannel = make(chan input.IRCMessage, 10)
func configureLogLevel() {
if l := viper.GetString("logging.level"); l != "" {
level, err := log.ParseLevel(l)
if err != nil {
log.WithFields(log.Fields{
"level": l,
}).Fatal("Uknown loglevel defined in configuration.")
}
log.WithFields(log.Fields{
"level": level,
}).Info("Setting loglevel defined by configuration")
log.SetLevel(level)
return
}
log.Info("Loglevel not defined in configuration. Defaulting to ERROR")
log.SetLevel(log.ErrorLevel)
}
type Configuration struct {
Modules map[string]InputModule `yaml:"modules"`
}
type InputModule struct {
Enalbed string `yaml:"enabled"`
}
func createModuleObject(name string) (input.Module, error) {
var m input.Module
var e error
switch name {
case "gitlab":
m = &input.GitlabModule{}
case "prometheus":
m = &input.PrometheusModule{}
case "simple":
m = &input.SimpleModule{}
default:
e = fmt.Errorf("found configuration for unknown module: %q", name)
}
return m, e
}
func main() {
confDirPtr := flag.String("config", "/etc/cpthook.yml", "Path to the configfile")
flag.Parse()
// Load configuration from file
confDir, confName := path.Split(*confDirPtr)
viper.SetConfigName(strings.Split(confName, ".")[0])
if len(confDir) > 0 {
viper.AddConfigPath(confDir)
} else {
viper.AddConfigPath(".")
}
err := viper.ReadInConfig()
if err != nil {
log.Fatal(err)
}
configureLogLevel()
var channelList = []string{}
var configurtaion = Configuration{}
err = viper.Unmarshal(&configurtaion)
if err != nil {
log.Fatal(err)
}
for moduleName := range configurtaion.Modules {
module, err := createModuleObject(moduleName)
if err != nil {
log.Error(err)
}
log.Infof("Loaded module %q", moduleName)
configPath := fmt.Sprintf("modules.%s", moduleName)
module.Init(viper.Sub(configPath), &inputChannel)
channelList = append(channelList, module.GetChannelList()...)
http.HandleFunc(module.GetEndpoint(), module.GetHandler())
}
// Start IRC connection
go ircConnection(viper.Sub("irc"), channelList)
// Start HTTP server
log.WithFields(log.Fields{
"listen": viper.GetString("http.listen"),
}).Info("Started HTTP Server")
log.Fatal(http.ListenAndServe(viper.GetString("http.listen"), nil))
}