98 lines
2.1 KiB
Go
98 lines
2.1 KiB
Go
// @title Beacon API
|
|
// @version 1.0
|
|
// @description This is a sample server for a beacon.
|
|
// @termsOfService http://swagger.io/terms/
|
|
|
|
// @contact.name API Support
|
|
// @contact.url http://www.swagger.io/support
|
|
// @contact.email support@swagger.io
|
|
|
|
// @license.name Apache 2.0
|
|
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
|
|
|
|
// @host localhost:8080
|
|
// @BasePath /api/v1
|
|
|
|
package main
|
|
|
|
import (
|
|
"beacon/config"
|
|
dao "beacon/pkg/dao/mysql"
|
|
"beacon/pkg/logger"
|
|
"beacon/pkg/validator"
|
|
"beacon/routers"
|
|
workers "beacon/workers/go"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func Run() {
|
|
// 从命令行获取配置
|
|
var cfn string
|
|
flag.StringVar(&cfn, "conf", "./config/config.yaml", "指定启动的配置文件")
|
|
flag.Parse()
|
|
|
|
// 1.加载配置文件
|
|
err := config.InitFromFile(cfn)
|
|
if err != nil {
|
|
panic(err) // 程序启动时加载配置文件失败直接退出
|
|
}
|
|
|
|
// 2.加载日志
|
|
err = logger.Init(config.Conf.LogConfig, config.Conf.Mode)
|
|
if err != nil {
|
|
panic(err) // 程序启动时初始化日志模块失败直接退出
|
|
}
|
|
|
|
// 添加校验规则
|
|
err = validator.InitTrans("zh")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// gorm
|
|
err = dao.InitGorm(config.Conf.MySQLConfig)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
gin.SetMode(gin.DebugMode)
|
|
|
|
// 6.HTTP 初始化路由
|
|
r := routers.Init()
|
|
go func() {
|
|
// 启动服务
|
|
err := r.Run(fmt.Sprintf("%s:%d", config.Conf.IP, config.Conf.Port))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}()
|
|
zap.L().Info(fmt.Sprintf("Serving Gin on %s:%d", config.Conf.IP, config.Conf.Port))
|
|
zap.L().Info("service start...")
|
|
|
|
go func() {
|
|
// 启动 Temporal Worker
|
|
err := workers.StartWorkflow(config.Conf.TemporalConfig)
|
|
if err != nil {
|
|
zap.L().Error("启动 Temporal Worker 失败", zap.Error(err))
|
|
os.Exit(1) // 如果启动 Temporal Worker 失败,退出程序
|
|
}
|
|
}()
|
|
|
|
// 接收操作系统发来的中断信号
|
|
QuitChan := make(chan os.Signal)
|
|
signal.Notify(QuitChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL) // kill -15 CTRL+C kill -9
|
|
<-QuitChan
|
|
|
|
}
|
|
|
|
func main() {
|
|
Run()
|
|
}
|