You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.3 KiB
71 lines
1.3 KiB
2 months ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"main/dao/mysql"
|
||
|
"main/logger"
|
||
|
"main/pkg/gfsd"
|
||
|
"main/pkg/snowflake"
|
||
|
"main/pkg/vt"
|
||
|
"main/routes"
|
||
|
"main/settings"
|
||
|
"net/http"
|
||
|
_ "net/http/pprof"
|
||
|
"runtime"
|
||
|
|
||
|
"github.com/spf13/viper"
|
||
|
"go.uber.org/zap"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
isTrace(viper.GetBool("app.test"))
|
||
|
// 1. 加载配置
|
||
|
if err := settings.Init(); err != nil {
|
||
|
fmt.Printf("init settings failed, err = %v\n", err)
|
||
|
return
|
||
|
}
|
||
|
// 2. 初始化日志
|
||
|
if err := logger.Init(); err != nil {
|
||
|
fmt.Printf("init logger failed, err = %v\n", err)
|
||
|
return
|
||
|
}
|
||
|
defer zap.L().Sync()
|
||
|
zap.L().Debug("init ogger success...")
|
||
|
|
||
|
// 3. 搭建 Mysql 连接
|
||
|
if err := mysql.Init(); err != nil {
|
||
|
fmt.Printf("init mysql failed = %v\n", err)
|
||
|
return
|
||
|
}
|
||
|
defer mysql.Close()
|
||
|
|
||
|
// 4. 搭建 Redis 连接
|
||
|
// 暂时不用
|
||
|
|
||
|
// 雪花算法初始化
|
||
|
if err := snowflake.Init(); err != nil {
|
||
|
fmt.Printf("init snowflake failed, err:%v\n", err)
|
||
|
return
|
||
|
}
|
||
|
// 初始化gin内置翻译器
|
||
|
if err := vt.InitTrans("zh"); err != nil {
|
||
|
fmt.Printf("init trans failed = %v\n", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// 5. 注册路由
|
||
|
r := routes.Setup()
|
||
|
|
||
|
// 6. 启动服务(同时实现优雅关机)
|
||
|
gfsd.Graceful_Shutdown(r)
|
||
|
}
|
||
|
|
||
|
func isTrace(f bool) {
|
||
|
if f {
|
||
|
runtime.SetCPUProfileRate(10000)
|
||
|
go func() {
|
||
|
http.ListenAndServe("localhost:6060", nil)
|
||
|
}()
|
||
|
}
|
||
|
}
|