diff --git a/check/checkrr.go b/check/checkrr.go index eb97226..29a527e 100644 --- a/check/checkrr.go +++ b/check/checkrr.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "github.com/aetaric/checkrr/logging" + "github.com/knadh/koanf/v2" "net/http" "os" "path/filepath" @@ -20,7 +21,6 @@ import ( "github.com/h2non/filetype/matchers" "github.com/kalafut/imohash" log "github.com/sirupsen/logrus" - "github.com/spf13/viper" bolt "go.etcd.io/bbolt" "gopkg.in/vansante/go-ffprobe.v2" ) @@ -40,8 +40,8 @@ type Checkrr struct { removeAudio []string removeLang []string ignoreHidden bool - config *viper.Viper - FullConfig *viper.Viper + FullConfig *koanf.Koanf + config *koanf.Koanf Chan *chan []string Logger *logging.Log } @@ -58,10 +58,7 @@ func (c *Checkrr) Run() { } c.Stats = features.Stats{Log: *c.Logger, DB: c.DB} - - if c.FullConfig.Sub("stats") != nil { - c.Stats.FromConfig(*c.FullConfig.Sub("stats")) - } + c.Stats.FromConfig(*c.FullConfig.Cut("stats")) // Connect to Sonarr, Radarr, and Lidarr c.connectServices() @@ -70,17 +67,17 @@ func (c *Checkrr) Run() { c.connectNotifications() // Setup CSV writer - if c.config.GetString("csvfile") != "" { - c.csv = features.CSV{FilePath: c.config.GetString("csvfile"), Log: c.Logger} + if c.config.String("csvfile") != "" { + c.csv = features.CSV{FilePath: c.config.String("csvfile"), Log: c.Logger} c.csv.Open() } - c.ignoreExts = c.config.GetStringSlice("ignoreexts") - c.ignorePaths = c.config.GetStringSlice("ignorepaths") - c.removeVideo = c.config.GetStringSlice("removevideo") - c.removeAudio = c.config.GetStringSlice("removeaudio") - c.removeLang = c.config.GetStringSlice("removelang") - c.ignoreHidden = c.config.GetBool("ignorehidden") + c.ignoreExts = c.config.Strings("ignoreexts") + c.ignorePaths = c.config.Strings("ignorepaths") + c.removeVideo = c.config.Strings("removevideo") + c.removeAudio = c.config.Strings("removeaudio") + c.removeLang = c.config.Strings("removelang") + c.ignoreHidden = c.config.Bool("ignorehidden") // I'm tired of waiting for filetype to support this. We'll force it by adding to the matchers on the fly. // TODO: if h2non/filetype#120 ever gets completed, remove this logic @@ -91,9 +88,9 @@ func (c *Checkrr) Run() { c.Stats.Start() - c.Logger.Debug(c.config.GetStringSlice("checkpath")) + c.Logger.Debug(c.config.Strings("checkpath")) - for _, path := range c.config.GetStringSlice("checkpath") { + for _, path := range c.config.Strings("checkpath") { c.Logger.WithFields(log.Fields{"startup": true}).Debugf("Path: %v", path) err := filepath.WalkDir(path, func(path string, d os.DirEntry, err error) error { @@ -184,20 +181,20 @@ func (c *Checkrr) Run() { ch <- []string{"time"} } -func (c *Checkrr) FromConfig(conf *viper.Viper) { +func (c *Checkrr) FromConfig(conf *koanf.Koanf) { c.config = conf } func (c *Checkrr) connectServices() { - if viper.GetViper().Sub("arr") != nil { - arrConfig := viper.GetViper().Sub("arr") - arrKeys := viper.GetViper().Sub("arr").AllKeys() + if c.FullConfig.Get("arr") != nil { + arrConfig := c.config.Cut("arr") + arrKeys := c.config.Cut("arr").Keys() for _, key := range arrKeys { if strings.Contains(key, "service") { k := strings.Split(key, ".")[0] - config := arrConfig.Sub(k) + config := arrConfig.Cut(k) - if config.GetString("service") == "sonarr" { + if config.String("service") == "sonarr" { sonarr := connections.Sonarr{Log: c.Logger} sonarr.FromConfig(config) sonarrConnected, sonarrMessage := sonarr.Connect() @@ -207,7 +204,7 @@ func (c *Checkrr) connectServices() { } } - if config.GetString("service") == "radarr" { + if config.String("service") == "radarr" { radarr := connections.Radarr{Log: c.Logger} radarr.FromConfig(config) radarrConnected, radarrMessage := radarr.Connect() @@ -217,7 +214,7 @@ func (c *Checkrr) connectServices() { } } - if config.GetString("service") == "lidarr" { + if config.String("service") == "lidarr" { lidarr := connections.Lidarr{Log: c.Logger} lidarr.FromConfig(config) lidarrConnected, lidarrMessage := lidarr.Connect() @@ -232,9 +229,9 @@ func (c *Checkrr) connectServices() { } func (c *Checkrr) connectNotifications() { - if viper.GetViper().Sub("notifications") != nil { + if c.FullConfig.Cut("notifications") != nil { c.notifications = notifications.Notifications{Log: c.Logger} - c.notifications.FromConfig(*viper.GetViper().Sub("notifications")) + c.notifications.FromConfig(c.config.Cut("notifications")) c.notifications.Connect() } else { c.Logger.WithFields(log.Fields{"Startup": true, "Notifications Connected": false}).Warn("No config options for notifications found.") @@ -434,7 +431,7 @@ func (c *Checkrr) recordBadFile(path string, fileType string, reason string) { c.Logger.WithFields(log.Fields{"DB Update": "Failure"}).Warnf("Error: %v", err.Error()) } - if c.config.GetString("csvfile") != "" { + if c.config.String("csvfile") != "" { c.csv.Write(path, fileType) } } diff --git a/connections/lidarr.go b/connections/lidarr.go index 8c50361..0556365 100644 --- a/connections/lidarr.go +++ b/connections/lidarr.go @@ -3,9 +3,9 @@ package connections import ( "fmt" "github.com/aetaric/checkrr/logging" + "github.com/knadh/koanf/v2" "strings" - "github.com/spf13/viper" "golift.io/starr" "golift.io/starr/lidarr" ) @@ -23,15 +23,15 @@ type Lidarr struct { Log *logging.Log } -func (l *Lidarr) FromConfig(conf *viper.Viper) { +func (l *Lidarr) FromConfig(conf *koanf.Koanf) { if conf != nil { - l.Address = conf.GetString("address") - l.Process = conf.GetBool("process") - l.ApiKey = conf.GetString("apikey") - l.Port = conf.GetInt("port") - l.BaseURL = conf.GetString("baseurl") - l.pathMaps = conf.GetStringMapString("mappings") - l.SSL = conf.GetBool("ssl") + l.Address = conf.String("address") + l.Process = conf.Bool("process") + l.ApiKey = conf.String("apikey") + l.Port = conf.Int("port") + l.BaseURL = conf.String("baseurl") + l.pathMaps = conf.StringMap("mappings") + l.SSL = conf.Bool("ssl") l.Log.Debugf("Lidarr Path Maps: %v", l.pathMaps) } else { l.Process = false diff --git a/connections/radarr.go b/connections/radarr.go index 92985b6..9bdb0a5 100644 --- a/connections/radarr.go +++ b/connections/radarr.go @@ -3,9 +3,9 @@ package connections import ( "fmt" "github.com/aetaric/checkrr/logging" + "github.com/knadh/koanf/v2" "strings" - "github.com/spf13/viper" "golift.io/starr" "golift.io/starr/radarr" ) @@ -23,15 +23,15 @@ type Radarr struct { Log *logging.Log } -func (r *Radarr) FromConfig(conf *viper.Viper) { +func (r *Radarr) FromConfig(conf *koanf.Koanf) { if conf != nil { - r.Address = conf.GetString("address") - r.Process = conf.GetBool("process") - r.ApiKey = conf.GetString("apikey") - r.Port = conf.GetInt("port") - r.BaseURL = conf.GetString("baseurl") - r.pathMaps = conf.GetStringMapString("mappings") - r.SSL = conf.GetBool("ssl") + r.Address = conf.String("address") + r.Process = conf.Bool("process") + r.ApiKey = conf.String("apikey") + r.Port = conf.Int("port") + r.BaseURL = conf.String("baseurl") + r.pathMaps = conf.StringMap("mappings") + r.SSL = conf.Bool("ssl") r.Log.Debugf("Radarr Path Maps: %v", r.pathMaps) } else { r.Process = false diff --git a/connections/sonarr.go b/connections/sonarr.go index 773573c..61efaa9 100644 --- a/connections/sonarr.go +++ b/connections/sonarr.go @@ -3,9 +3,9 @@ package connections import ( "fmt" "github.com/aetaric/checkrr/logging" + "github.com/knadh/koanf/v2" "strings" - "github.com/spf13/viper" "golift.io/starr" "golift.io/starr/sonarr" ) @@ -23,15 +23,15 @@ type Sonarr struct { Log *logging.Log } -func (s *Sonarr) FromConfig(conf *viper.Viper) { +func (s *Sonarr) FromConfig(conf *koanf.Koanf) { if conf != nil { - s.Address = conf.GetString("address") - s.Process = conf.GetBool("process") - s.ApiKey = conf.GetString("apikey") - s.Port = conf.GetInt("port") - s.BaseURL = conf.GetString("baseurl") - s.pathMaps = conf.GetStringMapString("mappings") - s.SSL = conf.GetBool("ssl") + s.Address = conf.String("address") + s.Process = conf.Bool("process") + s.ApiKey = conf.String("apikey") + s.Port = conf.Int("port") + s.BaseURL = conf.String("baseurl") + s.pathMaps = conf.StringMap("mappings") + s.SSL = conf.Bool("ssl") s.Log.Debugf("Sonarr Path Maps: %v", s.pathMaps) } else { s.Process = false diff --git a/features/stats.go b/features/stats.go index bd976f7..ebfa92c 100644 --- a/features/stats.go +++ b/features/stats.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "github.com/aetaric/checkrr/logging" + "github.com/knadh/koanf/v2" "net/http" "os" "strings" @@ -14,7 +15,6 @@ import ( "github.com/influxdata/influxdb-client-go/v2/api" "github.com/jedib0t/go-pretty/v6/table" log "github.com/sirupsen/logrus" - "github.com/spf13/viper" bolt "go.etcd.io/bbolt" ) @@ -23,7 +23,7 @@ type Stats struct { writeAPI1 api.WriteAPIBlocking `json:"-"` influxdb2 influxdb2.Client `json:"-"` writeAPI2 api.WriteAPIBlocking `json:"-"` - config viper.Viper `json:"-"` + config koanf.Koanf `json:"-"` Log logging.Log `json:"-"` splunk Splunk `json:"-"` splunkConfigured bool `json:"-"` @@ -68,33 +68,33 @@ type Splunk struct { token string } -func (s *Stats) FromConfig(config viper.Viper) { +func (s *Stats) FromConfig(config koanf.Koanf) { s.config = config - if config.Sub("influxdb1") != nil { - influx := config.Sub("influxdb1") + if len(config.Cut("influxdb1").Keys()) != 0 { + influx := config.Cut("influxdb1") var token string - if influx.GetString("user") != "" { - token = fmt.Sprintf("%s:%s", influx.GetString("user"), influx.GetString("pass")) + if influx.String("user") != "" { + token = fmt.Sprintf("%s:%s", influx.String("user"), influx.String("pass")) } else { token = "" } - s.influxdb1 = influxdb2.NewClient(influx.GetString("url"), token) - s.writeAPI1 = s.influxdb1.WriteAPIBlocking("", influx.GetString("bucket")) + s.influxdb1 = influxdb2.NewClient(influx.String("url"), token) + s.writeAPI1 = s.influxdb1.WriteAPIBlocking("", influx.String("bucket")) s.writeAPI1.EnableBatching() s.Log.WithFields(log.Fields{"startup": true, "influxdb": "enabled"}).Info("Sending data to InfluxDB 1.x") } - if config.Sub("influxdb2") != nil { - influx := config.Sub("influxdb2") - s.influxdb2 = influxdb2.NewClient(influx.GetString("url"), influx.GetString("token")) - s.writeAPI2 = s.influxdb2.WriteAPIBlocking(influx.GetString("org"), influx.GetString("bucket")) + if len(config.Cut("influxdb2").Keys()) != 0 { + influx := config.Cut("influxdb2") + s.influxdb2 = influxdb2.NewClient(influx.String("url"), influx.String("token")) + s.writeAPI2 = s.influxdb2.WriteAPIBlocking(influx.String("org"), influx.String("bucket")) s.writeAPI2.EnableBatching() s.Log.WithFields(log.Fields{"startup": true, "influxdb": "enabled"}).Info("Sending data to InfluxDB 2.x") } - if config.Sub("splunk") != nil { - splunk := config.Sub("splunk") - s.splunk = Splunk{address: splunk.GetString("address"), token: splunk.GetString("token")} + if len(config.Cut("splunk").Keys()) != 0 { + splunk := config.Cut("splunk") + s.splunk = Splunk{address: splunk.String("address"), token: splunk.String("token")} s.splunkConfigured = true s.Log.WithFields(log.Fields{"startup": true, "splunk stats": "enabled"}).Info("Sending stats data to Splunk") } diff --git a/go.mod b/go.mod index f20ac84..663dc25 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,9 @@ require ( github.com/influxdata/influxdb-client-go/v2 v2.14.0 github.com/jedib0t/go-pretty/v6 v6.5.9 github.com/kalafut/imohash v1.1.0 + github.com/knadh/koanf/parsers/yaml v0.1.0 + github.com/knadh/koanf/providers/file v1.1.2 + github.com/knadh/koanf/v2 v2.1.1 github.com/robfig/cron/v3 v3.0.1 github.com/sirupsen/logrus v1.9.3 github.com/spf13/pflag v1.0.5 @@ -26,6 +29,13 @@ require ( gopkg.in/vansante/go-ffprobe.v2 v2.2.0 ) +require ( + github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect + github.com/knadh/koanf/maps v0.1.1 // indirect + github.com/mitchellh/copystructure v1.2.0 // indirect + github.com/mitchellh/reflectwalk v1.0.2 // indirect +) + require ( github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect diff --git a/go.sum b/go.sum index dc0e138..c18b20f 100644 --- a/go.sum +++ b/go.sum @@ -90,6 +90,8 @@ github.com/go-playground/validator/v10 v10.22.1 h1:40JcKH+bBNGFczGuoBYgX4I6m/i27 github.com/go-playground/validator/v10 v10.22.1/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= +github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 h1:TQcrn6Wq+sKGkpyPvppOz99zsMBaUOKXq6HSv655U1c= +github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA= github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= @@ -122,6 +124,14 @@ github.com/kalafut/imohash v1.1.0/go.mod h1:6cn9lU0Sj8M4eu9UaQm1kR/5y3k/ayB68ynt github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM= github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/knadh/koanf/maps v0.1.1 h1:G5TjmUh2D7G2YWf5SQQqSiHRJEjaicvU0KpypqB3NIs= +github.com/knadh/koanf/maps v0.1.1/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI= +github.com/knadh/koanf/parsers/yaml v0.1.0 h1:ZZ8/iGfRLvKSaMEECEBPM1HQslrZADk8fP1XFUxVI5w= +github.com/knadh/koanf/parsers/yaml v0.1.0/go.mod h1:cvbUDC7AL23pImuQP0oRw/hPuccrNBS2bps8asS0CwY= +github.com/knadh/koanf/providers/file v1.1.2 h1:aCC36YGOgV5lTtAFz2qkgtWdeQsgfxUkxDOe+2nQY3w= +github.com/knadh/koanf/providers/file v1.1.2/go.mod h1:/faSBcv2mxPVjFrXck95qeoyoZ5myJ6uxN8OOVNJJCI= +github.com/knadh/koanf/v2 v2.1.1 h1:/R8eXqasSTsmDCsAyYj+81Wteg8AqrV9CP6gvsTsOmM= +github.com/knadh/koanf/v2 v2.1.1/go.mod h1:4mnTRbZCK+ALuBXHZMjDfG9y714L7TykVnZkXbMU3Es= github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= @@ -138,9 +148,13 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw= +github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ= +github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= diff --git a/logging/entry.go b/logging/entry.go index b84d5fd..47dbe70 100644 --- a/logging/entry.go +++ b/logging/entry.go @@ -36,7 +36,11 @@ func (entry *Entry) FromLogrus(logrus *log.Entry, loggers Log) { func (entry *Entry) Fatal(args ...interface{}) { for _, logger := range entry.Loggers.loggers { - logger.Warn(args) + if len(args) == 1 { + logger.WithFields(entry.Data).Warn(args[0]) + } else { + logger.WithFields(entry.Data).Warn(args) + } } entry.Loggers.LastResort.Fatal(args) } @@ -50,7 +54,11 @@ func (entry *Entry) Fatalf(format string, args ...interface{}) { func (entry *Entry) Info(args ...interface{}) { for _, logger := range entry.Loggers.loggers { - logger.WithFields(entry.Data).Info(args) + if len(args) == 1 { + logger.WithFields(entry.Data).Info(args[0]) + } else { + logger.WithFields(entry.Data).Info(args) + } } } @@ -62,7 +70,11 @@ func (entry *Entry) Infof(format string, args ...interface{}) { func (entry *Entry) Warn(args ...interface{}) { for _, logger := range entry.Loggers.loggers { - logger.WithFields(entry.Data).Info(args) + if len(args) == 1 { + logger.WithFields(entry.Data).Warn(args[0]) + } else { + logger.WithFields(entry.Data).Warn(args) + } } } @@ -74,7 +86,11 @@ func (entry *Entry) Warnf(format string, args ...interface{}) { func (entry *Entry) Debug(args ...interface{}) { for _, logger := range entry.Loggers.loggers { - logger.WithFields(entry.Data).Debug(args) + if len(args) == 1 { + logger.WithFields(entry.Data).Debug(args[0]) + } else { + logger.WithFields(entry.Data).Debug(args) + } } } @@ -86,7 +102,11 @@ func (entry *Entry) Debugf(format string, args ...interface{}) { func (entry *Entry) Error(args ...interface{}) { for _, logger := range entry.Loggers.loggers { - logger.WithFields(entry.Data).Error(args) + if len(args) == 1 { + logger.WithFields(entry.Data).Error(args[0]) + } else { + logger.WithFields(entry.Data).Error(args) + } } } diff --git a/logging/log.go b/logging/log.go index 90156dd..6e68302 100644 --- a/logging/log.go +++ b/logging/log.go @@ -5,9 +5,9 @@ package logging import ( "errors" + "github.com/knadh/koanf/v2" log "github.com/sirupsen/logrus" logrus_syslog "github.com/sirupsen/logrus/hooks/syslog" - "github.com/spf13/viper" "io" "log/syslog" "os" @@ -16,19 +16,19 @@ import ( type Log struct { loggers []*log.Logger - config *viper.Viper + config *koanf.Koanf LastResort *log.Logger } -func (logger *Log) FromConfig(conf *viper.Viper) { +func (logger *Log) FromConfig(conf *koanf.Koanf) { logger.config = conf if conf != nil { - logKeys := conf.AllKeys() + logKeys := conf.Keys() for _, key := range logKeys { k := strings.Split(key, ".")[0] - config := conf.Sub(k) + config := conf.Cut(k) if strings.Contains(strings.Split(key, ".")[1], "out") { - outConf := config.GetString("out") + outConf := config.String("out") var hook *logrus_syslog.SyslogHook = nil var stdout bool @@ -38,8 +38,8 @@ func (logger *Log) FromConfig(conf *viper.Viper) { switch outConf { case "syslog": var err error - proto := config.GetString("protocol") - addr := config.GetString("addr") + proto := config.String("protocol") + addr := config.String("addr") hook, err = logrus_syslog.NewSyslogHook(proto, addr, syslog.LOG_INFO, "") if err != nil { logger.LastResort.Warn("Error setting up syslog logger") @@ -53,7 +53,7 @@ func (logger *Log) FromConfig(conf *viper.Viper) { } case "file": var err error - path := config.GetString("path") + path := config.String("path") if _, err = os.Stat(path); errors.Is(err, os.ErrNotExist) { logFile, err = os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666) if err != nil { @@ -76,7 +76,7 @@ func (logger *Log) FromConfig(conf *viper.Viper) { l.AddHook(hook) } - switch config.GetString("formatter") { + switch config.String("formatter") { case "default": l.SetFormatter(&log.TextFormatter{}) case "json": @@ -108,7 +108,11 @@ func (logger *Log) SetLevel(level log.Level) { func (logger Log) Fatal(args ...interface{}) { for _, logInstance := range logger.loggers { - logInstance.Warn(args) + if len(args) == 1 { + logInstance.Warn(args[0]) + } else { + logInstance.Warn(args) + } } logger.LastResort.Fatal(args) } @@ -122,7 +126,11 @@ func (logger Log) Fatalf(format string, args ...interface{}) { func (logger Log) Info(args ...interface{}) { for _, logInstance := range logger.loggers { - logInstance.Info(args) + if len(args) == 1 { + logInstance.Info(args[0]) + } else { + logInstance.Info(args) + } } } @@ -134,7 +142,11 @@ func (logger Log) Infof(format string, args ...interface{}) { func (logger Log) Warn(args ...interface{}) { for _, logInstance := range logger.loggers { - logInstance.Info(args) + if len(args) == 1 { + logInstance.Warn(args[0]) + } else { + logInstance.Warn(args) + } } } @@ -146,7 +158,11 @@ func (logger Log) Warnf(format string, args ...interface{}) { func (logger Log) Debug(args ...interface{}) { for _, logInstance := range logger.loggers { - logInstance.Debug(args) + if len(args) == 1 { + logInstance.Debug(args[0]) + } else { + logInstance.Debug(args) + } } } @@ -158,7 +174,11 @@ func (logger Log) Debugf(format string, args ...interface{}) { func (logger Log) Error(args ...interface{}) { for _, logInstance := range logger.loggers { - logInstance.Error(args) + if len(args) == 1 { + logInstance.Error(args[0]) + } else { + logInstance.Error(args) + } } } @@ -170,21 +190,29 @@ func (logger Log) Errorf(format string, args ...interface{}) { func (logger Log) Panic(args ...interface{}) { for _, logInstance := range logger.loggers { - logInstance.Warn(args) + if len(args) == 1 { + logInstance.Warn(args[0]) + } else { + logInstance.Warn(args) + } } logger.LastResort.Panic(args) } func (logger Log) Panicf(format string, args ...interface{}) { for _, logInstance := range logger.loggers { - logInstance.Warn(format, args) + logInstance.Warnf(format, args) } logger.LastResort.Panicf(format, args) } func (logger Log) Println(args ...interface{}) { for _, logInstance := range logger.loggers { - logInstance.Println(args) + if len(args) == 1 { + logInstance.Println(args[0]) + } else { + logInstance.Println(args) + } } } diff --git a/main.go b/main.go index a2cbb4c..616831a 100644 --- a/main.go +++ b/main.go @@ -17,16 +17,20 @@ import ( "github.com/aetaric/checkrr/features" "github.com/aetaric/checkrr/webserver" "github.com/common-nighthawk/go-figure" + "github.com/knadh/koanf/parsers/yaml" + "github.com/knadh/koanf/providers/file" + "github.com/knadh/koanf/v2" "github.com/robfig/cron/v3" log "github.com/sirupsen/logrus" "github.com/spf13/pflag" - "github.com/spf13/viper" bolt "go.etcd.io/bbolt" ) var scheduler *cron.Cron var flagSet pflag.FlagSet +var k = koanf.New(".") + var cfgFile string var checkVer bool var oneShot bool @@ -58,7 +62,7 @@ func main() { initConfig() // Setup logger - logger.FromConfig(viper.GetViper().Sub("logs")) + logger.FromConfig(k.Cut("logs")) // Verify ffprobe is in PATH _, binpatherr := exec.LookPath("ffprobe") @@ -100,10 +104,10 @@ func main() { }() // Setup Database - if viper.GetViper().GetString("checkrr.database") != "" { + if k.String("checkrr.database") != "" { var err error - DB, err = bolt.Open(viper.GetViper().GetString("checkrr.database"), 0600, nil) + DB, err = bolt.Open(k.String("checkrr.database"), 0600, nil) if err != nil { logger.WithFields(log.Fields{"startup": true}).Fatalf("Error setting up database: %s", err) } @@ -183,13 +187,13 @@ func main() { } // Build checkrr from config - c := check.Checkrr{Chan: &rendertime, FullConfig: viper.GetViper(), DB: DB, Logger: logger} - c.FromConfig(viper.GetViper().Sub("checkrr")) + c := check.Checkrr{Chan: &rendertime, DB: DB, Logger: logger, FullConfig: k} + c.FromConfig(k.Cut("checkrr")) // Webserver Init - if viper.GetViper().Sub("webserver") != nil { - web = webserver.Webserver{DB: DB} - web.FromConfig(viper.GetViper().Sub("webserver"), webdata, &c) + if len(k.Cut("webserver").Keys()) != 0 { + web = webserver.Webserver{DB: DB, FullConfig: k} + web.FromConfig(k.Cut("webserver"), webdata, &c) } if oneShot { @@ -199,8 +203,8 @@ func main() { // Setup Cron runner. var id cron.EntryID scheduler = cron.New() - id, _ = scheduler.AddJob(viper.GetViper().GetString("checkrr.cron"), &c) - web.AddScehduler(scheduler, id) + id, _ = scheduler.AddJob(k.String("checkrr.cron"), &c) + web.AddScheduler(scheduler, id) go web.Run() scheduler.Start() logger.Infof("Next Run: %v", scheduler.Entry(id).Next.String()) @@ -224,7 +228,7 @@ func main() { initConfig() scheduler.Remove(id) scheduler.Stop() - id, _ = scheduler.AddJob(viper.GetViper().GetString("checkrr.cron"), &c) + id, _ = scheduler.AddJob(k.String("checkrr.cron"), &c) scheduler.Start() logger.Info("Config reloaded!") logger.Infof("Next Run: %v", scheduler.Entry(id).Next.String()) @@ -254,33 +258,37 @@ func initFlags() { func initConfig() { if cfgFile != "" { - // Use config file from the flag. - viper.SetConfigFile(cfgFile) + if err := k.Load(file.Provider(cfgFile), yaml.Parser()); err != nil { + logger.LastResort.Fatalf("Error loading config file: %s", cfgFile) + } } else { + logger.LastResort.Warn("No Config file specified, trying to load a default...") // Find home directory. home, _ := os.UserHomeDir() - viper.AddConfigPath(home) + paths := []string{"."} runtimeOS := runtime.GOOS switch runtimeOS { case "windows": - viper.AddConfigPath("C:/") + paths = append(paths, "C:/") default: - viper.AddConfigPath("/etc") - viper.AddConfigPath("/etc/checkrr") + paths = append(paths, "/etc", "/etc/checkrr") } - viper.AddConfigPath(".") - viper.SetConfigType("yaml") - viper.SetConfigName("checkrr") - } - viper.AutomaticEnv() // read in environment variables that match + paths = append(paths, home) - // If a config file is found, read it in. - if err := viper.ReadInConfig(); err == nil { - logger.LastResort.Infof("Using config file: %s", viper.ConfigFileUsed()) - } else { - logger.LastResort.Printf("err: %v", err.Error()) + for _, path := range paths { + err := k.Load(file.Provider(fmt.Sprintf("%s/checkrr.yaml", path)), yaml.Parser()) + if err != nil { + logger.LastResort.Infof("Couldn't load config at: %s/checkrr.yaml", path) + } else { + logger.LastResort.Infof("Found config at: %s/checkrr.yaml", path) + return + } + + } + + logger.LastResort.Fatal("Couldn't find a default config! Please specify a config file with -c") } } diff --git a/notifications/discord.go b/notifications/discord.go index d3a0507..0265578 100644 --- a/notifications/discord.go +++ b/notifications/discord.go @@ -1,13 +1,13 @@ package notifications import ( + "github.com/knadh/koanf/v2" "regexp" "strconv" "github.com/disgoorg/disgo/discord" webhook "github.com/disgoorg/disgo/webhook" discordsnowflake "github.com/disgoorg/snowflake/v2" - "github.com/spf13/viper" ) type DiscordWebhook struct { @@ -17,9 +17,9 @@ type DiscordWebhook struct { AllowedNotifs []string } -func (d *DiscordWebhook) FromConfig(config viper.Viper) { - d.URL = config.GetString("URL") - d.AllowedNotifs = config.GetStringSlice("notificationtypes") +func (d *DiscordWebhook) FromConfig(config koanf.Koanf) { + d.URL = config.String("URL") + d.AllowedNotifs = config.Strings("notificationtypes") } func (d *DiscordWebhook) Connect() (bool, string) { diff --git a/notifications/gotify.go b/notifications/gotify.go index 936e580..c15fb21 100644 --- a/notifications/gotify.go +++ b/notifications/gotify.go @@ -3,6 +3,7 @@ package notifications import ( "fmt" "github.com/aetaric/checkrr/logging" + "github.com/knadh/koanf/v2" "net/http" "net/url" @@ -11,7 +12,6 @@ import ( "github.com/gotify/go-api-client/v2/client/message" "github.com/gotify/go-api-client/v2/gotify" "github.com/gotify/go-api-client/v2/models" - "github.com/spf13/viper" ) type GotifyNotifs struct { @@ -23,10 +23,10 @@ type GotifyNotifs struct { Log *logging.Log } -func (d *GotifyNotifs) FromConfig(config viper.Viper) { - d.URL = config.GetString("URL") - d.AllowedNotifs = config.GetStringSlice("notificationtypes") - d.AuthToken = config.GetString("authtoken") +func (d *GotifyNotifs) FromConfig(config koanf.Koanf) { + d.URL = config.String("URL") + d.AllowedNotifs = config.Strings("notificationtypes") + d.AuthToken = config.String("authtoken") } func (d *GotifyNotifs) Connect() bool { diff --git a/notifications/healthchecks.go b/notifications/healthchecks.go index 2ce481e..0aaf058 100644 --- a/notifications/healthchecks.go +++ b/notifications/healthchecks.go @@ -1,15 +1,14 @@ package notifications import ( + "github.com/knadh/koanf/v2" "net/http" "strings" "time" - - "github.com/spf13/viper" ) type Healthchecks struct { - config viper.Viper + config koanf.Koanf AllowedNotifs []string URL string } @@ -51,8 +50,8 @@ func (h Healthchecks) Connect() bool { } } -func (h *Healthchecks) FromConfig(config viper.Viper) { +func (h *Healthchecks) FromConfig(config koanf.Koanf) { h.config = config - h.URL = config.GetString("url") - h.AllowedNotifs = config.GetStringSlice("notificationtypes") + h.URL = config.String("url") + h.AllowedNotifs = config.Strings("notificationtypes") } diff --git a/notifications/notifications.go b/notifications/notifications.go index 620a9f6..0a3d60f 100644 --- a/notifications/notifications.go +++ b/notifications/notifications.go @@ -2,8 +2,8 @@ package notifications import ( "github.com/aetaric/checkrr/logging" + "github.com/knadh/koanf/v2" log "github.com/sirupsen/logrus" - "github.com/spf13/viper" ) type Notification interface { @@ -12,7 +12,7 @@ type Notification interface { type Notifications struct { EnabledServices []Notification - config viper.Viper + config *koanf.Koanf Log *logging.Log } @@ -23,9 +23,9 @@ func (n Notifications) Notify(title string, description string, notifType string } func (n *Notifications) Connect() { - if n.config.Sub("discord") != nil { + if len(n.config.Cut("discord").Keys()) != 0 { discord := DiscordWebhook{} - discord.FromConfig(*n.config.Sub("discord")) + discord.FromConfig(*n.config.Cut("discord")) discordConnected, discordMessage := discord.Connect() n.Log.WithFields(log.Fields{"Startup": true, "Discord Connected": discordConnected}).Info(discordMessage) if discordConnected { @@ -33,72 +33,72 @@ func (n *Notifications) Connect() { } } - if n.config.Sub("healthchecks") != nil { + if len(n.config.Cut("healthchecks").Keys()) != 0 { healthcheck := Healthchecks{} - healthcheck.FromConfig(*n.config.Sub("healthchecks")) + healthcheck.FromConfig(*n.config.Cut("healthchecks")) healthcheckConnected := healthcheck.Connect() if healthcheckConnected { n.EnabledServices = append(n.EnabledServices, healthcheck) } } - if n.config.Sub("telegram") != nil { + if len(n.config.Cut("telegram").Keys()) != 0 { telegram := Telegram{Log: n.Log} - telegram.FromConfig(*n.config.Sub("telegram")) + telegram.FromConfig(*n.config.Cut("telegram")) telegramConnected := telegram.Connect() if telegramConnected { n.EnabledServices = append(n.EnabledServices, telegram) } } - if n.config.Sub("webhook") != nil { + if len(n.config.Cut("webhook").Keys()) != 0 { webhook := Notifywebhook{Log: n.Log} - webhook.FromConfig(*n.config.Sub("webhook")) + webhook.FromConfig(*n.config.Cut("webhook")) webhookConnected := webhook.Connect() if webhookConnected { n.EnabledServices = append(n.EnabledServices, webhook) } } - if n.config.Sub("pushbullet") != nil { + if len(n.config.Cut("pushbullet").Keys()) != 0 { pushbullet := Pushbullet{Log: n.Log} - pushbullet.FromConfig(*n.config.Sub("pushbullet")) + pushbullet.FromConfig(*n.config.Cut("pushbullet")) pushbulletConnected := pushbullet.Connect() if pushbulletConnected { n.EnabledServices = append(n.EnabledServices, pushbullet) } } - if n.config.Sub("pushover") != nil { + if len(n.config.Cut("pushover").Keys()) != 0 { pushover := Pushover{Log: n.Log} - pushover.FromConfig(*n.config.Sub("pushover")) + pushover.FromConfig(*n.config.Cut("pushover")) pushoverConnected := pushover.Connect() if pushoverConnected { n.EnabledServices = append(n.EnabledServices, pushover) } } - if n.config.Sub("gotify") != nil { + if len(n.config.Cut("gotify").Keys()) != 0 { gotify := GotifyNotifs{Log: n.Log} - gotify.FromConfig(*n.config.Sub("gotify")) + gotify.FromConfig(*n.config.Cut("gotify")) gotifyConnected := gotify.Connect() if gotifyConnected { n.EnabledServices = append(n.EnabledServices, gotify) } } - if n.config.Sub("splunk") != nil { + if len(n.config.Cut("splunk").Keys()) != 0 { splunk := SplunkHEC{Log: n.Log} - splunk.FromConfig(*n.config.Sub("splunk")) + splunk.FromConfig(*n.config.Cut("splunk")) splunkConnected := splunk.Connect() if splunkConnected { n.EnabledServices = append(n.EnabledServices, splunk) } } - if n.config.Sub("ntfy") != nil { + if len(n.config.Cut("ntfy").Keys()) != 0 { ntfy := NtfyNotifs{Log: n.Log} - ntfy.FromConfig(*n.config.Sub("ntfy")) + ntfy.FromConfig(*n.config.Cut("ntfy")) ntfyConnected := ntfy.Connect() if ntfyConnected { n.EnabledServices = append(n.EnabledServices, ntfy) @@ -106,6 +106,6 @@ func (n *Notifications) Connect() { } } -func (n *Notifications) FromConfig(c viper.Viper) { +func (n *Notifications) FromConfig(c *koanf.Koanf) { n.config = c } diff --git a/notifications/ntfy.go b/notifications/ntfy.go index 2df8f72..af1191b 100644 --- a/notifications/ntfy.go +++ b/notifications/ntfy.go @@ -4,11 +4,11 @@ import ( "encoding/base64" "fmt" "github.com/aetaric/checkrr/logging" + "github.com/knadh/koanf/v2" "net/http" "strings" log "github.com/sirupsen/logrus" - "github.com/spf13/viper" ) type NtfyNotifs struct { @@ -21,16 +21,16 @@ type NtfyNotifs struct { Log *logging.Log } -func (n *NtfyNotifs) FromConfig(config viper.Viper) { - n.host = config.GetString("host") - n.topic = config.GetString("topic") - token := config.GetString("token") - n.AllowedNotifs = config.GetStringSlice("notificationtypes") +func (n *NtfyNotifs) FromConfig(config koanf.Koanf) { + n.host = config.String("host") + n.topic = config.String("topic") + token := config.String("token") + n.AllowedNotifs = config.Strings("notificationtypes") if token == "" { - n.user = config.GetString("user") - n.pass = config.GetString("password") + n.user = config.String("user") + n.pass = config.String("password") } else { - n.token = config.GetString("token") + n.token = config.String("token") } if n.token == "" && n.user == "" { diff --git a/notifications/pushbullet.go b/notifications/pushbullet.go index c01b6d3..6fdc09a 100644 --- a/notifications/pushbullet.go +++ b/notifications/pushbullet.go @@ -2,12 +2,12 @@ package notifications import ( "github.com/aetaric/checkrr/logging" - "github.com/spf13/viper" + "github.com/knadh/koanf/v2" "github.com/xconstruct/go-pushbullet" ) type Pushbullet struct { - config viper.Viper + config koanf.Koanf AllowedNotifs []string apiToken string devices []string @@ -50,9 +50,9 @@ func (p *Pushbullet) Connect() bool { } } -func (p *Pushbullet) FromConfig(config viper.Viper) { +func (p *Pushbullet) FromConfig(config koanf.Koanf) { p.config = config - p.apiToken = config.GetString("apitoken") - p.devices = config.GetStringSlice("devices") - p.AllowedNotifs = config.GetStringSlice("notificationtypes") + p.apiToken = config.String("apitoken") + p.devices = config.Strings("devices") + p.AllowedNotifs = config.Strings("notificationtypes") } diff --git a/notifications/pushover.go b/notifications/pushover.go index 92d17ff..c3fdc6e 100644 --- a/notifications/pushover.go +++ b/notifications/pushover.go @@ -3,11 +3,11 @@ package notifications import ( "github.com/aetaric/checkrr/logging" "github.com/gregdel/pushover" - "github.com/spf13/viper" + "github.com/knadh/koanf/v2" ) type Pushover struct { - config viper.Viper + config koanf.Koanf AllowedNotifs []string apiToken string recipient *pushover.Recipient @@ -36,7 +36,7 @@ func (p Pushover) Notify(title string, description string, notifType string, pat func (p *Pushover) Connect() bool { p.bot = pushover.New(p.apiToken) - p.recipient = pushover.NewRecipient(p.config.GetString("recipient")) + p.recipient = pushover.NewRecipient(p.config.String("recipient")) if p.bot != nil { p.Log.Info("Connected to pushover") return true @@ -46,8 +46,8 @@ func (p *Pushover) Connect() bool { } } -func (p *Pushover) FromConfig(config viper.Viper) { +func (p *Pushover) FromConfig(config koanf.Koanf) { p.config = config - p.apiToken = config.GetString("apitoken") - p.AllowedNotifs = config.GetStringSlice("notificationtypes") + p.apiToken = config.String("apitoken") + p.AllowedNotifs = config.Strings("notificationtypes") } diff --git a/notifications/splunkhec.go b/notifications/splunkhec.go index 0b39574..d8a2ad4 100644 --- a/notifications/splunkhec.go +++ b/notifications/splunkhec.go @@ -4,12 +4,12 @@ import ( "encoding/json" "fmt" "github.com/aetaric/checkrr/logging" + "github.com/knadh/koanf/v2" "net/http" "strings" "time" log "github.com/sirupsen/logrus" - "github.com/spf13/viper" ) type SplunkHEC struct { @@ -31,10 +31,10 @@ type SplunkEvent struct { SourceType string `json:"sourcetype"` } -func (d *SplunkHEC) FromConfig(config viper.Viper) { - d.URL = config.GetString("url") - d.Token = config.GetString("token") - d.AllowedNotifs = config.GetStringSlice("notificationtypes") +func (d *SplunkHEC) FromConfig(config koanf.Koanf) { + d.URL = config.String("url") + d.Token = config.String("token") + d.AllowedNotifs = config.Strings("notificationtypes") } func (d *SplunkHEC) Connect() bool { diff --git a/notifications/telegram.go b/notifications/telegram.go index 29c21e6..2857c57 100644 --- a/notifications/telegram.go +++ b/notifications/telegram.go @@ -3,14 +3,14 @@ package notifications import ( "fmt" "github.com/aetaric/checkrr/logging" + "github.com/knadh/koanf/v2" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" log "github.com/sirupsen/logrus" - "github.com/spf13/viper" ) type Telegram struct { - config viper.Viper + config koanf.Koanf AllowedNotifs []string apiToken string username string @@ -67,10 +67,10 @@ func (t *Telegram) Connect() bool { return true } -func (t *Telegram) FromConfig(config viper.Viper) { +func (t *Telegram) FromConfig(config koanf.Koanf) { t.config = config - t.apiToken = config.GetString("apitoken") - t.username = config.GetString("username") - t.chatid = config.GetInt64("chatid") - t.AllowedNotifs = config.GetStringSlice("notificationtypes") + t.apiToken = config.String("apitoken") + t.username = config.String("username") + t.chatid = config.Int64("chatid") + t.AllowedNotifs = config.Strings("notificationtypes") } diff --git a/notifications/webhook.go b/notifications/webhook.go index de49831..006fa69 100644 --- a/notifications/webhook.go +++ b/notifications/webhook.go @@ -4,14 +4,13 @@ import ( "bytes" "encoding/json" "github.com/aetaric/checkrr/logging" + "github.com/knadh/koanf/v2" "net/http" - - "github.com/spf13/viper" ) type Notifywebhook struct { url string - config viper.Viper + config koanf.Koanf AllowedNotifs []string Log *logging.Log } @@ -21,10 +20,10 @@ type payload struct { Path string `json:"path,omitempty"` } -func (n *Notifywebhook) FromConfig(config viper.Viper) { +func (n *Notifywebhook) FromConfig(config koanf.Koanf) { n.config = config - n.url = config.GetString("url") - n.AllowedNotifs = config.GetStringSlice("notificationtypes") + n.url = config.String("url") + n.AllowedNotifs = config.Strings("notificationtypes") } func (n *Notifywebhook) Connect() bool { diff --git a/webserver/webserver.go b/webserver/webserver.go index 9991558..6e230ee 100644 --- a/webserver/webserver.go +++ b/webserver/webserver.go @@ -14,8 +14,8 @@ import ( "github.com/aetaric/checkrr/check" "github.com/gin-contrib/static" "github.com/gin-gonic/gin" + "github.com/knadh/koanf/v2" "github.com/robfig/cron/v3" - "github.com/spf13/viper" bolt "go.etcd.io/bbolt" ) @@ -41,6 +41,8 @@ type Webserver struct { data chan []string trustedProxies []string DB *bolt.DB + config *koanf.Koanf + FullConfig *koanf.Koanf } type BaseURL string @@ -58,17 +60,18 @@ func (b BaseURL) String() string { return string(b) } -func (w *Webserver) FromConfig(conf *viper.Viper, c chan []string, checkrr *check.Checkrr) { - w.Port = conf.GetInt("port") - w.tls = conf.GetBool("tls") +func (w *Webserver) FromConfig(conf *koanf.Koanf, c chan []string, checkrr *check.Checkrr) { + w.config = conf + w.Port = conf.Int("port") + w.tls = conf.Bool("tls") if w.tls { - w.key = conf.Sub("certs").GetString("key") - w.cert = conf.Sub("certs").GetString("cert") + w.key = conf.String("certs.key") + w.cert = conf.String("certs.cert") } - w.BaseURL = BaseURL(conf.GetString("baseurl")).EnforceTrailingSlash() + w.BaseURL = BaseURL(conf.String("baseurl")).EnforceTrailingSlash() baseurl = w.BaseURL - if conf.GetStringSlice("trustedproxies") != nil { - w.trustedProxies = conf.GetStringSlice("trustedproxies") + if conf.Strings("trustedproxies") != nil { + w.trustedProxies = conf.Strings("trustedproxies") } else { w.trustedProxies = nil } @@ -78,7 +81,7 @@ func (w *Webserver) FromConfig(conf *viper.Viper, c chan []string, checkrr *chec checkrrLogger = checkrr.Logger } -func (w *Webserver) AddScehduler(cron *cron.Cron, entryid cron.EntryID) { +func (w *Webserver) AddScheduler(cron *cron.Cron, entryid cron.EntryID) { scheduler = cron cronEntry = entryid } @@ -106,14 +109,17 @@ func (w *Webserver) Run() { func createServer(w *Webserver) *gin.Engine { embeddedBuildFolder := newStaticFileSystem() // use debug mode if chekrr.debug is true - if viper.Sub("checkrr").GetBool("debug") { + if w.FullConfig.Bool("checkrr.debug") { gin.SetMode(gin.DebugMode) } else { gin.SetMode(gin.ReleaseMode) } router := gin.Default() - router.SetTrustedProxies(w.trustedProxies) + err := router.SetTrustedProxies(w.trustedProxies) + if err != nil { + checkrrLogger.Warn("Error setting Trusted Proxies") + } router.Use(static.Serve(w.BaseURL.String(), embeddedBuildFolder)) api := router.Group(w.BaseURL.String() + "api") api.GET("/files/bad", getBadFiles) @@ -124,9 +130,17 @@ func createServer(w *Webserver) *gin.Engine { api.POST("/run", runCheckrr) if w.tls { - router.RunTLS(fmt.Sprintf(":%v", w.Port), w.cert, w.key) + checkrrLogger.Infof("Starting HTTPS Webserver on port %d", w.Port) + err := router.RunTLS(fmt.Sprintf(":%v", w.Port), w.cert, w.key) + if err != nil { + checkrrLogger.Warn("Failed to start Webserver in TLS mode") + } } else { - router.Run(fmt.Sprintf(":%v", w.Port)) + checkrrLogger.Infof("Starting HTTP Webserver on port %d", w.Port) + err := router.Run(fmt.Sprintf(":%v", w.Port)) + if err != nil { + checkrrLogger.Warn("Failed to start Webserver") + } } return router }