修复context问题和添加变量支持
This commit is contained in:
parent
5dac511cae
commit
a3835e2ead
11
cmd/main.go
11
cmd/main.go
|
@ -7,6 +7,7 @@ import (
|
||||||
"git.nspix.com/golang/kos/entry/cli"
|
"git.nspix.com/golang/kos/entry/cli"
|
||||||
"git.nspix.com/golang/kos/entry/http"
|
"git.nspix.com/golang/kos/entry/http"
|
||||||
httpkg "net/http"
|
httpkg "net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.nspix.com/golang/kos"
|
"git.nspix.com/golang/kos"
|
||||||
)
|
)
|
||||||
|
@ -28,18 +29,28 @@ func (s *subServer) Start(ctx context.Context) (err error) {
|
||||||
kos.Http().Handle(httpkg.MethodGet, "/hello", func(ctx *http.Context) (err error) {
|
kos.Http().Handle(httpkg.MethodGet, "/hello", func(ctx *http.Context) (err error) {
|
||||||
return ctx.Success("Hello World")
|
return ctx.Success("Hello World")
|
||||||
})
|
})
|
||||||
|
|
||||||
kos.Command().Handle("/test", "test command", func(ctx *cli.Context) (err error) {
|
kos.Command().Handle("/test", "test command", func(ctx *cli.Context) (err error) {
|
||||||
return ctx.Success([][]string{
|
return ctx.Success([][]string{
|
||||||
[]string{"NAME", "AGE"},
|
[]string{"NAME", "AGE"},
|
||||||
[]string{"SSS", "aaa"},
|
[]string{"SSS", "aaa"},
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
kos.Command().Handle("/users", "test command", func(ctx *cli.Context) (err error) {
|
kos.Command().Handle("/users", "test command", func(ctx *cli.Context) (err error) {
|
||||||
return ctx.Success([]*users{
|
return ctx.Success([]*users{
|
||||||
{Name: "Zhan", Age: 10, Tags: []string{"a", "b"}},
|
{Name: "Zhan", Age: 10, Tags: []string{"a", "b"}},
|
||||||
{Name: "Lisi", Age: 15, Tags: []string{"c", "d"}},
|
{Name: "Lisi", Age: 15, Tags: []string{"c", "d"}},
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
kos.Command().Handle("/ctx", "context test", func(ctx *cli.Context) (err error) {
|
||||||
|
select {
|
||||||
|
case <-ctx.Context().Done():
|
||||||
|
case <-time.After(time.Second * 2):
|
||||||
|
}
|
||||||
|
return ctx.Success("OK")
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,15 +5,18 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Context struct {
|
type Context struct {
|
||||||
Id int64
|
Id int64
|
||||||
seq uint16
|
seq uint16
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
wc io.WriteCloser
|
wc io.WriteCloser
|
||||||
params map[string]string
|
params map[string]string
|
||||||
args []string
|
locker sync.RWMutex
|
||||||
|
variables map[string]any
|
||||||
|
args []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *Context) reset(id int64, wc io.WriteCloser) {
|
func (ctx *Context) reset(id int64, wc io.WriteCloser) {
|
||||||
|
@ -23,6 +26,7 @@ func (ctx *Context) reset(id int64, wc io.WriteCloser) {
|
||||||
ctx.ctx = context.Background()
|
ctx.ctx = context.Background()
|
||||||
ctx.args = make([]string, 0)
|
ctx.args = make([]string, 0)
|
||||||
ctx.params = make(map[string]string)
|
ctx.params = make(map[string]string)
|
||||||
|
ctx.variables = make(map[string]any)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *Context) setArgs(args []string) {
|
func (ctx *Context) setArgs(args []string) {
|
||||||
|
@ -42,7 +46,7 @@ func (ctx *Context) setContext(c context.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *Context) Context() context.Context {
|
func (ctx *Context) Context() context.Context {
|
||||||
return ctx.Context()
|
return ctx.ctx
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *Context) Argument(index int) string {
|
func (ctx *Context) Argument(index int) string {
|
||||||
|
@ -59,6 +63,22 @@ func (ctx *Context) Param(s string) string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ctx *Context) SetValue(name string, value any) {
|
||||||
|
ctx.locker.Lock()
|
||||||
|
if ctx.variables == nil {
|
||||||
|
ctx.variables = make(map[string]any)
|
||||||
|
}
|
||||||
|
ctx.variables[name] = value
|
||||||
|
ctx.locker.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ctx *Context) GetValue(name string) (val any, ok bool) {
|
||||||
|
ctx.locker.RLock()
|
||||||
|
defer ctx.locker.RUnlock()
|
||||||
|
val, ok = ctx.variables[name]
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (ctx *Context) Success(v any) (err error) {
|
func (ctx *Context) Success(v any) (err error) {
|
||||||
return ctx.send(responsePayload{Type: PacketTypeCommand, Data: v})
|
return ctx.send(responsePayload{Type: PacketTypeCommand, Data: v})
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,7 +44,7 @@ func (svr *Server) releaseContext(ctx *Context) {
|
||||||
ctxPool.Put(ctx)
|
ctxPool.Put(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (svr *Server) handle(ctx *Context, frame *Frame) (err error) {
|
func (svr *Server) execute(ctx *Context, frame *Frame) (err error) {
|
||||||
var (
|
var (
|
||||||
params map[string]string
|
params map[string]string
|
||||||
tokens []string
|
tokens []string
|
||||||
|
@ -139,7 +139,7 @@ func (svr *Server) process(conn net.Conn) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
case PacketTypeCommand:
|
case PacketTypeCommand:
|
||||||
if err = svr.handle(ctx, frame); err != nil {
|
if err = svr.execute(ctx, frame); err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|
Loading…
Reference in New Issue