diff --git a/.gitignore b/.gitignore index eb8c3b8..b00fa04 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ .data *.sqlite *.sqlite3 -xtest \ No newline at end of file +xtest +*.sock \ No newline at end of file diff --git a/etc/config.json b/etc/config.json index bb2e5a3..bc52d45 100644 --- a/etc/config.json +++ b/etc/config.json @@ -1,7 +1,8 @@ { "name": "ult", "listen": { - "http": "0.0.0.0:8080" + "http": "0.0.0.0:8080", + "unix": "unix://./dev.sock" }, "db": { "_uri": "postgres::host=pg.dev user=xx_user password=xx_password dbname=xx_database port=5432 sslmode=disable TimeZone=Asia/Shanghai", @@ -24,4 +25,4 @@ "username": "admin", "password": "password" } -} \ No newline at end of file +} diff --git a/go.mod b/go.mod index 5a0f7f0..a2934af 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/samber/lo v1.39.0 github.com/sirupsen/logrus v1.9.2 github.com/spf13/cast v1.6.0 + github.com/spf13/cobra v1.8.1 github.com/tdewolff/minify/v2 v2.20.16 golang.org/x/crypto v0.23.0 google.golang.org/grpc v1.50.0 @@ -36,6 +37,7 @@ require ( github.com/glebarez/go-sqlite v1.21.2 // indirect github.com/go-sql-driver/mysql v1.7.0 // indirect github.com/golang/protobuf v1.5.2 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/jackc/chunkreader/v2 v2.0.1 // indirect github.com/jackc/pgconn v1.13.0 // indirect github.com/jackc/pgio v1.0.0 // indirect @@ -51,6 +53,7 @@ require ( github.com/pkg/errors v0.9.1 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/rivo/uniseg v0.2.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect github.com/stretchr/testify v1.9.0 // indirect github.com/tdewolff/parse/v2 v2.7.11 // indirect github.com/vesoft-inc/nebula-go/v3 v3.5.0 // indirect diff --git a/go.sum b/go.sum index b5f1edb..78f3862 100644 --- a/go.sum +++ b/go.sum @@ -13,6 +13,7 @@ github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= +github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -76,6 +77,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo= github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk= github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8= @@ -183,6 +186,7 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA= github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= @@ -195,6 +199,10 @@ github.com/sirupsen/logrus v1.9.2 h1:oxx1eChJGI6Uks2ZC4W1zpLlVgqB8ner4EuQwV4Ik1Y github.com/sirupsen/logrus v1.9.2/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= +github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= diff --git a/internal/api/api.go b/internal/api/api.go index ecbf7f8..8091cd7 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -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))}) } }()) diff --git a/internal/cmd/cli.go b/internal/cmd/cli.go new file mode 100644 index 0000000..c4dd0da --- /dev/null +++ b/internal/cmd/cli.go @@ -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 + }, + }) +} diff --git a/internal/cmd/execute.go b/internal/cmd/execute.go index ebd6dc9..34db7f3 100644 --- a/internal/cmd/execute.go +++ b/internal/cmd/execute.go @@ -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) +} diff --git a/internal/cmd/init.go b/internal/cmd/init.go index 5eef46f..e153c60 100644 --- a/internal/cmd/init.go +++ b/internal/cmd/init.go @@ -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() } diff --git a/internal/cmd/root.go b/internal/cmd/root.go new file mode 100644 index 0000000..e3ff471 --- /dev/null +++ b/internal/cmd/root.go @@ -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()) + }, + } +) diff --git a/internal/opt/opt.go b/internal/opt/opt.go index cd16e11..5503307 100644 --- a/internal/opt/opt.go +++ b/internal/opt/opt.go @@ -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 diff --git a/internal/opt/var.go b/internal/opt/var.go index 9009a51..d34979d 100644 --- a/internal/opt/var.go +++ b/internal/opt/var.go @@ -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 ) diff --git a/internal/unix/handler.go b/internal/unix/handler.go new file mode 100644 index 0000000..8122f6c --- /dev/null +++ b/internal/unix/handler.go @@ -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 +} diff --git a/internal/unix/start.go b/internal/unix/start.go new file mode 100644 index 0000000..7d7f5ad --- /dev/null +++ b/internal/unix/start.go @@ -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 +} diff --git a/main.go b/main.go index c9e67fa..9a2ac2c 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,6 @@ import ( "os/signal" "syscall" "ultone/internal/cmd" - "ultone/internal/tool" ) func main() { @@ -16,7 +15,4 @@ func main() { if err := cmd.Execute(ctx); err != nil { log.Error("cmd.Execute: err=%v", err) } - - log.Warn("received quit signal...(2s)") - <-tool.Timeout(2).Done() }