From 73580d0df5f9fb3b23a29666a5506c04c6a87d3a Mon Sep 17 00:00:00 2001 From: shaoying Date: Sat, 3 Dec 2022 09:38:31 +0800 Subject: [PATCH] opt web --- base/aaa/offer.go | 2 +- base/aaa/role.go | 1 - base/web/broad.go | 8 ++++---- base/web/cache.go | 1 - base/web/dream.go | 4 ++-- base/web/serve.go | 4 +--- base/web/spide.go | 50 ++++++++++++++++++++++++++++++++--------------- base/web/web.go | 2 +- 8 files changed, 43 insertions(+), 29 deletions(-) diff --git a/base/aaa/offer.go b/base/aaa/offer.go index 64b6b2b1..867a44ee 100644 --- a/base/aaa/offer.go +++ b/base/aaa/offer.go @@ -14,7 +14,7 @@ const OFFER = "offer" func init() { Index.MergeCommands(ice.Commands{ - OFFER: {Name: "offer hash auto", Actions: ice.MergeActions(ice.Actions{ + OFFER: {Name: "offer hash auto", Help: "邀请", Actions: ice.MergeActions(ice.Actions{ INVITE: {Name: "invite email='shylinux@163.com' content", Help: "邀请", Hand: func(m *ice.Message, arg ...string) { h := mdb.HashCreate(m, m.OptionSimple(EMAIL, "content"), "from", m.Option(ice.MSG_USERNAME)) msg := m.Cmd("web.share", mdb.CREATE, mdb.TYPE, "field", mdb.NAME, m.PrefixKey(), mdb.TEXT, kit.Format(kit.List(h)), diff --git a/base/aaa/role.go b/base/aaa/role.go index 3765fb02..7204844c 100644 --- a/base/aaa/role.go +++ b/base/aaa/role.go @@ -135,7 +135,6 @@ func Right(m *ice.Message, key ...ice.Any) bool { return m.Option(ice.MSG_USERROLE) == ROOT || !m.Warn(m.Cmdx(ROLE, RIGHT, m.Option(ice.MSG_USERROLE), key) != ice.OK, ice.ErrNotRight, kit.Keys(key...), USERROLE, m.Option(ice.MSG_USERROLE), logs.FileLineMeta(2)) } - func White(m *ice.Message, key ...string) { for _, key := range key { m.Cmd(ROLE, WHITE, VOID, key) diff --git a/base/web/broad.go b/base/web/broad.go index da7861cf..941bfda2 100644 --- a/base/web/broad.go +++ b/base/web/broad.go @@ -19,7 +19,7 @@ func _broad_addr(m *ice.Message, host, port string) *net.UDPAddr { return nil } func _broad_send(m *ice.Message, host, port string, remote_host, remote_port string) { - if s, e := net.DialUDP("udp", nil, _broad_addr(m, remote_host, remote_port)); !m.Warn(e, ice.ErrNotValid) { + if s, e := net.DialUDP("udp4", nil, _broad_addr(m, remote_host, remote_port)); !m.Warn(e, ice.ErrNotValid) { defer s.Close() msg := m.Spawn(kit.Dict(tcp.HOST, host, tcp.PORT, port)) m.Logs(mdb.EXPORT, BROAD, msg.FormatMeta(), "to", remote_host+ice.DF+remote_port) @@ -33,11 +33,11 @@ func _broad_serve(m *ice.Message, host, port string) { defer mdb.HashCreateDeferRemove(m, tcp.HOST, host, tcp.PORT, port, kit.Dict(mdb.TARGET, s))() buf := make([]byte, ice.MOD_BUFS) for { - n, addr, err := s.ReadFromUDP(buf[:]) - if err != nil { + n, from, e := s.ReadFromUDP(buf[:]) + if e != nil { break } - m.Logs(mdb.IMPORT, BROAD, string(buf[:n]), "from", addr) + m.Logs(mdb.IMPORT, BROAD, string(buf[:n]), "from", from) msg := m.Spawn(buf[:n]) if m.Cmd(BROAD, kit.Format("%s,%s", msg.Option(tcp.HOST), msg.Option(tcp.PORT))).Length() > 0 { continue diff --git a/base/web/cache.go b/base/web/cache.go index cd1ecf0d..1b5edff7 100644 --- a/base/web/cache.go +++ b/base/web/cache.go @@ -71,7 +71,6 @@ func _cache_upload(m *ice.Message, r *http.Request) (mime, name, file, size stri return "", "", "", "0" } func _cache_download(m *ice.Message, r *http.Response, file string, cb ice.Any) string { - defer r.Body.Close() if f, p, e := miss.CreateFile(file); !m.Warn(e, ice.ErrNotValid, DOWNLOAD) { defer f.Close() last, step, bufs := 0, 10, 10*ice.MOD_BUFS diff --git a/base/web/dream.go b/base/web/dream.go index e788e16f..6391ccb9 100644 --- a/base/web/dream.go +++ b/base/web/dream.go @@ -59,8 +59,8 @@ func _dream_show(m *ice.Message, name string) { ), cli.CMD_OUTPUT, path.Join(p, ice.BIN_BOOT_LOG)) defer m.Options(cli.CMD_DIR, "", cli.CMD_ENV, "", cli.CMD_OUTPUT, "") gdb.Event(m, DREAM_CREATE, m.OptionSimple(mdb.NAME, mdb.TYPE)) - bin := kit.Select(os.Args[0], cli.SystemFind(m, ice.ICE_BIN, nfs.PWD+path.Join(p, ice.BIN), nfs.PWD+ice.BIN)) - m.Cmd(cli.DAEMON, bin, SPACE, tcp.DIAL, ice.DEV, ice.OPS, mdb.TYPE, WORKER, m.OptionSimple(mdb.NAME, RIVER), cli.DAEMON, ice.OPS) + m.Cmd(cli.DAEMON, kit.Select(os.Args[0], cli.SystemFind(m, ice.ICE_BIN, nfs.PWD+path.Join(p, ice.BIN), nfs.PWD+ice.BIN)), + SPACE, tcp.DIAL, ice.DEV, ice.OPS, mdb.TYPE, WORKER, m.OptionSimple(mdb.NAME, RIVER), cli.DAEMON, ice.OPS) } func _dream_template(m *ice.Message, p string) { nfs.MkdirAll(m, p) diff --git a/base/web/serve.go b/base/web/serve.go index 1945cd4e..709131bc 100644 --- a/base/web/serve.go +++ b/base/web/serve.go @@ -139,7 +139,6 @@ const ( SERVE_PARSE = "serve.parse" SERVE_LOGIN = "serve.login" SERVE_CHECK = "serve.check" - SERVE_RENDER = "serve.render" SERVE_STOP = "serve.stop" WEB_LOGIN = "_login" @@ -233,7 +232,6 @@ func init() { if sub = kit.Select(sub, PP(key), sub == ice.PS); action.Hand == nil { action.Hand = func(m *ice.Message, arg ...string) { m.Cmdy(key, arg) } } - // c.Commands[sub] = &ice.Command{Name: sub, Help: cmd.Help, Actions: cmd.Actions, Hand: action.Hand} actions := ice.Actions{} for k, v := range cmd.Actions { switch k { @@ -247,5 +245,5 @@ func init() { }) } func ServeAction() ice.Actions { - return gdb.EventsAction(SERVE_START, SERVE_REWRITE, SERVE_PARSE, SERVE_LOGIN, SERVE_CHECK, SERVE_RENDER, SERVE_STOP) + return gdb.EventsAction(SERVE_START, SERVE_REWRITE, SERVE_PARSE, SERVE_LOGIN, SERVE_CHECK, SERVE_STOP) } diff --git a/base/web/spide.go b/base/web/spide.go index a06e65be..643b4cb0 100644 --- a/base/web/spide.go +++ b/base/web/spide.go @@ -13,6 +13,7 @@ import ( "time" ice "shylinux.com/x/icebergs" + "shylinux.com/x/icebergs/base/aaa" "shylinux.com/x/icebergs/base/cli" "shylinux.com/x/icebergs/base/mdb" "shylinux.com/x/icebergs/base/nfs" @@ -27,13 +28,13 @@ func _spide_create(m *ice.Message, name, address string) { dir, file := path.Split(uri.EscapedPath()) value[SPIDE_CLIENT] = kit.Dict(mdb.NAME, name, SPIDE_METHOD, http.MethodPost, "url", address, tcp.PROTOCOL, uri.Scheme, tcp.HOSTNAME, uri.Host, nfs.PATH, dir, nfs.FILE, file, "query", uri.RawQuery, - cli.TIMEOUT, "600s", LOGHEADERS, ice.FALSE, + cli.TIMEOUT, "30s", LOGHEADERS, ice.FALSE, ) }) } } func _spide_args(m *ice.Message, arg []string, val ...string) (string, []string) { - if kit.IndexOf(val, arg[0]) { + if kit.IndexOf(val, arg[0]) > -1 { return arg[0], arg[1:] } return "", arg @@ -53,6 +54,9 @@ func _spide_show(m *ice.Message, name string, arg ...string) { method = kit.Select(http.MethodPost, kit.Select(msg.Append(CLIENT_METHOD), method)) uri, arg := arg[0], arg[1:] body, head, arg := _spide_body(m, method, arg...) + if c, ok := body.(io.Closer); ok { + defer c.Close() + } req, e := http.NewRequest(method, kit.MergeURL2(msg.Append(CLIENT_URL), uri, arg), body) if m.Warn(e, ice.ErrNotValid, uri) { return @@ -100,6 +104,7 @@ func _spide_body(m *ice.Message, method string, arg ...string) (io.Reader, ice.M head[ContentType], body = ContentJSON, bytes.NewBufferString(kit.Select("{}", arg, 1)) case SPIDE_FILE: if f, e := nfs.OpenFile(m, arg[1]); m.Assert(e) { + m.Logs(mdb.IMPORT, nfs.FILE, arg[1]) body = f } case SPIDE_JSON: @@ -125,11 +130,11 @@ func _spide_part(m *ice.Message, arg ...string) (string, io.Reader) { if arg[i] == nfs.SIZE { size = kit.Int64(arg[i+1]) } else if arg[i] == SPIDE_CACHE { - if t, e := time.ParseInLocation(ice.MOD_TIME, arg[i+1], time.Local); e == nil { + if t, e := time.ParseInLocation(ice.MOD_TIME, arg[i+1], time.Local); !m.Warn(e, ice.ErrNotValid) { cache = t } } else if strings.HasPrefix(arg[i+1], ice.AT) { - if s, e := nfs.StatFile(m, arg[i+1][1:]); e == nil { + if s, e := nfs.StatFile(m, arg[i+1][1:]); !m.Warn(e, ice.ErrNotValid) { if s.Size() == size && s.ModTime().Before(cache) { continue } @@ -150,7 +155,7 @@ func _spide_part(m *ice.Message, arg ...string) (string, io.Reader) { return mp.FormDataContentType(), buf } func _spide_head(m *ice.Message, req *http.Request, head ice.Maps, value ice.Map) { - m.Logs(req.Method, req.URL) + m.Logs(req.Method, req.URL.String()) kit.Fetch(value[SPIDE_HEADER], func(k string, v string) { req.Header.Set(k, v) m.Logs("Header", k, v) @@ -195,9 +200,8 @@ func _spide_save(m *ice.Message, format, file, uri string, res *http.Response) { case SPIDE_SAVE: _cache_download(m, res, file, m.OptionCB(SPIDE)) case SPIDE_CACHE: - m.Optionv(RESPONSE, res) - m.Cmdy(CACHE, DOWNLOAD, res.Header.Get(ContentType), uri) - m.Echo(m.Append(mdb.DATA)) + m.Cmdy(CACHE, DOWNLOAD, res.Header.Get(ContentType), uri, kit.Dict(RESPONSE, res)) + m.Echo(m.Append(mdb.HASH)) default: var data ice.Any if b, e := ioutil.ReadAll(res.Body); !m.Warn(e) { @@ -213,8 +217,8 @@ func _spide_save(m *ice.Message, format, file, uri string, res *http.Response) { const ( SPIDE_RAW = "raw" SPIDE_MSG = "msg" - SPIDE_SAVE = "save" SPIDE_CACHE = "cache" + SPIDE_SAVE = "save" SPIDE_BODY = "body" SPIDE_FORM = "form" @@ -246,7 +250,7 @@ const ( CLIENT_PROTOCOL = "client.protocol" CLIENT_HOSTNAME = "client.hostname" - + CLIENT_NAME = "client.name" CLIENT_METHOD = "client.method" CLIENT_TIMEOUT = "client.timeout" @@ -283,14 +287,20 @@ func init() { m.Push(DOMAIN, msg.Append(CLIENT_PROTOCOL)+"://"+msg.Append(CLIENT_HOSTNAME)+kit.Select("", arg, 1)) m.Push(tcp.PROTOCOL, msg.Append(CLIENT_PROTOCOL)).Push(tcp.HOSTNAME, msg.Append(CLIENT_HOSTNAME)) }}, - MERGE: {Hand: func(m *ice.Message, arg ...string) { m.Echo(kit.MergeURL2(m.CmdAppend("", arg[0], CLIENT_URL), arg[1], arg[2:])) }}, + MERGE: {Hand: func(m *ice.Message, arg ...string) { + m.Echo(kit.MergeURL2(m.CmdAppend("", arg[0], CLIENT_URL), arg[1], arg[2:])) + }}, }, mdb.HashAction(mdb.SHORT, CLIENT_NAME, mdb.FIELD, "time,client.name,client.url", LOGHEADERS, ice.FALSE), mdb.ClearHashOnExitAction()), Hand: func(m *ice.Message, arg ...string) { if len(arg) < 2 || arg[0] == "" || (len(arg) > 3 && arg[3] == "") { mdb.HashSelect(m, kit.Slice(arg, 0, 1)...).Sort(CLIENT_NAME) } else { - _spide_show(m, arg...) + _spide_show(m, arg[0], arg[1:]...) } }}, + "/spide-demo/": {Actions: aaa.WhiteAction(), Hand: func(m *ice.Message, arg ...string) { + m.Push("hi", "hello") + m.Echo("hello world") + }}, http.MethodGet: {Name: "GET url key value run", Help: "蜘蛛侠", Hand: func(m *ice.Message, arg ...string) { m.Echo(kit.Formats(kit.UnMarshal(m.Cmdx(SPIDE, ice.DEV, SPIDE_RAW, http.MethodGet, arg[0], arg[1:])))) }}, @@ -306,10 +316,18 @@ func init() { }) } -func SpideGet(m *ice.Message, arg ...ice.Any) ice.Any { return kit.UnMarshal(m.Cmdx(http.MethodGet, arg)) } -func SpidePut(m *ice.Message, arg ...ice.Any) ice.Any { return kit.UnMarshal(m.Cmdx(http.MethodPut, arg)) } -func SpidePost(m *ice.Message, arg ...ice.Any) ice.Any { return kit.UnMarshal(m.Cmdx(http.MethodPost, arg)) } -func SpideDelete(m *ice.Message, arg ...ice.Any) ice.Any { return kit.UnMarshal(m.Cmdx(http.MethodDelete, arg)) } +func SpideGet(m *ice.Message, arg ...ice.Any) ice.Any { + return kit.UnMarshal(m.Cmdx(http.MethodGet, arg)) +} +func SpidePut(m *ice.Message, arg ...ice.Any) ice.Any { + return kit.UnMarshal(m.Cmdx(http.MethodPut, arg)) +} +func SpidePost(m *ice.Message, arg ...ice.Any) ice.Any { + return kit.UnMarshal(m.Cmdx(http.MethodPost, arg)) +} +func SpideDelete(m *ice.Message, arg ...ice.Any) ice.Any { + return kit.UnMarshal(m.Cmdx(http.MethodDelete, arg)) +} func SpideSave(m *ice.Message, file, link string, cb func(int, int, int)) *ice.Message { return m.Cmd("web.spide", ice.DEV, SPIDE_SAVE, file, http.MethodGet, link, cb) } diff --git a/base/web/web.go b/base/web/web.go index 8561d9ba..8a90fd2a 100644 --- a/base/web/web.go +++ b/base/web/web.go @@ -96,7 +96,7 @@ const WEB = "web" var Index = &ice.Context{Name: WEB, Help: "网络模块"} func init() { - ice.Index.Register(Index, &Frame{}, BROAD, SERVE, SPACE, DREAM, CACHE, SPIDE, SHARE) + ice.Index.Register(Index, &Frame{}, BROAD, SERVE, SPACE, DREAM, SHARE, CACHE, SPIDE) } func ApiAction(arg ...string) ice.Actions { return ice.Actions{kit.Select(ice.PS, arg, 0): {}} }