/
init.go
49 lines (42 loc) · 999 Bytes
/
init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"log"
"os"
"os/signal"
"syscall"
"github.com/thelvaen/gobot/config"
"github.com/thelvaen/gobot/models"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func init() {
err := config.LoadAndParse()
if err == config.ErrorLoading {
log.Fatalln("can't open config.yml file")
}
if err == config.ErrorLogin {
log.Fatalln("main Channel not provided in the config")
}
if err != nil {
log.Fatalln(err)
}
// Opening DB
dbConf := gorm.Config{
FullSaveAssociations: true,
Logger: nil,
}
dataStore, err = gorm.Open(sqlite.Open("twitchbot.db"), &dbConf)
if err != nil {
log.Fatalf("can't open Sqlite3 DB : %s", err)
}
dataStore.Migrator().AutoMigrate(&models.TwitchUser{}, &models.GiveAway{}, &models.Poll{}, &models.PollOption{}, &models.Stat{})
// Intercepting Ctrl+C to close DB properly
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
db, _ := dataStore.DB()
db.Close()
os.Exit(0)
}()
}