forked from summerblue/gohub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (55 loc) · 1.38 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
package main
import (
"fmt"
"gohub/app/cmd"
"gohub/app/cmd/make"
"gohub/bootstrap"
btsConfig "gohub/config"
"gohub/pkg/config"
"gohub/pkg/console"
"os"
"github.com/spf13/cobra"
)
func init() {
// 加载 config 目录下的配置信息
btsConfig.Initialize()
}
func main() {
// 应用的主入口,默认调用 cmd.CmdServe 命令
var rootCmd = &cobra.Command{
Use: config.Get("app.name"),
Short: "A simple forum project",
Long: `Default will run "serve" command, you can use "-h" flag to see all subcommands`,
// rootCmd 的所有子命令都会执行以下代码
PersistentPreRun: func(command *cobra.Command, args []string) {
// 配置初始化,依赖命令行 --env 参数
config.InitConfig(cmd.Env)
// 初始化 Logger
bootstrap.SetupLogger()
// 初始化数据库
bootstrap.SetupDB()
// 初始化 Redis
bootstrap.SetupRedis()
// 初始化缓存
bootstrap.SetupCache()
},
}
// 注册子命令
rootCmd.AddCommand(
cmd.CmdServe,
cmd.CmdKey,
cmd.CmdPlay,
make.CmdMake,
cmd.CmdMigrate,
cmd.CmdDBSeed,
cmd.CmdCache,
)
// 配置默认运行 Web 服务
cmd.RegisterDefaultCmd(rootCmd, cmd.CmdServe)
// 注册全局参数,--env
cmd.RegisterGlobalFlags(rootCmd)
// 执行主命令
if err := rootCmd.Execute(); err != nil {
console.Exit(fmt.Sprintf("Failed to run app with %v: %s", os.Args, err.Error()))
}
}