| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package main
- import (
- "fmt"
- "os"
- "github.com/spf13/cobra"
- "github.com/runningwater/gohub/app/cmd"
- "github.com/runningwater/gohub/bootstrap"
- appConfig "github.com/runningwater/gohub/config"
- "github.com/runningwater/gohub/pkg/config"
- "github.com/runningwater/gohub/pkg/console"
- )
- func init() {
- // 加载 config 目录下的配置信息
- appConfig.Initialize()
- }
- func main() {
- // 应用主入口,默认调用 cmd.CmdServe 命令
- // 该命令会启动 HTTP 服务
- var rootCmd = &cobra.Command{
- Use: "gohub",
- Short: "gohub is a simple forum project",
- Long: `Default will run "serve" command, you can use "-h" flag to see all subcommands`,
- // 所有子命令都会调用以下代码来初始化配置信息
- PersistentPreRun: func(command *cobra.Command, args []string) {
- // 配置初始化,依赖命令行 --env 参数
- config.InitConfig(cmd.Env)
- // 初始化 Logger
- bootstrap.SetupLogger()
- // 初始化 DB
- // 注意: 初始化 DB 前应该先初始化 logger
- bootstrap.SetupDB()
- // 初始化 Redis
- bootstrap.SetupRedis()
- },
- }
- // 注册子命令
- rootCmd.AddCommand(
- cmd.CmdServe,
- cmd.CmdKey,
- cmd.CmdPlay,
- )
- // 配置默认运行 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()))
- }
- }
|