feat: 完成基本的 model
This commit is contained in:
@ -11,8 +11,10 @@ var (
|
||||
root *cobra.Command
|
||||
)
|
||||
|
||||
func Init(ctx context.Context) {
|
||||
root = rootCmd()
|
||||
func Init() {
|
||||
root = rootCmd(
|
||||
svcCmd(),
|
||||
)
|
||||
}
|
||||
|
||||
func Execute(ctx context.Context) {
|
||||
|
@ -26,6 +26,7 @@ func rootCmd(subCmds ...*cobra.Command) *cobra.Command {
|
||||
}
|
||||
|
||||
cmd.PersistentFlags().BoolVar(&opt.Cfg.Debug, "debug", false, "debug mode")
|
||||
cmd.PersistentFlags().StringVar(&opt.Cfg.Name, "name", "utodo", "app name")
|
||||
|
||||
cmd.AddCommand(subCmds...)
|
||||
|
||||
|
55
internal/cmd/svc.go
Normal file
55
internal/cmd/svc.go
Normal file
@ -0,0 +1,55 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"loveuer/utodo/internal/model"
|
||||
"loveuer/utodo/internal/opt"
|
||||
g_api "loveuer/utodo/pkg/api"
|
||||
|
||||
"gitea.loveuer.com/yizhisec/packages/database/db"
|
||||
"gitea.loveuer.com/yizhisec/packages/tool"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func svcCmd() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "svc",
|
||||
Short: "run todo service",
|
||||
RunE: svcRun,
|
||||
}
|
||||
|
||||
cmd.Flags().StringVar(&opt.Cfg.Svc.Address, "address", ":9119", "address to listen on")
|
||||
cmd.Flags().StringVar(&opt.Cfg.Svc.DBFile, "db", "/data/data.db", "database file")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func svcRun(cmd *cobra.Command, args []string) error {
|
||||
var (
|
||||
err error
|
||||
stopApi func(context.Context) error
|
||||
)
|
||||
|
||||
if err = db.Init(db.WithSqlite(opt.Cfg.Svc.DBFile)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = model.Init(cmd.Context(), db.Default.Session(tool.Timeout(60))); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if stopApi, err = g_api.Start(
|
||||
cmd.Context(),
|
||||
g_api.WithAddress(opt.Cfg.Svc.Address),
|
||||
g_api.WithVersion(opt.Cfg.Version),
|
||||
g_api.WithName(opt.Cfg.Name),
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
<-cmd.Context().Done()
|
||||
|
||||
tool.MustStop(tool.Timeout(3), stopApi)
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user