/
main.go
106 lines (102 loc) · 2.63 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
package main
import (
"fmt"
"github.com/RichardKnop/machinery/v1"
"github.com/urfave/cli/v2"
"github.com/voyager-go/start-go-api/bootstrap"
"github.com/voyager-go/start-go-api/config"
"github.com/voyager-go/start-go-api/pkg/migrate"
"github.com/voyager-go/start-go-api/pkg/validator"
"github.com/voyager-go/start-go-api/router"
schedule_server "github.com/voyager-go/start-go-api/schedule/server"
schedule_worker "github.com/voyager-go/start-go-api/schedule/worker"
"os"
"runtime"
"time"
)
var (
// AppName 当前应用的名称
AppName = "start go api"
AppUsage = "使用gin框架作为基础开发库,封装一套适用于面向api编程的快速开发结构"
// AppPort 程序启动的端口
AppPort string
// BuildVersion 编译的app版本
BuildVersion string
// BuildAt 编译时间
BuildAt string
// openSchedule 是否开启任务调度服务
openSchedule = true
// taskServer 任务服务
taskServer *machinery.Server
taskServerArg = "server"
taskWorkerArg = "worker"
)
// stack 程序运行前的处理
func stack() *cli.App {
buildInfo := fmt.Sprintf("%s-%s-%s-%s-%s", runtime.GOOS, runtime.GOARCH, BuildVersion, BuildAt, time.Now().Format("2006-01-02 15:04:05"))
if openSchedule {
var err error
taskServer, err = schedule_server.InitMachineryServer()
if err != nil {
panic(err)
}
}
return &cli.App{
Name: AppName,
Version: buildInfo,
Usage: AppUsage,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "env",
Value: "dev",
Usage: "请选择配置文件 [dev | pre]",
Destination: &config.ConfEnv,
},
&cli.StringFlag{
Name: "port",
Value: "8090",
Usage: "请选择启动端口",
Destination: &AppPort,
},
},
Action: func(context *cli.Context) error {
// 初始化配置文件信息
config.InitConfig()
// 程序启动时需要加载的服务
bootstrap.BootService()
// 引入验证翻译器
validator.NewValidate()
// 自动生成表结构
migrate.AuthMigrate()
// 注册路由 启动程序
return router.Register().Run(":" + AppPort)
},
Commands: []*cli.Command{
{
Name: taskServerArg,
Usage: "Launch application server",
Action: func(context *cli.Context) error {
if openSchedule {
schedule_server.StartServer(taskServer)
}
return nil
},
},
{
Name: taskWorkerArg,
Usage: "Launch application worker",
Action: func(context *cli.Context) error {
if openSchedule {
return schedule_worker.StartWorker(taskServer)
}
return nil
},
},
},
}
}
func main() {
if err := stack().Run(os.Args); err != nil {
panic(err)
}
}