From 8eb0db0dc101bf1d46a92428a37117bd271531fa Mon Sep 17 00:00:00 2001 From: cdle Date: Fri, 2 Jun 2023 16:52:49 +0800 Subject: [PATCH] x --- core/adapter.go | 52 ++++++++++++++++++------------------- core/node_web.go | 42 ++++++++++++++++++++++++++++-- core/plugin_web_response.go | 10 ++----- core/web.go | 42 +++++++++++++++++++++++++++++- 4 files changed, 109 insertions(+), 37 deletions(-) diff --git a/core/adapter.go b/core/adapter.go index 180f497..3b25eed 100644 --- a/core/adapter.go +++ b/core/adapter.go @@ -269,33 +269,33 @@ func GetReplyMessage(vm *goja.Runtime, plt string, bots_id []string) *goja.Promi for k, v := range mc.Msg { msg[k] = v } - // obj := adapters[i].vm.NewObject() - // for k, v := range mc.Msg { - // obj.Set(k, v) - // } - // obj.Set("bot_id", adapters[i].botid) - msg["bot_id"] = adapters[i].botid - msg["setMessageId"] = func(id string) { - select { - case <-mc.Chan: - case <-time.After(time.Millisecond): - mc.Chan <- id - } + obj := adapters[i].vm.NewObject() + for k, v := range mc.Msg { + obj.Set(k, v) } - resolve(mc.Msg) - // adapters[i].vm.NewProxy(obj, &goja.ProxyTrapConfig{ - // Set: func(target *goja.Object, property string, value, receiver goja.Value) (success bool) { - // if property == "message_id" { - // select { - // case <-mc.Chan: - // return false - // case <-time.After(time.Millisecond): - // } - // mc.Chan <- fmt.Sprint(value.Export()) - // } - // return true - // }, - // }) + obj.Set("bot_id", adapters[i].botid) + // msg["bot_id"] = adapters[i].botid + // msg["setMessageId"] = func(id string) { + // select { + // case <-mc.Chan: + // case <-time.After(time.Millisecond): + // mc.Chan <- id + // } + // } + // resolve(msg) + resolve(vm.NewProxy(obj, &goja.ProxyTrapConfig{ + Set: func(target *goja.Object, property string, value, receiver goja.Value) (success bool) { + if property == "message_id" { + select { + case <-mc.Chan: + return false + case <-time.After(time.Millisecond): + } + mc.Chan <- fmt.Sprint(value.Export()) + } + return true + }, + })) } }(i) } diff --git a/core/node_web.go b/core/node_web.go index 9f36060..f3c4e94 100644 --- a/core/node_web.go +++ b/core/node_web.go @@ -23,15 +23,42 @@ type HttpListen struct { } var httpListens sync.Map +var httpListensAny sync.Map var listenCounter2 int64 func AddHttpListen(api, method string, vm *goja.Runtime, uuid string, resolve func(result interface{}), reject func(reason interface{})) { - key := atomic.AddInt64(&listenCounter2, 1) + if method == "ANY" { + var hl *HttpListen + v, ok := httpListensAny.Load(uuid) + if ok { + // logs.Debug("load", uuid) + hl = v.(*HttpListen) + } else { + hl = &HttpListen{ + Path: api, + Method: method, + Chan: make(chan *RR, 1000), + } + // logs.Debug("crate", uuid) + httpListensAny.Store(uuid, hl) + } + f, ok := <-hl.Chan + if ok { + // logs.Debug("http resolved") + resolve(f) + } else { + // logs.Debug("script is stopped") + reject(Error(vm, "script is stopped")) + } + return + } hl := &HttpListen{ Path: api, Method: method, - Chan: make(chan *RR, 1), + UUID: uuid, } + key := atomic.AddInt64(&listenCounter2, 1) + hl.Chan = make(chan *RR, 1) httpListens.Store(key, hl) f, ok := <-hl.Chan if ok { @@ -53,4 +80,15 @@ func CancelHttpListen(uuid string) { } return true }) + httpListensAny.Range(func(key, value any) bool { + hl := value.(*HttpListen) + if hl.UUID == uuid { + httpListens.Delete(key) + if !hl.Closed { + hl.Closed = true + close(hl.Chan) + } + } + return true + }) } diff --git a/core/plugin_web_response.go b/core/plugin_web_response.go index 4ffda0e..2dbab7f 100644 --- a/core/plugin_web_response.go +++ b/core/plugin_web_response.go @@ -3,7 +3,6 @@ package core import ( "fmt" "net/http" - "strings" "github.com/cdle/sillyplus/utils" "github.com/dop251/goja" @@ -62,14 +61,9 @@ func (r *Response) Json(ps ...interface{}) *Response { } d, err := json.Marshal(data) f := string(d) + r.status = int(status) if err == nil { - if strings.Contains(f, `"error"`) { - f = strings.Replace(f, "error", "msg", 1) - if status == 200 { - status = -1 - } - } - r.content = strings.Replace(f, "{", fmt.Sprintf(`{"status":%d, `, status), 1) + r.content = f r.isJson = true } else { r.content += fmt.Sprint(data) diff --git a/core/web.go b/core/web.go index 37fb477..e9cb874 100644 --- a/core/web.go +++ b/core/web.go @@ -87,6 +87,47 @@ func init() { var res = &Response{ c: c, } + + httpListensAny.Range(func(_, value any) bool { + web := value.(*HttpListen) + if web.Closed { + return true + } + req.handled = true + var end = make(chan bool, 1) + web.Chan <- &RR{ + Req: req, + Res: res, + End: func() { + if !web.Closed { + end <- true + } + }, + } + select { + case <-end: + // fmt.Println("end", req.handled) + case <-time.After(time.Second * 10): + logs.Warn("%s 响应超时", c.Request.URL.Path) + } + close(end) + if req.handled { + if res.isRedirect { + return false + } + if res.isJson { + c.Header("Content-Type", "application/json") + } + // fmt.Println(res.status, res.content) + c.String(res.status, res.content) + return false + } + return true + }) + + if req.handled { + return + } httpListens.Range(func(key, value any) bool { web := value.(*HttpListen) if web.Closed { @@ -108,7 +149,6 @@ func init() { httpListens.Delete(key) req.handled = true var end = make(chan bool, 1) - // fmt.Println(c.Request.URL.Path, key) web.Chan <- &RR{ Req: req, Res: res,