84 lines
1.6 KiB
Go
84 lines
1.6 KiB
Go
package template
|
|
|
|
const templateCmdExec = `package cmd
|
|
|
|
import (
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/spf13/cobra"
|
|
"gitter.top/common/lormatter"
|
|
|
|
"{{.Name}}/config"
|
|
)
|
|
|
|
var (
|
|
rootCmd = &cobra.Command{}
|
|
cfgFile string
|
|
)
|
|
|
|
func Execute() {
|
|
// 预加载配置文件
|
|
loadConfig()
|
|
if err := rootCmd.Execute(); err != nil {
|
|
logrus.Fatalf("exec command failed: %v", err)
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
var formatter = lormatter.Formatter{
|
|
ShowTime: true,
|
|
ShowFile: true,
|
|
}
|
|
formatter.Register()
|
|
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "config_dev.yaml", "config file")
|
|
rootCmd.AddCommand(apiServerCommand) // API服务
|
|
rootCmd.AddCommand(grpcServerCommand) // GRPC服务
|
|
}
|
|
|
|
func loadConfig() {
|
|
// 初始化配置文件
|
|
config.New(cfgFile)
|
|
conf := config.Get()
|
|
if err := conf.Load(); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
`
|
|
|
|
const templateCmdNewServe = `package cmd
|
|
|
|
import (
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/spf13/cobra"
|
|
|
|
"{{.Name}}/register"
|
|
)
|
|
|
|
var (
|
|
apiServerCommand = &cobra.Command{
|
|
Use: "api",
|
|
Short: "start api server",
|
|
Long: "start api server",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
router := register.NewRegister()
|
|
if err := router.NewRouter(); err != nil {
|
|
logrus.Errorf("register router failed: %v", err)
|
|
}
|
|
},
|
|
}
|
|
)
|
|
|
|
var (
|
|
grpcServerCommand = &cobra.Command{
|
|
Use: "grpc",
|
|
Short: "start grpc server",
|
|
Long: "start grpc server",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
router := register.NewRegister()
|
|
if err := router.NewGrpc(); err != nil {
|
|
logrus.Errorf("register grpc failed: %v", err)
|
|
}
|
|
},
|
|
}
|
|
)
|
|
`
|