-
Notifications
You must be signed in to change notification settings - Fork 1
/
chicha.go
75 lines (62 loc) · 2.17 KB
/
chicha.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
65
66
67
68
69
70
71
72
73
74
75
package main
/*
Work in progress.
*/
import (
"embed"
//"fmt"
"log"
"chicha/Packages/Config"
"chicha/Packages/Models" // Our package with database models
"chicha/Packages/view"
//"gorm.io/driver/postgres" // Gorm Postgres driver package
//"gorm.io/driver/sqlite" //gorm sqlite driver
"github.com/glebarez/sqlite"
"gorm.io/gorm" // Database ORM package
"gorm.io/gorm/logger"
//"github.com/sethvargo/go-password/password" //password generator
//profiling CPU:
//"github.com/pkg/profile"
)
//go:embed static
var static embed.FS
func main() {
var (
err error
dsn = Config.DB_FULL_FILE_PATH
)
Models.DB, err = gorm.Open(sqlite.Open(dsn), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
SkipDefaultTransaction: true,
})
if err != nil {
log.Fatal("ERROR: Connect to local SQLite database failed at", dsn, err)
}
log.Println("Connected to local SQLite database at", dsn)
// Database Migrations
log.Println("Creating or changing database structures (applying migrations)...")
if err = Models.DB.AutoMigrate(&Models.Lap{}, &Models.User{}, &Models.Race{}, &Models.Checkin{}, &Models.Admin{}); err != nil {
log.Fatal("failed to apply migration: ", err)
}
// Create new system administator if them not exists
//adminPass, err := password.Generate(8, 1, 3, true, true)
//if err != nil {
// fmt.Println(err)
//} else {
// fmt.Println("Creating system administrator account if not exists with name =", Config.ADMIN_LOGIN, "and password =", adminPass)
// Models.CreateDefaultAdmin(Config.ADMIN_LOGIN, adminPass)
//}
// Start RFID listener
go Models.StartAntennaListener()
log.Printf("Data collector IP = %s, db save interval = %d sec, minimal lap time = %d sec and race timeout = %d sec.\n", Config.APP_ANTENNA_LISTENER_IP, Config.LAPS_SAVE_INTERVAL_SEC, Config.MINIMAL_LAP_TIME_SEC, Config.RACE_TIMEOUT_SEC)
// Routing
r := Models.SetupRouter()
// view
view.New(r, static, Models.SubscribeOnceOnRacePositionsChange())
// Start API server
log.Printf("WEB API server IP = %s\n", Config.API_SERVER_LISTENER_IP)
errAPI := r.Run(Config.API_SERVER_LISTENER_IP)
if errAPI != nil {
log.Println("ERROR: API server start failed:", errAPI)
}
}