-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.go
64 lines (54 loc) · 1.32 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/Thelvaen/gobot/config"
"github.com/Thelvaen/gobot/models"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
func init() {
var err error
conf, err = config.NewConfig()
if err == config.ErrorLoading {
log.Fatalln("can't open conf.yml file")
}
if err == config.ErrorLogin {
log.Fatalln("main Channel not provided in the config")
}
if err != nil {
log.Fatalln(err)
}
// Configuring Gorm & Gorm Logger
gormLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
logger.Config{
SlowThreshold: time.Second, // Slow SQL threshold
LogLevel: logger.Silent, // Log level
Colorful: true, // Disable color
},
)
dbConf := gorm.Config{
FullSaveAssociations: true,
Logger: gormLogger,
}
dbFile := "twitchbot.db"
dataStore, err = gorm.Open(sqlite.Open(dbFile), &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)
}()
}