85 lines
2.1 KiB
Go
85 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"pandax/kit/logger"
|
|
"pandax/kit/restfulx"
|
|
"pandax/kit/starter"
|
|
"pandax/pkg/config"
|
|
"pandax/pkg/global"
|
|
"pandax/pkg/initialize"
|
|
"pandax/pkg/middleware"
|
|
"syscall"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var (
|
|
configFile string
|
|
)
|
|
|
|
var rootCmd = &cobra.Command{
|
|
Use: "轻量级的xScript运行时.",
|
|
Short: `仓湖云函数数字应用引擎`,
|
|
PreRun: func(cmd *cobra.Command, args []string) {
|
|
if configFile != "" {
|
|
global.Conf = config.InitConfig(configFile)
|
|
global.Log = logger.InitLog(global.Conf.Log.File.GetFilename(), global.Conf.Log.Level)
|
|
dbGorm := starter.DbGorm{Type: global.Conf.Server.DbType}
|
|
if global.Conf.Server.DbType == "mysql" {
|
|
dbGorm.Dsn = global.Conf.Mysql.Dsn()
|
|
dbGorm.MaxIdleConns = global.Conf.Mysql.MaxIdleConns
|
|
dbGorm.MaxOpenConns = global.Conf.Mysql.MaxOpenConns
|
|
} else {
|
|
dbGorm.Dsn = global.Conf.Postgresql.PgDsn()
|
|
dbGorm.MaxIdleConns = global.Conf.Postgresql.MaxIdleConns
|
|
dbGorm.MaxOpenConns = global.Conf.Postgresql.MaxOpenConns
|
|
}
|
|
global.Db = dbGorm.GormInit()
|
|
global.Log.Infof("%s连接成功", global.Conf.Server.DbType)
|
|
} else {
|
|
global.Log.Error("请配置config")
|
|
os.Exit(1)
|
|
}
|
|
},
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
// 前置 函数
|
|
restfulx.UseBeforeHandlerInterceptor(middleware.PermissionHandler)
|
|
// 后置 函数
|
|
restfulx.UseAfterHandlerInterceptor(middleware.LogHandler)
|
|
|
|
app := initialize.InitRouter()
|
|
global.Log.Info("路由初始化完成")
|
|
app.Start(context.TODO())
|
|
stop := make(chan os.Signal, 1)
|
|
signal.Notify(stop, syscall.SIGTERM, os.Interrupt)
|
|
<-stop
|
|
if err := app.Stop(context.TODO()); err != nil {
|
|
log.Fatalf("应用结束失败: %s", err)
|
|
os.Exit(-3)
|
|
}
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.Flags().StringVar(&configFile, "config", getEnvStr("SYS_CONFIG", "./config.yml"), "system config file path.")
|
|
}
|
|
|
|
func getEnvStr(env string, defaultValue string) string {
|
|
v := os.Getenv(env)
|
|
if v == "" {
|
|
return defaultValue
|
|
}
|
|
return v
|
|
}
|
|
|
|
func main() {
|
|
if err := rootCmd.Execute(); err != nil {
|
|
rootCmd.PrintErrf("请使用 root 权限执行指令: %s", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|