wip: cli
This commit is contained in:
@ -24,10 +24,9 @@ func initApp(ctx context.Context) *nf.App {
|
||||
// for example: app := engine.Group("/api/{project}")
|
||||
app := engine.Group("/api")
|
||||
app.Get("/available", func() nf.HandlerFunc {
|
||||
start := time.Now()
|
||||
return func(c *nf.Ctx) error {
|
||||
now := time.Now()
|
||||
return resp.Resp200(c, nf.Map{"ok": true, "start": start, "now": now, "duration": fmt.Sprint(now.Sub(start))})
|
||||
return resp.Resp200(c, nf.Map{"ok": opt.OK, "start": opt.Start, "now": now, "duration": fmt.Sprint(now.Sub(opt.Start))})
|
||||
}
|
||||
}())
|
||||
|
||||
|
73
internal/cmd/cli.go
Normal file
73
internal/cmd/cli.go
Normal file
@ -0,0 +1,73 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"net/rpc"
|
||||
"net/url"
|
||||
"ultone/internal/log"
|
||||
"ultone/internal/unix"
|
||||
)
|
||||
|
||||
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", "unix:///tmp/.cli.sock", "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)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
})
|
||||
}
|
@ -6,17 +6,18 @@ import (
|
||||
"ultone/internal/controller"
|
||||
"ultone/internal/database/cache"
|
||||
"ultone/internal/database/db"
|
||||
"ultone/internal/log"
|
||||
"ultone/internal/model"
|
||||
"ultone/internal/opt"
|
||||
"ultone/internal/tool"
|
||||
"ultone/internal/unix"
|
||||
)
|
||||
|
||||
var (
|
||||
filename string
|
||||
)
|
||||
|
||||
func Execute(ctx context.Context) error {
|
||||
|
||||
func execute(ctx context.Context) error {
|
||||
tool.Must(opt.Init(filename))
|
||||
tool.Must(db.Init())
|
||||
tool.Must(cache.Init())
|
||||
@ -31,7 +32,17 @@ func Execute(ctx context.Context) error {
|
||||
tool.Must(controller.Init(ctx))
|
||||
tool.Must(api.Start(ctx))
|
||||
|
||||
// todo: if need some cli operation, should start local unix rpc svc
|
||||
tool.Must(unix.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)
|
||||
}
|
||||
|
@ -1,16 +1,12 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"time"
|
||||
"ultone/internal/opt"
|
||||
)
|
||||
|
||||
func init() {
|
||||
time.Local = time.FixedZone("CST", 8*3600)
|
||||
|
||||
flag.StringVar(&filename, "c", "etc/config.json", "config json file path")
|
||||
flag.BoolVar(&opt.Debug, "debug", false, "")
|
||||
|
||||
flag.Parse()
|
||||
initRoot()
|
||||
initCli()
|
||||
}
|
||||
|
29
internal/cmd/root.go
Normal file
29
internal/cmd/root.go
Normal file
@ -0,0 +1,29 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/loveuer/nf/nft/log"
|
||||
"github.com/spf13/cobra"
|
||||
"ultone/internal/opt"
|
||||
)
|
||||
|
||||
func initRoot() {
|
||||
rootCommand.PersistentFlags().BoolVar(&opt.Debug, "debug", false, "debug mode")
|
||||
rootCommand.PersistentFlags().StringVarP(&filename, "config", "c", "etc/config.json", "config json file path")
|
||||
rootCommand.PersistentPreRun = func(cmd *cobra.Command, args []string) {
|
||||
if opt.Debug {
|
||||
log.SetLogLevel(log.LogLevelDebug)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
rootCommand.AddCommand(cliCommand)
|
||||
}
|
||||
|
||||
var (
|
||||
rootCommand = &cobra.Command{
|
||||
Use: "nf-app",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return execute(cmd.Context())
|
||||
},
|
||||
}
|
||||
)
|
@ -48,6 +48,7 @@ type config struct {
|
||||
|
||||
var (
|
||||
Debug bool
|
||||
Mode string
|
||||
Cfg = &config{}
|
||||
)
|
||||
|
||||
@ -68,10 +69,6 @@ func Init(filename string) error {
|
||||
return fmt.Errorf("opt.Init: json marshal config=%s err=%v", string(bs), err)
|
||||
}
|
||||
|
||||
if Debug {
|
||||
log.SetLogLevel(log.LogLevelDebug)
|
||||
}
|
||||
|
||||
tool.TablePrinter(Cfg)
|
||||
|
||||
return nil
|
||||
|
@ -1,6 +1,9 @@
|
||||
package opt
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
// todo: 可以替换自己生生成的 secret
|
||||
@ -40,6 +43,10 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
Locker = &sync.Mutex{}
|
||||
// todo: 颁发的 token, (cookie) 在缓存中存在的时间 (每次请求该时间也会被刷新)
|
||||
TokenTimeout = time.Duration(3600*12) * time.Second
|
||||
|
||||
Start = time.Now()
|
||||
OK bool
|
||||
)
|
||||
|
56
internal/unix/handler.go
Normal file
56
internal/unix/handler.go
Normal file
@ -0,0 +1,56 @@
|
||||
package unix
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
"ultone/internal/log"
|
||||
"ultone/internal/opt"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
Ctx context.Context
|
||||
}
|
||||
|
||||
type AvailableReq struct{}
|
||||
type AvailableResp struct {
|
||||
OK bool
|
||||
Now time.Time
|
||||
Start time.Time
|
||||
Duration string
|
||||
}
|
||||
|
||||
func (*Handler) Available(_ *AvailableReq, out *AvailableResp) error {
|
||||
now := time.Now()
|
||||
out.OK, out.Now = opt.OK, now
|
||||
out.Start = opt.Start
|
||||
out.Duration = fmt.Sprint(now.Sub(opt.Start))
|
||||
return nil
|
||||
}
|
||||
|
||||
type SettingReq struct {
|
||||
Debug bool
|
||||
}
|
||||
type Resp[T any] struct {
|
||||
Status uint32
|
||||
Msg string
|
||||
Data T
|
||||
}
|
||||
|
||||
func (h *Handler) Setting(in *SettingReq, out *Resp[bool]) error {
|
||||
opt.Locker.Lock()
|
||||
defer opt.Locker.Unlock()
|
||||
|
||||
if in.Debug {
|
||||
opt.Debug = true
|
||||
log.Info(h.Ctx, "set global debug[true]")
|
||||
} else {
|
||||
opt.Debug = false
|
||||
log.Info(h.Ctx, "set global debug[false]")
|
||||
}
|
||||
|
||||
out.Status = 200
|
||||
out.Msg = "操作成功"
|
||||
|
||||
return nil
|
||||
}
|
51
internal/unix/start.go
Normal file
51
internal/unix/start.go
Normal file
@ -0,0 +1,51 @@
|
||||
package unix
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/rpc"
|
||||
"net/url"
|
||||
"ultone/internal/log"
|
||||
"ultone/internal/opt"
|
||||
)
|
||||
|
||||
func Start(ctx context.Context) error {
|
||||
ready := make(chan bool)
|
||||
defer close(ready)
|
||||
|
||||
uri, err := url.Parse(opt.Cfg.Listen.Unix)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
address := uri.Host + uri.Path
|
||||
log.Debug(ctx, "[rpc-svc] listen at [%s] [%s]", uri.Scheme, address)
|
||||
|
||||
ln, err := net.Listen(uri.Scheme, address)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
go func() {
|
||||
ready <- true
|
||||
<-ctx.Done()
|
||||
_ = ln.Close()
|
||||
}()
|
||||
|
||||
<-ready
|
||||
|
||||
svc := rpc.NewServer()
|
||||
if err = svc.RegisterName("svc", &Handler{Ctx: ctx}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
go func() {
|
||||
log.Info(ctx, "[rpc-svc] start at: [%s] [%s]", uri.Scheme, address)
|
||||
ready <- true
|
||||
svc.Accept(ln)
|
||||
}()
|
||||
|
||||
<-ready
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user