wip: 完成 client api 分析

This commit is contained in:
loveuer
2024-12-19 15:03:36 +08:00
commit 64cdd0cb0e
76 changed files with 5146 additions and 0 deletions

78
internal/cmd/cli.go Normal file
View File

@ -0,0 +1,78 @@
package cmd
import (
"fmt"
"net/rpc"
"net/url"
"esway/internal/log"
"esway/internal/opt"
"esway/internal/unix"
"github.com/spf13/cobra"
)
var (
cliCommand = &cobra.Command{
Use: "cli",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
log.Debug(cmd.Context(), "[cmd.cli] svc address: %s", svc)
uri, err := url.Parse(svc)
if err != nil {
return err
}
if cliClient, err = rpc.Dial(uri.Scheme, uri.Host+uri.Path); err != nil {
return fmt.Errorf("rpc dial [%s] [%s] err: %w", uri.Scheme, uri.Host+uri.Path, err)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
log.Debug(cmd.Context(), "[cli] start run cli... all args: %v", args)
return nil
},
}
svc string
cliClient *rpc.Client
)
func initCli() {
cliCommand.PersistentFlags().StringVar(&svc, "svc", opt.RpcAddress, "server unix listen address")
cliCommand.AddCommand(&cobra.Command{
Use: "set",
RunE: func(cmd *cobra.Command, args []string) (err error) {
log.Debug(cmd.Context(), "[cli.set] all args: %v", args)
if len(args) < 2 {
return fmt.Errorf("at least 2 args required")
}
switch args[0] {
case "debug":
out := &unix.Resp[bool]{}
in := &unix.SettingReq{Debug: false}
switch args[1] {
case "true":
in.Debug = true
case "false":
default:
return fmt.Errorf("unknown debug value")
}
if err = cliClient.Call("svc.Setting", in, out); err != nil {
return err
}
log.Info(cmd.Context(), out.Msg)
default:
return fmt.Errorf("unknown set variable(debug is available now)")
}
return nil
},
})
}

37
internal/cmd/execute.go Normal file
View File

@ -0,0 +1,37 @@
package cmd
import (
"context"
"esway/internal/api"
"esway/internal/controller"
"esway/internal/database/cache"
"esway/internal/database/db"
"esway/internal/gateway"
"esway/internal/log"
"esway/internal/model"
"esway/internal/opt"
"esway/internal/tool"
)
func execute(ctx context.Context) error {
tool.Must(opt.Init(opt.Cfg.Config))
tool.Must(db.Init(ctx, opt.Cfg.DB.Uri))
tool.Must(cache.Init())
tool.Must(model.Init(db.Default.Session()))
tool.Must(controller.Init(ctx))
tool.Must(gateway.Start(ctx))
tool.Must(api.Start(ctx))
<-ctx.Done()
log.Warn(ctx, "received quit signal...(2s)")
<-tool.Timeout(2).Done()
return nil
}
func Execute(ctx context.Context) error {
return rootCommand.ExecuteContext(ctx)
}

13
internal/cmd/init.go Normal file
View File

@ -0,0 +1,13 @@
package cmd
import (
"time"
)
func init() {
time.Local = time.FixedZone("CST", 8*3600)
initCli()
initRoot(cliCommand)
}

26
internal/cmd/root.go Normal file
View File

@ -0,0 +1,26 @@
package cmd
import (
"esway/internal/opt"
"github.com/loveuer/nf/nft/log"
"github.com/spf13/cobra"
)
var rootCommand = &cobra.Command{
Use: "esway",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if opt.Cfg.Debug {
log.SetLogLevel(log.LogLevelDebug)
}
},
RunE: func(cmd *cobra.Command, args []string) error {
return execute(cmd.Context())
},
}
func initRoot(cmds ...*cobra.Command) {
rootCommand.PersistentFlags().BoolVar(&opt.Cfg.Debug, "debug", false, "debug mode")
rootCommand.PersistentFlags().StringVarP(&opt.Cfg.Config, "config", "c", "etc/config.json", "config json file path")
rootCommand.AddCommand(cmds...)
}