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

29
main.go
View File

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