Adding feature selection to cfg

This commit is contained in:
Andreas Mieke 2020-04-11 05:16:48 +02:00
parent a7b4e31bec
commit 9730d63e58
4 changed files with 29 additions and 6 deletions

View file

@ -21,13 +21,26 @@ func main() {
config.LoadConfig(*cfg)
database.Init()
bot.Init()
web.Init()
if config.C.Features.Bot {
bot.Init()
} else {
log.Print("[BOTS] Feature disabled in config!")
}
if config.C.Features.Web {
web.Init()
} else {
log.Print("[WEB] Feature disabled in config!")
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
bot.DeInit()
if config.C.Features.Bot {
bot.DeInit()
}
database.Close()
config.WriteConfig(*cfg)
}

View file

@ -43,7 +43,6 @@ func Init() {
func DeInit() {
stream.Stop()
telegram.DeInit()
database.Close()
}
func handleHashtagTweet(tweet *twitter.Tweet) {

View file

@ -12,6 +12,7 @@ import (
// Config main type
type Config struct {
Features Features
Web Web
Twitter Twitter
Telegram Telegram
@ -45,11 +46,20 @@ type Web struct {
WebDir string
}
// Features related config
type Features struct {
Bot bool
Web bool
}
// C holds the loaded configuration
var C Config
// LoadDefaults puts default values into C
func LoadDefaults() {
C.Features.Bot = true
C.Features.Web = true
C.Web.BindUnix = false
C.Web.Bind = ":8080"
C.Web.WebDir = "./web"
@ -68,6 +78,7 @@ func LoadDefaults() {
// LoadConfig loads the configuration from given path
func LoadConfig(path string) error {
LoadDefaults()
_, err := toml.DecodeFile(path, &C)
if errors.Is(err, os.ErrNotExist) {
log.Printf("Could not find file \"%s\", using defaults!", path)

View file

@ -19,8 +19,8 @@ func Init() {
r.GET("/betriebsstellen/:code", getBetriebsstelleByCode)
if config.C.Web.BindUnix {
r.RunUnix(config.C.Web.Bind)
go r.RunUnix(config.C.Web.Bind)
} else {
r.Run(config.C.Web.Bind)
go r.Run(config.C.Web.Bind)
}
}