-
Notifications
You must be signed in to change notification settings - Fork 39
/
main.go
77 lines (67 loc) · 1.99 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
package main
import (
"apiproject/bootstrap"
"apiproject/config"
"apiproject/dao"
log2 "apiproject/log"
"apiproject/router"
"apiproject/util"
"go.uber.org/zap"
"gopkg.in/urfave/cli.v1"
"log"
"os"
)
/**
@author 王世彪
个人博客: https://sofineday.com?from=apiproject
微信: 645102170
QQ: 645102170
*/
//当有新的.go源文件引用静态文件时, 注意同步修改如下命令的--import-path参数
//go:generate rice -v --import-path "./router" --import-path "./config" embed-go
//生成swagger api接口文档, 注意指定-g参数
//需要指定参数"--parseDependency --parseInternal",否则报"cannot find type definition: gin.H"。或者切换swaggo为旧版本1.6.7(go get -u github.com/swaggo/swag/cmd/[email protected])
//go:generate swag init --parseDependency --parseInternal -g router/router.go
func main() {
//解析命令行参数
parseCliParam()
//系统初始化
bootstrap.Init()
defer dao.Db.Close()
//初始化路由
engine := router.Init()
//如下代码放到最后, 否则其他代码没机会执行
if err := engine.Run(":" + util.IntToStr(config.GlobalConfig.ServicePort)); err != nil {
log2.Logger.Error("main函数异常", zap.Error(err))
}
}
/**
解析命令行参数
*/
func parseCliParam() {
//实例化一个命令行程序
app := cli.NewApp()
//程序名称
app.Name = "GoTool"
//程序的用途描述
app.Usage = "To save the world"
//程序的版本号
app.Version = "1.0.0"
//设置启动参数
app.Flags = []cli.Flag{
//参数string, int, bool
cli.StringFlag{
Name: "profile, p", //参数名称
Value: "dev", //参数默认值
Usage: "运行环境:back-dev,back-test,front-dev,front-test", //参数功能描述
},
}
//该程序执行的代码
app.Action = func(c *cli.Context) error {
config.GlobalConfig.Profile = c.String("profile") //不使用变量接收,直接解析
return nil
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}