-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload.go
89 lines (70 loc) · 1.67 KB
/
load.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"fmt"
"time"
"github.com/lc-tut/club-portal/consts"
"github.com/lc-tut/club-portal/router"
"github.com/lc-tut/club-portal/utils"
"github.com/spf13/viper"
"go.uber.org/zap"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
func loadConfig() error {
viper.SetConfigName("config")
viper.SetConfigType("toml")
viper.AddConfigPath("/run/secrets") // for docker secrets
if err := viper.ReadInConfig(); err != nil {
return err
}
viper.SetDefault("mode", "local")
viper.SetDefault("domain", "localhost")
return nil
}
func newZapLogger() (*zap.Logger, error) {
if utils.IsProd() {
return newZapProdConfig()
} else {
return newZapDevConfig()
}
}
func newDB() (*gorm.DB, error) {
dbUser := viper.GetString("mariadb_user")
dbPass := viper.GetString("mariadb_password")
dbAddress := viper.GetString("mariadb_address")
dbPort := viper.GetString("mariadb_port")
dbName := viper.GetString("mariadb_dbname")
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Asia%%2FTokyo", dbUser, dbPass, dbAddress, dbPort, dbName)
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
PrepareStmt: true,
SkipDefaultTransaction: true,
NowFunc: func() time.Time {
return time.Now().In(consts.JST)
},
})
if err != nil {
return nil, err
}
if utils.IsLocal() {
db = db.Debug()
}
return db, err
}
func newServer() (*router.Server, error) {
if err := loadConfig(); err != nil {
return nil, err
}
logger, err := newZapLogger()
if err != nil {
return nil, err
}
db, err := newDB()
if err != nil {
return nil, err
}
server, err := router.NewServer(logger, db)
if err != nil {
return nil, err
}
return server, nil
}