package cmd import ( "context" "loveuer/utodo/internal/api" "loveuer/utodo/internal/model" "loveuer/utodo/internal/opt" g_api "loveuer/utodo/pkg/api" "loveuer/utodo/pkg/database/cache" "loveuer/utodo/pkg/tool" "gitea.loveuer.com/yizhisec/packages/database/db" "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") cmd.Flags().StringVar(&opt.Cfg.Svc.Cache, "cache", "", "cache uri: empty for memory cache; for redis(rdp://user:password@host:port)") return cmd } func svcRun(cmd *cobra.Command, args []string) error { var ( err error stopApi func(context.Context) error ) if err = db.Init(db.WithCtx(cmd.Context()), db.WithSqlite(opt.Cfg.Svc.DBFile)); err != nil { return err } if err = cache.Init( cache.WithCtx(cmd.Context()), tool.If(opt.Cfg.Svc.Cache == "", cache.WithMemory(), cache.WithRedisURI(opt.Cfg.Svc.Cache)), ); err != nil { return err } if err = model.Init(cmd.Context(), db.Default.Session(tool.Timeout(60), db.Config{Debug: opt.Cfg.Debug})); 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), g_api.WithApp(api.New(cmd.Context())), ); err != nil { return err } <-cmd.Context().Done() tool.MustStop(tool.Timeout(3), stopApi) return nil }