-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
57 lines (53 loc) · 1.38 KB
/
main.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
package main
import (
"fmt"
"github.com/dangkaka/leaderboard/helper"
"github.com/dangkaka/leaderboard/scores"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/spf13/viper"
)
func main() {
viper.SetConfigType("json")
viper.SetConfigFile("config/config.json")
err := viper.ReadInConfig()
if err != nil {
helper.ReportFatal("Failed to read config: %v", err)
return
}
dbHost := viper.GetString(`database.host`)
dbPort := viper.GetString(`database.port`)
dbUser := viper.GetString(`database.user`)
dbPass := viper.GetString(`database.pass`)
dbName := viper.GetString(`database.name`)
dsn := fmt.Sprintf(
"user=%s password=%s host=%s port=%s dbname=%s sslmode=disable",
dbUser,
dbPass,
dbHost,
dbPort,
dbName)
db, err := gorm.Open("postgres", dsn)
if err != nil {
helper.ReportFatal("Something went horribly wrong: %v", err)
return
}
defer db.Close()
db.LogMode(true)
//schema migrations
db.AutoMigrate(&scores.Game{}, &scores.Score{})
// Seed the db
var count int
db.Model(&scores.Game{}).Count(&count)
if games := viper.GetStringSlice("games"); count == 0 && len(games) > 0 {
for _, game := range games {
if err := db.Create(&scores.Game{
Name: game,
}).Error; err != nil {
helper.ReportFatal("Something went horribly wrong: %v", err)
}
}
}
r := NewRouter(db)
r.Run(viper.GetString("server.address"))
}