Moved bot and webserver to goroutines

This commit is contained in:
Nefrace 2022-12-06 14:27:54 +03:00
parent 3a962cc8a3
commit 5eebacceea
1 changed files with 22 additions and 7 deletions

23
main.go
View File

@ -2,13 +2,14 @@ package main
import (
"log"
"sync"
"nefrace.ru/doomer/config"
"nefrace.ru/doomer/db"
)
func main() {
config.InitConfig()
cfg := config.InitConfig()
db, err := db.NewDatabase()
if err != nil {
log.Fatalf("Cant open database: %v", err)
@ -17,10 +18,24 @@ func main() {
defer db.Close()
doomer := NewDoomer(db)
dp := NewDispatcher(doomer)
app := InitServer(":3000")
var wg sync.WaitGroup
if cfg.Telegram.Token != "" {
wg.Add(1)
log.Println("Starting telegram bot")
go func() {
defer wg.Done()
dp := NewDispatcher(doomer)
log.Println(dp.Poll())
}()
}
if cfg.Host != "" {
wg.Add(1)
go func() {
defer wg.Done()
app := InitServer(cfg.Host)
log.Fatal(app.Run())
}()
log.Println(dp.Poll())
}
wg.Wait()
}