-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
108 lines (94 loc) · 2.69 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"time"
"github.com/charmbracelet/log"
"github.com/limitcool/lib"
"github.com/limitcool/starter/configs"
"github.com/limitcool/starter/global"
"github.com/limitcool/starter/internal/database"
"github.com/limitcool/starter/internal/database/mongodb"
"github.com/limitcool/starter/routers"
"github.com/limitcool/starter/pkg/env"
"github.com/spf13/viper"
"github.com/limitcool/starter/pkg/logger"
)
func loadConfig() {
env := env.Get()
log.Info("current env:", env)
// 设置默认配置文件
viper.SetConfigName("config")
viper.AddConfigPath("./configs")
viper.SetConfigType("yaml")
// 读取默认配置
if err := viper.ReadInConfig(); err != nil {
log.Fatal("read default config err = ", err)
}
// 读取环境配置
viper.SetConfigName(fmt.Sprintf("config-%s", env))
if err := viper.MergeInConfig(); err != nil {
log.Warn("config not found, use default config")
}
// 解析配置到结构体
if err := viper.Unmarshal(&global.Config); err != nil {
log.Fatal("config unmarshal err = ", err)
}
}
func main() {
lib.SetDebugMode(func() {
log.Info("Debug Mode")
log.SetLevel(log.DebugLevel)
log.SetReportCaller(true)
})
log.SetPrefix("🌏 starter ")
// 加载配置
loadConfig()
// 初始化日志
logger.Setup(global.Config.Log)
switch global.Config.Driver {
case configs.DriverMongo:
log.Info("driver is mongo")
_, err := mongodb.NewMongoDBConn(context.Background(), &global.Config.Mongo)
if err != nil {
log.Fatal("mongo connect err = ", err)
}
case configs.DriverMysql, configs.DriverPostgres, configs.DriverSqlite, configs.DriverMssql, configs.DriverOracle:
log.Info("driver is ", global.Config.Driver)
db := database.NewDB(*global.Config)
db.AutoMigrate()
default:
log.Info("driver is none")
}
// _, _, err = redis.NewRedisClient(global.Config)
// if err != nil {
// log.Fatal("redis connect err = ", err)
// }
router := routers.NewRouter()
s := &http.Server{
Addr: fmt.Sprint("0.0.0.0:", global.Config.App.Port),
Handler: router,
MaxHeaderBytes: 1 << 20,
}
log.Infof("Listen: %s:%d\n", "http://127.0.0.1", global.Config.App.Port)
go func() {
// 服务连接 监听
if err := s.ListenAndServe(); err != nil {
log.Fatalf("Listen:%s\n", err)
}
}()
// 等待中断信号以优雅地关闭服务器,这里需要缓冲
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
//(设置5秒超时时间)
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
if err := s.Shutdown(ctx); err != nil {
// 处理错误,例如记录日志、返回错误等
log.Infof("Error during shutdown: %v", err)
}
}