From 2ea6bfde9761e6871e81a6f556aa4c93d6c26b1b Mon Sep 17 00:00:00 2001 From: shaoying Date: Mon, 30 Dec 2019 02:53:28 +0800 Subject: [PATCH] add system.init --- base/web/web.go | 86 +++++++++++++++++++++++++++++++++++------------ build.sh | 9 +++-- conf.go | 2 ++ core/chat/chat.go | 20 ++++++----- core/code/code.go | 39 +++++++++++++++++---- misc/git/git.go | 2 +- misc/tmux/tmux.go | 36 +++++++++++++------- miss/go.mod | 10 ++++++ type.go | 2 ++ 9 files changed, 152 insertions(+), 54 deletions(-) create mode 100644 miss/go.mod diff --git a/base/web/web.go b/base/web/web.go index b30c15a2..f6c3a42a 100644 --- a/base/web/web.go +++ b/base/web/web.go @@ -44,6 +44,9 @@ func IsLocalIP(ip string) bool { return ip == "::1" || ip == "127.0.0.1" } func (web *Frame) Login(msg *ice.Message, w http.ResponseWriter, r *http.Request) bool { + if strings.HasPrefix(msg.Option(ice.MSG_USERURL), "/space/") { + return true + } if msg.Options(ice.WEB_SESS) { // 会话认证 sub := msg.Cmd(ice.AAA_SESS, "check", msg.Option(ice.WEB_SESS)) @@ -377,6 +380,14 @@ var Index = &ice.Context{Name: "web", Help: "网页模块", ice.ICE_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { m.Done() m.Done() + p := m.Conf(ice.WEB_CACHE, "meta.store") + m.Richs(ice.WEB_CACHE, nil, "*", func(key string, value map[string]interface{}) { + if f, _, e := kit.Create(path.Join(p, key[:2], key)); e == nil { + defer f.Close() + f.WriteString(kit.Formats(value)) + } + }) + m.Conf(ice.WEB_CACHE, "hash", kit.Dict()) m.Cmd(ice.CTX_CONFIG, "save", "web.json", ice.WEB_SPIDE, ice.WEB_FAVOR, ice.WEB_CACHE, ice.WEB_STORY, ice.WEB_SHARE) }}, @@ -540,8 +551,14 @@ var Index = &ice.Context{Name: "web", Help: "网页模块", // 验证结果 if m.Cost("%s %s: %s", res.Status, res.Header.Get("Content-Length"), res.Header.Get("Content-Type")); m.Warn(e != nil, "%s", e) { + if cache != "" { + m.Set("result") + } return } else if m.Warn(res.StatusCode != http.StatusOK, "%s", res.Status) { + if cache != "" { + m.Set("result") + } return } @@ -572,10 +589,18 @@ var Index = &ice.Context{Name: "web", Help: "网页模块", m.Conf(ice.CLI_RUNTIME, "node.type", ice.WEB_SERVER) // 启动服务 - m.Target().Start(m, kit.Select("self", arg, 0)) - m.Richs(ice.WEB_SPIDE, nil, "dev", func(key string, value map[string]interface{}) { - m.Cmd(ice.WEB_SPACE, "connect", "dev") - }) + switch kit.Select("self", arg, 0) { + case "dev": + m.Event(ice.SYSTEM_INIT) + fallthrough + case "self": + m.Target().Start(m, "self") + fallthrough + default: + m.Richs(ice.WEB_SPIDE, nil, "dev", func(key string, value map[string]interface{}) { + m.Cmd(ice.WEB_SPACE, "connect", "dev") + }) + } }}, ice.WEB_SPACE: {Name: "space", Help: "空间站", Meta: kit.Dict("exports", []string{"pod", "name"}), List: kit.List( kit.MDB_INPUT, "text", "name", "pod", @@ -813,22 +838,15 @@ var Index = &ice.Context{Name: "web", Help: "网页模块", favor := "" if m.Richs(ice.WEB_FAVOR, nil, arg[0], func(key string, value map[string]interface{}) { favor = key - }) == nil { + }) == nil && len(arg) > 1 { favor = m.Rich(ice.WEB_FAVOR, nil, kit.Data(kit.MDB_NAME, arg[0])) - m.Info("create favor: %s name: %s", favor, arg[0]) + m.Log(ice.LOG_CREATE, "favor: %s name: %s", favor, arg[0]) } - extras := []string{} - if len(arg) == 3 && arg[1] == "extra" { - extras, arg = strings.Split(arg[2], " "), arg[:1] - } if len(arg) == 1 { // 收藏列表 m.Grows(ice.WEB_FAVOR, kit.Keys(kit.MDB_HASH, favor), "", "", func(index int, value map[string]interface{}) { m.Push(kit.Format(index), value, []string{kit.MDB_TIME, kit.MDB_ID, kit.MDB_TYPE, kit.MDB_NAME, kit.MDB_TEXT}) - for _, k := range extras { - m.Push(k, kit.Select("", kit.Value(value, "extra."+k))) - } }) return } @@ -984,6 +1002,14 @@ var Index = &ice.Context{Name: "web", Help: "网页模块", // head list data time text file switch arg[0] { case "catch": + if last := m.Richs(ice.WEB_STORY, "head", arg[2], nil); last != nil { + if t, e := time.ParseInLocation(ice.ICE_TIME, kit.Format(last["time"]), time.Local); e == nil { + if s, e := os.Stat(arg[2]); e == nil && s.ModTime().Before(t) { + m.Info("%s last: %s", arg[2], kit.Format(t)) + break + } + } + } fallthrough case "add", "upload": // 保存数据 @@ -999,6 +1025,11 @@ var Index = &ice.Context{Name: "web", Help: "网页模块", m.Log("info", "head: %v prev: %v count: %v", head, prev, count) }) + if last := m.Richs(ice.WEB_STORY, nil, prev, nil); prev != "" && last != nil && last["data"] == arg[3] { + // 重复提交 + break + } + // 添加节点 list := m.Rich(ice.WEB_STORY, nil, kit.Dict( "count", count+1, "scene", arg[1], "story", arg[2], "data", arg[3], "prev", prev, @@ -1064,12 +1095,18 @@ var Index = &ice.Context{Name: "web", Help: "网页模块", for i := 0; i < 10 && list != ""; i++ { m.Confm(ice.WEB_STORY, kit.Keys("hash", list), func(value map[string]interface{}) { // 直连节点 - m.Confm(ice.WEB_CACHE, kit.Keys("hash", value["data"]), func(val map[string]interface{}) { - m.Push(list, value, []string{"key", "time", "count", "scene", "story"}) + val := m.Confm(ice.WEB_CACHE, kit.Keys("hash", value["data"])) + if val == nil { + data := kit.Format(value["data"]) + if f, e := os.Open(path.Join(m.Conf(ice.WEB_CACHE, "meta.store"), data[:2], data)); e != nil || + json.NewDecoder(f).Decode(&val) != nil { + return + } + } + m.Push(list, value, []string{"key", "time", "count", "scene", "story"}) - m.Push("drama", val["text"]) - m.Push("data", value["data"]) - }) + m.Push("drama", val["text"]) + m.Push("data", value["data"]) // 复合节点 kit.Fetch(value["list"], func(key string, val string) { @@ -1102,11 +1139,16 @@ var Index = &ice.Context{Name: "web", Help: "网页模块", } // 查询数据 - if node := m.Confm(ice.WEB_CACHE, kit.Keys("hash", arg[1])); node != nil { - m.Push("data", arg[1]) - m.Push(arg[1], node, []string{"text", "time", "size", "type", "name", "file"}) - m.Echo("%s", node["text"]) + node := m.Confm(ice.WEB_CACHE, kit.Keys("hash", arg[1])) + if node == nil { + if f, e := os.Open(path.Join(m.Conf(ice.WEB_CACHE, "meta.store"), arg[1][:2], arg[1])); e != nil || + json.NewDecoder(f).Decode(&node) != nil { + return + } } + m.Push("data", arg[1]) + m.Push(arg[1], node, []string{"text", "time", "size", "type", "name", "file"}) + m.Echo("%s", node["text"]) default: if len(arg) == 1 { m.Cmd(ice.WEB_STORY, "history", arg).Table(func(index int, value map[string]string, head []string) { diff --git a/build.sh b/build.sh index 0784d8d6..25fd2f8f 100755 --- a/build.sh +++ b/build.sh @@ -47,10 +47,13 @@ prepare() { curl \$ctx_dev/publish/\${bin} -o ice.bin && chmod u+x ice.bin } start() { - prepare && shutdown && while true; do + while true; do date && ice.bin \$@ 2>boot.log && echo -e "\n\nrestarting..." || break done } +serve() { + prepare && shutdown && start \$@ +} restart() { [ -e \$ctx_pid ] && kill -2 \`cat \$ctx_pid\` || echo } @@ -58,7 +61,7 @@ shutdown() { [ -e \$ctx_pid ] && kill -3 \`cat \$ctx_pid\` || echo } -cmd=\$1 && [ -n "\$cmd" ] && shift || cmd=start +cmd=\$1 && [ -n "\$cmd" ] && shift || cmd=serve \$cmd \$* END chmod u+x ice.sh @@ -66,7 +69,7 @@ END build() { miss=./ && [ "$1" != "" ] && miss=$1 && shift && mkdir $miss - cd $miss && prepare && go build -o ice.bin main.go && chmod u+x ice.bin && ./ice.sh start + cd $miss && prepare && go build -o ice.bin main.go && chmod u+x ice.bin && ./ice.sh start serve dev } cmd=build && [ "$1" != "" ] && cmd=$1 && shift diff --git a/conf.go b/conf.go index 8cb0b66b..f1ca6796 100644 --- a/conf.go +++ b/conf.go @@ -97,6 +97,8 @@ const ( // GDB GDB_TIMER = "timer" GDB_EVENT = "event" + SYSTEM_INIT = "system.init" + SERVE_START = "serve.start" SERVE_CLOSE = "serve.close" SPACE_START = "space.start" diff --git a/core/chat/chat.go b/core/chat/chat.go index 165287df..ebcf3fdc 100644 --- a/core/chat/chat.go +++ b/core/chat/chat.go @@ -7,7 +7,7 @@ import ( "github.com/shylinux/toolkits" ) -var Index = &ice.Context{Name: "chat", Help: "聊天模块", +var Index = &ice.Context{Name: "chat", Help: "聊天中心", Caches: map[string]*ice.Cache{}, Configs: map[string]*ice.Config{ ice.CHAT_RIVER: {Name: "river", Help: "群组", Value: kit.Data()}, @@ -16,10 +16,18 @@ var Index = &ice.Context{Name: "chat", Help: "聊天模块", ice.ICE_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { m.Cmd(ice.CTX_CONFIG, "load", "chat.json") - if m.Conf(ice.CLI_RUNTIME, "boot.count") == "1" { + m.Watch(ice.SYSTEM_INIT, "web.chat.init") + m.Watch(ice.USER_CREATE, "web.chat./ocean", "spawn", "") + }}, + ice.ICE_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + m.Cmd(ice.CTX_CONFIG, "save", "chat.json", ice.CHAT_RIVER) + }}, + + "init": {Name: "init", Help: "初始化", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + if len(m.Confm(ice.CHAT_RIVER, "hash")) == 0 { // 系统群组 - m.Option(ice.MSG_USERNAME, m.Conf(ice.CLI_RUNTIME, "boot.username")) m.Option(ice.MSG_USERROLE, ice.ROLE_ROOT) + m.Option(ice.MSG_USERNAME, m.Conf(ice.CLI_RUNTIME, "boot.username")) river := m.Cmdx("web.chat./ocean", "spawn", "meet", m.Conf(ice.CLI_RUNTIME, "boot.username")) river = m.Cmdx("web.chat./steam", river, "spawn", "miss", "", "", "spide", "", @@ -41,13 +49,7 @@ var Index = &ice.Context{Name: "chat", Help: "聊天模块", m.Cmd(ice.AAA_ROLE, "black", ice.ROLE_VOID, "enable", "dream.停止") } - // 用户群组 - m.Watch(ice.USER_CREATE, "web.chat./ocean", "spawn", "") }}, - ice.ICE_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - m.Cmd(ice.CTX_CONFIG, "save", "chat.json", ice.CHAT_RIVER) - }}, - ice.WEB_LOGIN: {Name: "login", Help: "登录", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { if len(arg) > 0 { switch arg[0] { diff --git a/core/code/code.go b/core/code/code.go index a0ff9158..51651504 100644 --- a/core/code/code.go +++ b/core/code/code.go @@ -33,11 +33,22 @@ var Index = &ice.Context{Name: "code", Help: "编程模块", "compile": {Name: "compile", Help: "编译", Value: kit.Data("path", "usr/publish")}, "publish": {Name: "publish", Help: "发布", Value: kit.Data("path", "usr/publish")}, - "upgrade": {Name: "upgrade", Help: "升级", Value: kit.Data("path", "usr/publish")}, + "upgrade": {Name: "upgrade", Help: "升级", Value: kit.Dict( + kit.MDB_HASH, kit.Dict( + "system", kit.Dict( + kit.MDB_LIST, kit.List( + kit.MDB_INPUT, "bin", "file", "ice.sh", "path", "ice.sh", + kit.MDB_INPUT, "bin", "file", "ice.bin", "path", "ice.bin", + ), + ), + ), + )}, }, Commands: map[string]*ice.Command{ ice.ICE_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { m.Cmd(ice.CTX_CONFIG, "load", "code.json") + m.Watch(ice.SYSTEM_INIT, "compile", "linux") + m.Watch(ice.SYSTEM_INIT, "publish", "ice.sh") }}, ice.ICE_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { m.Cmd(ice.CTX_CONFIG, "save", "code.json", "web.code.login") @@ -92,12 +103,28 @@ var Index = &ice.Context{Name: "code", Help: "编程模块", m.Log(ice.LOG_EXPORT, "%s: %s", arg[0], target) }}, "upgrade": {Name: "upgrade", Help: "升级", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - os.Rename("ice.sh", "ice.sh.bak") - os.Link(m.Cmd(ice.WEB_STORY, "index", m.Cmdx(ice.WEB_SPIDE, "dev", "cache", "/publish/ice.sh")).Append("file"), "ice.sh") + exit := true + m.Grows("upgrade", "hash.system", "", "", func(index int, value map[string]interface{}) { + if value["file"] == "ice.bin" { + value["file"] = kit.Keys("ice", m.Conf(ice.CLI_RUNTIME, "host.GOOS"), m.Conf(ice.CLI_RUNTIME, "host.GOARCH")) + } - os.Rename("ice.bin", "ice.bin.bak") - os.Link(m.Cmd(ice.WEB_STORY, "index", m.Cmdx(ice.WEB_SPIDE, "dev", "cache", kit.Keys("/publish/ice", - m.Conf(ice.CLI_RUNTIME, "host.GOOS"), m.Conf(ice.CLI_RUNTIME, "host.GOARCH")))).Append("file"), "ice.bin") + h := m.Cmdx(ice.WEB_SPIDE, "dev", "cache", "/publish/"+kit.Format(value["file"])) + if h == "" { + exit = false + return + } + m.Cmd(ice.WEB_STORY, "add", "bin", value["path"], h) + + os.Rename(kit.Format(value["path"]), kit.Keys(value["path"], "bak")) + os.Link(m.Cmd(ice.WEB_STORY, "index", h).Append("file"), kit.Format(value["path"])) + os.Chmod(kit.Format(value["path"]), 777) + m.Log(ice.LOG_EXPORT, "%s: %s", h, value["path"]) + }) + + if exit { + m.Cmd("exit") + } }}, "login": {Name: "login", Help: "登录", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { diff --git a/misc/git/git.go b/misc/git/git.go index da842a47..407792c0 100644 --- a/misc/git/git.go +++ b/misc/git/git.go @@ -21,7 +21,7 @@ var Index = &ice.Context{Name: "git", Help: "代码管理", "name", "volcanos", "path", "usr/volcanos", "branch", "master", "remote", "https://github.com/shylinux/volcanos", )) - m.Watch(ice.SERVE_START, "cli.git.check", "volcanos") + m.Watch(ice.SYSTEM_INIT, "cli.git.check", "volcanos") }}, ice.ICE_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { }}, diff --git a/misc/tmux/tmux.go b/misc/tmux/tmux.go index 61d541c2..717c48a3 100644 --- a/misc/tmux/tmux.go +++ b/misc/tmux/tmux.go @@ -4,6 +4,7 @@ import ( "github.com/shylinux/icebergs" "github.com/shylinux/icebergs/base/cli" "github.com/shylinux/toolkits" + "os" "path" "strings" "time" @@ -52,21 +53,30 @@ var Index = &ice.Context{Name: "tmux", Help: "终端模块", }, Commands: map[string]*ice.Command{ ice.ICE_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + m.Watch(ice.SYSTEM_INIT, "cli.tmux.init") m.Watch(ice.DREAM_START, "cli.tmux.auto") - return - m.Watch(ice.SERVE_START, "cli.tmux.auto") - for _, p := range []string{"auto.sh", "auto.vim"} { - if m.Richs(ice.WEB_STORY, "head", p, nil) == nil { - m.Cmd(ice.WEB_STORY, "add", ice.TYPE_SHELL, p, m.Cmdx(ice.WEB_SPIDE, "shy", "cache", "GET", "/publish/"+p)) - } - } - if m.Richs(ice.WEB_FAVOR, nil, ice.FAVOR_TMUX, nil) == nil { - m.Cmd(ice.WEB_FAVOR, ice.FAVOR_TMUX, ice.TYPE_SHELL, "下载脚本", `curl -s "$ctx_dev/code/zsh?cmd=download&arg=auto.sh" > auto.sh`) - m.Cmd(ice.WEB_FAVOR, ice.FAVOR_TMUX, ice.TYPE_SHELL, "加载脚本", `source auto.sh`) - } }}, ice.ICE_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { }}, + "init": {Name: "init", Help: "初始化", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + for _, v := range []string{"auto.sh", "auto.vim"} { + p := path.Join(m.Conf("web.code.publish", "meta.path"), v) + if _, e := os.Stat(p); e != nil && os.IsNotExist(e) { + if h := m.Cmdx(ice.WEB_SPIDE, "shy", "cache", "GET", "/publish/"+v); h != "" { + os.Link(m.Cmd(ice.WEB_STORY, "index", h).Append("file"), p) + m.Log(ice.LOG_EXPORT, "%s: %s", h, p) + } + } + } + + if m.Richs(ice.WEB_FAVOR, nil, "tmux.auto", nil) == nil { + m.Cmd(ice.WEB_FAVOR, "tmux.auto", ice.TYPE_SHELL, "下载脚本", `curl -s "$ctx_dev/publish/auto.sh" -o auto.sh`) + m.Cmd(ice.WEB_FAVOR, "tmux.auto", ice.TYPE_SHELL, "加载脚本", `source auto.sh`) + } + if m.Richs(ice.WEB_FAVOR, nil, "tmux.init", nil) == nil { + m.Cmd(ice.WEB_FAVOR, "tmux.init", ice.TYPE_SHELL, "一键启动", `curl -s "$ctx_dev/publish/ice.sh" |sh`) + } + }}, "buffer": {Name: "buffer", Help: "终端", List: kit.List( @@ -233,12 +243,12 @@ var Index = &ice.Context{Name: "tmux", Help: "终端模块", m.Cmd(prefix, "new-session", "-ds", arg[0]) } + m.Cmdy(prefix, "send-keys", "-t", arg[0], "export ctx_dev=", kit.Select(m.Conf(ice.CLI_RUNTIME, "conf.ctx_dev"), m.Conf(ice.CLI_RUNTIME, "host.ctx_self")), "Enter") m.Richs(ice.WEB_SPACE, nil, arg[0], func(key string, value map[string]interface{}) { - m.Cmdy(prefix, "send-keys", "-t", arg[0], "export ctx_dev=", kit.Select(m.Conf(ice.CLI_RUNTIME, "host.ctx_dev"), m.Conf(ice.CLI_RUNTIME, "host.ctx_self")), "Enter") m.Cmdy(prefix, "send-keys", "-t", arg[0], "export ctx_share=", value["share"], "Enter") }) - m.Cmd(ice.WEB_FAVOR, kit.Select("tmux.init", arg, 1)).Table(func(index int, value map[string]string, head []string) { + m.Cmd(ice.WEB_FAVOR, kit.Select("tmux.auto", arg, 1)).Table(func(index int, value map[string]string, head []string) { switch value["type"] { case "shell": m.Cmdy(prefix, "send-keys", "-t", arg[0], value["text"], "Enter") diff --git a/miss/go.mod b/miss/go.mod new file mode 100644 index 00000000..f0b362e2 --- /dev/null +++ b/miss/go.mod @@ -0,0 +1,10 @@ +module miss + +go 1.13 + +require github.com/shylinux/icebergs v0.1.0 // indirect + +replace ( + github.com/shylinux/icebergs => ../ + github.com/shylinux/toolkits => ../../toolkits +) diff --git a/type.go b/type.go index fa2c4bb8..49cbf686 100644 --- a/type.go +++ b/type.go @@ -279,6 +279,8 @@ func (m *Message) Spawn(arg ...interface{}) *Message { source: m.target, target: m.target, + R: m.R, + W: m.W, } if len(arg) > 0 {