-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (61 loc) · 1.7 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"errors"
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"gorm.io/gorm"
"go-battle/common/cache"
"go-battle/common/db"
"go-battle/model"
"go-battle/router"
"go-battle/service"
)
func main() {
var err error
if err = configInit(); err != nil {
panic(fmt.Sprintln("config init error ", err))
}
if err = db.Init(); err != nil {
panic(fmt.Sprintln("db init error ", err))
}
if err = migrate(db.DBConn()); err != nil {
panic(fmt.Sprintln("db migrate error ", err))
}
if err = cache.Init(); err != nil {
panic(fmt.Sprintln("redis init error ", err))
}
serviceInit(db.DBConn())
router := router.RoutersInit()
host := viper.GetString("server.host")
port := viper.GetString("server.port")
router.Run(fmt.Sprintf("%s:%s", host, port))
}
func configInit() error {
viper.SetConfigName("config")
viper.AddConfigPath("./config")
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found
return errors.New("config file not found")
} else {
// Config file was found but another error was produced
return fmt.Errorf("config file was found but another error is %v", err)
}
}
// Config file found and successfully parsed
fmt.Println("config init success")
//add config change event
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config file changed:", e.Name)
})
viper.WatchConfig()
return nil
}
func serviceInit(dbConn *gorm.DB) {
service.TestServiceInit(dbConn)
}
//TODO优化,获取包内所有结构体
func migrate(dbConn *gorm.DB) error {
return dbConn.AutoMigrate(&model.Test{})
}