diff --git a/cmd/db640bot/main.go b/cmd/db640bot/main.go index 8f43bee..6fad4f0 100644 --- a/cmd/db640bot/main.go +++ b/cmd/db640bot/main.go @@ -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) } diff --git a/internal/bot/bot.go b/internal/bot/bot.go index 6b4c1ab..a0fcd28 100644 --- a/internal/bot/bot.go +++ b/internal/bot/bot.go @@ -43,7 +43,6 @@ func Init() { func DeInit() { stream.Stop() telegram.DeInit() - database.Close() } func handleHashtagTweet(tweet *twitter.Tweet) { diff --git a/internal/config/config.go b/internal/config/config.go index beea53d..7866d50 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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) diff --git a/internal/web/web.go b/internal/web/web.go index 6c3fe45..68a4c05 100644 --- a/internal/web/web.go +++ b/internal/web/web.go @@ -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) } }