1
0
forked from x/icebergs

opt m.Travel

This commit is contained in:
shaoying 2019-12-19 02:09:18 +08:00
parent 7a2f552db5
commit e1ff0cd66d
12 changed files with 150 additions and 82 deletions

View File

@ -53,6 +53,7 @@ var Index = &ice.Context{Name: "cli", Help: "命令模块",
cmd := exec.Command(arg[0], arg[1:]...)
cmd.Dir = m.Option("cmd_dir")
m.Log("info", "dir: %s", cmd.Dir)
if m.Option("cmd_type") == "daemon" {
m.Gos(m, func(m *ice.Message) {
if e := cmd.Start(); e != nil {

View File

@ -59,10 +59,14 @@ var Index = &ice.Context{Name: "ctx", Help: "元始模块",
m.Push("help", i.Help)
m.Push("meta", kit.Format(i.Meta))
m.Push("list", kit.Format(i.List))
} else {
if i.Meta != nil && kit.Format(i.Meta["remote"]) == "true" && m.Option("you") != "" {
m.Copy(m.Spawns(s).Cmd("web.space", m.Option("you"), "ctx.command", arg[0], arg[1], "run", arg[3:]))
} else {
m.Copy(m.Spawns(s).Runs(key, key, arg[3:]...))
}
}
}
})
}},
"config": {Name: "config", Help: "配置", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {

View File

@ -17,20 +17,10 @@ func (f *Frame) Spawn(m *ice.Message, c *ice.Context, arg ...string) ice.Server
return &Frame{}
}
func (f *Frame) Begin(m *ice.Message, arg ...string) ice.Server {
if f, p, e := kit.Create(m.Conf("logpid")); m.Assert(e) {
defer f.Close()
f.WriteString(kit.Format(os.Getpid()))
m.Log("info", "pid %d %s", os.Getpid(), p)
}
f.p = make(chan os.Signal, 10)
return f
}
func (f *Frame) Start(m *ice.Message, arg ...string) bool {
f.p = make(chan os.Signal, 10)
m.Confm("signal", nil, func(sig string, action string) {
m.Log("signal", "add %s: %s", sig, action)
signal.Notify(f.p, syscall.Signal(kit.Int(sig)))
})
m.Cap("stream", m.Conf("timer", "meta.tick"))
tick := time.Tick(kit.Duration(m.Conf("timer", "meta.tick")))
for {
@ -42,7 +32,6 @@ func (f *Frame) Start(m *ice.Message, arg ...string) bool {
m.Log("info", "signal %v", sig)
m.Cmd(m.Confv("signal", kit.Format(sig)))
case now, _ := <-tick:
// m.Log("info", "ticker %v", kit.Format(now))
stamp := int(now.Unix())
m.Confm("timer", "hash", func(key string, value map[string]interface{}) {
if kit.Int(value["next"]) <= stamp {
@ -81,7 +70,17 @@ var Index = &ice.Context{Name: "gdb", Help: "调试模块",
},
Commands: map[string]*ice.Command{
"_init": {Name: "_init", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Echo("hello %s world", c.Name)
if f, p, e := kit.Create(m.Conf("logpid")); m.Assert(e) {
defer f.Close()
f.WriteString(kit.Format(os.Getpid()))
m.Log("info", "pid %d %s", os.Getpid(), p)
}
f := m.Target().Server().(*Frame)
m.Confm("signal", nil, func(sig string, action string) {
m.Log("signal", "add %s: %s", sig, action)
signal.Notify(f.p, syscall.Signal(kit.Int(sig)))
})
}},
"_exit": {Name: "_exit", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
f := m.Target().Server().(*Frame)

View File

@ -29,14 +29,6 @@ func (f *Frame) Begin(m *ice.Message, arg ...string) ice.Server {
return f
}
func (f *Frame) Start(m *ice.Message, arg ...string) bool {
m.Confm("file", nil, func(key string, value map[string]interface{}) {
if f, p, e := kit.Create(kit.Format(value["path"])); m.Assert(e) {
m.Cap("stream", path.Base(p))
m.Log("info", "log %s %s", key, p)
value["file"] = f
}
})
for {
if l, ok := <-f.p; !ok {
break
@ -80,7 +72,13 @@ var Index = &ice.Context{Name: "log", Help: "日志模块",
},
Commands: map[string]*ice.Command{
"_init": {Name: "_init", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Echo("hello %s world", c.Name)
m.Confm("file", nil, func(key string, value map[string]interface{}) {
if f, p, e := kit.Create(kit.Format(value["path"])); m.Assert(e) {
m.Cap("stream", path.Base(p))
m.Log("info", "log %s %s", key, p)
value["file"] = f
}
})
}},
"_exit": {Name: "_exit", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
f := m.Target().Server().(*Frame)

View File

@ -15,6 +15,7 @@ var Index = &ice.Context{Name: "mdb", Help: "数据模块",
index := kit.Int(arg[2]) - kit.Int(meta["offset"]) - 1
data := m.Confm(arg[0], arg[1]+".list."+kit.Format(index))
m.Log("what", "%v %v", arg[0], arg[1]+".list."+kit.Format(index))
for i := 3; i < len(arg)-1; i += 2 {
kit.Value(data, arg[i], arg[i+1])
}

View File

@ -18,7 +18,7 @@ import (
func dir(m *ice.Message, root string, name string, level int, deep bool, dir_type string, dir_reg *regexp.Regexp, fields []string, format string) {
if fs, e := ioutil.ReadDir(name); m.Assert(e) {
if fs, e := ioutil.ReadDir(path.Join(root, name)); m.Assert(e) {
for _, f := range fs {
if f.Name() == "." || f.Name() == ".." {
continue
@ -27,7 +27,7 @@ func dir(m *ice.Message, root string, name string, level int, deep bool, dir_typ
continue
}
p := path.Join(name, f.Name())
p := path.Join(root, name, f.Name())
if f, e = os.Lstat(p); e != nil {
m.Log("info", "%s", e)
continue

View File

@ -66,7 +66,7 @@ var Index = &ice.Context{Name: "ssh", Help: "终端模块",
"_exit": {Name: "_exit", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
f := m.Target().Server().(*Frame)
f.in.Close()
m.Target().Done()
m.Done()
}},
},
}

View File

@ -47,7 +47,7 @@ func (web *WEB) Login(msg *ice.Message, w http.ResponseWriter, r *http.Request)
msg.Runs("_login", msg.Option("url"), kit.Simple(msg.Optionv("cmds"))...)
return true
}
func (web *WEB) HandleWSS(m *ice.Message, safe bool, c *websocket.Conn) {
func (web *WEB) HandleWSS(m *ice.Message, safe bool, c *websocket.Conn) bool {
for {
if t, b, e := c.ReadMessage(); e != nil {
m.Log("warn", "space recv %d msg %v", t, e)
@ -86,6 +86,9 @@ func (web *WEB) HandleWSS(m *ice.Message, safe bool, c *websocket.Conn) {
// 本地执行
if safe {
msg = msg.Cmd()
if msg.Detail() == "exit" {
return true
}
} else {
msg.Echo("no right")
}
@ -102,6 +105,7 @@ func (web *WEB) HandleWSS(m *ice.Message, safe bool, c *websocket.Conn) {
}
}
}
return false
}
func (web *WEB) HandleCGI(m *ice.Message, which string) *template.Template {
cgi := template.FuncMap{
@ -131,8 +135,8 @@ func (web *WEB) HandleCGI(m *ice.Message, which string) *template.Template {
}(k, v)
}
tmpl = tmpl.Funcs(cgi)
tmpl = template.Must(tmpl.ParseGlob(path.Join(m.Conf("serve", "template.path"), "/*.tmpl")))
tmpl = template.Must(tmpl.ParseGlob(path.Join(m.Conf("serve", "template.path"), m.Target().Name, "/*.tmpl")))
// tmpl = template.Must(tmpl.ParseGlob(path.Join(m.Conf("serve", "template.path"), "/*.tmpl")))
// tmpl = template.Must(tmpl.ParseGlob(path.Join(m.Conf("serve", "template.path"), m.Target().Name, "/*.tmpl")))
tmpl = template.Must(tmpl.ParseFiles(which))
m.Confm("serve", "template.list", func(index int, value string) { tmpl = template.Must(tmpl.Parse(value)) })
for i, v := range tmpl.Templates() {
@ -248,7 +252,7 @@ func (web *WEB) Start(m *ice.Message, arg ...string) bool {
// 级联路由
route := "/" + s.Name + "/"
if n, ok := p.Server().(*WEB); ok && n.ServeMux != nil {
msg.Log("route", "%s <- %s", p.Name, route)
msg.Log("route", "%s <= %s", p.Name, route)
n.Handle(route, http.StripPrefix(path.Dir(route), w))
}
@ -259,12 +263,12 @@ func (web *WEB) Start(m *ice.Message, arg ...string) bool {
})
// 命令路由
for k, x := range s.Commands {
if k[0] == '/' {
m.Travel(func(p *ice.Context, sub *ice.Context, k string, x *ice.Command) {
if s == sub && k[0] == '/' {
msg.Log("route", "%s <- %s", s.Name, k)
w.HandleCmd(msg, k, x)
}
}
})
}
})
@ -285,7 +289,7 @@ var Index = &ice.Context{Name: "web", Help: "网页模块",
Caches: map[string]*ice.Cache{},
Configs: map[string]*ice.Config{
"spide": {Name: "客户端", Value: map[string]interface{}{
"self": map[string]interface{}{"port": ":9020"},
"self": map[string]interface{}{"port": "127.0.0.1:9020"},
}},
"serve": {Name: "服务端", Value: map[string]interface{}{
"static": map[string]interface{}{"/": "usr/volcanos/",
@ -313,7 +317,7 @@ var Index = &ice.Context{Name: "web", Help: "网页模块",
m.Echo("hello %s world", c.Name)
}},
"_exit": {Name: "_exit", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Target().Done()
m.Done()
}},
"serve": {Name: "hi", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Conf("cli.runtime", "node.type", "server")
@ -350,6 +354,7 @@ var Index = &ice.Context{Name: "web", Help: "网页模块",
})
return
}
web := m.Target().Server().(*WEB)
switch arg[0] {
case "connect":
@ -369,11 +374,17 @@ var Index = &ice.Context{Name: "web", Help: "网页模块",
web.send[id] = m
s.WriteMessage(MSG_MAPS, []byte(m.Format("meta")))
web.HandleWSS(m, true, s)
if web.HandleWSS(m, true, s) {
break
}
} else {
m.Log("warn", "wss %s", e)
}
} else {
m.Log("warn", "dial %s", e)
}
time.Sleep(time.Duration(rand.Intn(m.Confi("web.space", "meta.redial"))) * time.Millisecond)
m.Log("info", "reconnect %v", host)
m.Log("info", "reconnect %v", u)
}
})
}
@ -382,6 +393,7 @@ var Index = &ice.Context{Name: "web", Help: "网页模块",
m.Cmdy(arg[1:])
break
}
target := strings.Split(arg[0], ".")
if socket, ok := m.Confv("space", "hash."+target[0]+".socket").(*websocket.Conn); !ok {
m.Echo("error").Echo("not found")
@ -389,13 +401,16 @@ var Index = &ice.Context{Name: "web", Help: "网页模块",
id := kit.Format(c.ID())
m.Optionv("_source", []string{id, target[0]})
m.Optionv("_target", target[1:])
m.Set("detail", arg[1:]...)
web := m.Target().Server().(*WEB)
web.send[id] = m
m.Set("detail", arg[1:]...)
now := time.Now()
socket.WriteMessage(MSG_MAPS, []byte(m.Format("meta")))
m.Call(true, func(msg *ice.Message) *ice.Message {
m.Copy(msg)
m.Log("info", "cost %s", time.Now().Sub(now))
return nil
})
}

View File

@ -29,7 +29,7 @@ var Index = &ice.Context{Name: "team", Help: "团队模块",
}, List: []interface{}{
map[string]interface{}{"type": "text", "value": "", "name": "name"},
map[string]interface{}{"type": "text", "value": "", "name": "type"},
map[string]interface{}{"type": "button", "value": "创建"},
map[string]interface{}{"type": "button", "value": "创建", "action": "auto"},
}, Hand: func(m *ice.Message, c *ice.Context, key string, arg ...string) {
if len(arg) > 1 {
switch arg[1] {
@ -57,12 +57,16 @@ var Index = &ice.Context{Name: "team", Help: "团队模块",
}
}
m.Cmdy("nfs.dir", "", m.Conf("miss", "meta.path"), "time name")
m.Cmdy("nfs.dir", m.Conf("miss", "meta.path"), "", "time name")
m.Table(func(index int, value map[string]string, head []string) {
m.Push("status", kit.Select("stop", "start", m.Confs("web.space", "hash."+value["name"])))
})
}},
"task": {Name: "task", Help: "任务", List: []interface{}{
"task": {Name: "task", Help: "任务",
Meta: map[string]interface{}{
"remote": "true",
},
List: []interface{}{
map[string]interface{}{"type": "select", "value": "create", "values": "create action cancel finish"},
map[string]interface{}{"type": "text", "value": "", "name": "name"},
map[string]interface{}{"type": "text", "value": "", "name": "text"},
@ -84,19 +88,24 @@ var Index = &ice.Context{Name: "team", Help: "团队模块",
}
}},
"process": {Name: "process", Help: "任务进度", Meta: map[string]interface{}{
"detail": []string{"准备", "开始", "取消", "完成"},
"remote": "true",
"detail": []string{"编辑", "准备", "开始", "取消", "完成"},
}, List: []interface{}{
map[string]interface{}{"type": "text", "value": "0", "name": "offend"},
map[string]interface{}{"type": "text", "value": "10", "name": "limit"},
map[string]interface{}{"type": "text", "value": "", "name": "match"},
map[string]interface{}{"type": "text", "value": "", "name": "value"},
map[string]interface{}{"type": "button", "value": "查看"},
map[string]interface{}{"type": "button", "value": "查看", "action": "auto"},
}, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
msg := m.Cmd("mdb.select", "web.chat.group", "hash."+m.Option("sess.river")+".task", arg[0])
switch arg[1] {
case "modify":
prefix := []string{"mdb.update", "web.chat.group", "hash." + m.Option("sess.river") + ".task", arg[0], arg[2], arg[3]}
m.Cmd(prefix)
arg = arg[4:]
case "准备", "开始", "取消", "完成":
msg.Log("what", "%v %v", msg.Append("status"), arg[1])
msg := m.Cmd("mdb.select", "web.chat.group", "hash."+m.Option("sess.river")+".task", arg[0])
if msg.Append("status") == arg[1] {
arg = arg[4:]
break

View File

@ -13,7 +13,15 @@ import (
var Index = &ice.Context{Name: "wiki", Help: "文档模块",
Caches: map[string]*ice.Cache{},
Configs: map[string]*ice.Config{},
Configs: map[string]*ice.Config{
"note": {Name: "note", Value: map[string]interface{}{
"meta": map[string]interface{}{
"path": "usr/local/wiki",
},
"list": map[string]interface{}{},
"hash": map[string]interface{}{},
}},
},
Commands: map[string]*ice.Command{
"chart": {Name: "chart", Help: "绘图", List: []interface{}{
map[string]interface{}{"type": "select", "value": "chain", "values": "chain table"},
@ -43,10 +51,10 @@ var Index = &ice.Context{Name: "wiki", Help: "文档模块",
}},
"_tree": {Name: "_tree", Help: "目录", Hand: func(m *ice.Message, c *ice.Context, key string, arg ...string) {
m.Cmdy("nfs.dir", "", arg[0])
m.Cmdy("nfs.dir", m.Conf("note", "meta.path"), kit.Select("", arg, 0), "time size line path")
}},
"_text": {Name: "_text", Help: "文章", Hand: func(m *ice.Message, c *ice.Context, key string, arg ...string) {
tmpl := m.Target().Server().(*web.WEB).HandleCGI(m, arg[0])
tmpl := m.Target().Server().(*web.WEB).HandleCGI(m, path.Join(m.Conf("note", "meta.path"), arg[0]))
m.Optionv("title", map[string]int{})
buffer := bytes.NewBuffer([]byte{})
@ -64,9 +72,11 @@ var Index = &ice.Context{Name: "wiki", Help: "文档模块",
}},
"note": {Name: "note file|favor|commit", Help: "笔记", Meta: map[string]interface{}{
"display": "inner",
"remote": "true",
}, List: []interface{}{
map[string]interface{}{"type": "text", "value": "miss.md"},
map[string]interface{}{"type": "button", "value": "执行"},
map[string]interface{}{"type": "text", "value": "miss.md", "name": "path"},
map[string]interface{}{"type": "button", "value": "执行", "action": "auto"},
map[string]interface{}{"type": "button", "value": "返回", "cb": "Last"},
}, Hand: func(m *ice.Message, c *ice.Context, key string, arg ...string) {
if len(arg) == 0 {
m.Cmdy("_tree")

51
type.go
View File

@ -85,14 +85,6 @@ func (c *Context) Register(s *Context, x Server) *Context {
return s
}
func (c *Context) Done() bool {
if c.context != nil && c.context.wg != nil {
c.context.wg.Done()
} else {
c.root.wg.Done()
}
return true
}
func (c *Context) Begin(m *Message, arg ...string) *Context {
c.Caches["status"] = &Cache{Name: "status", Value: ""}
c.Caches["stream"] = &Cache{Name: "stream", Value: ""}
@ -113,14 +105,17 @@ func (c *Context) Start(m *Message, arg ...string) bool {
c.root.wg.Add(1)
}
wait := make(chan bool)
m.Gos(m, func(m *Message) {
m.Log("start", "%s", c.Name)
c.Cap("status", "start")
wait <- true
c.server.Start(m, arg...)
c.Cap("status", "close")
c.Done()
m.Done()
})
<-wait
return true
}
func (c *Context) Close(m *Message, arg ...string) bool {
@ -485,6 +480,12 @@ func (m *Message) Resultv(arg ...interface{}) []string {
func (m *Message) Result(arg ...interface{}) string {
return strings.Join(m.Resultv(), "")
}
func (m *Message) Detailv(arg ...interface{}) []string {
return m.meta["detail"]
}
func (m *Message) Detail(arg ...interface{}) string {
return kit.Select("", m.meta["detail"], 0)
}
var Log func(*Message, string, string)
@ -541,10 +542,31 @@ func (m *Message) TryCatch(msg *Message, safe bool, hand ...func(msg *Message))
}
return m
}
func (m *Message) Travel(cb func(p *Context, s *Context)) *Message {
func (m *Message) Travel(cb interface{}) *Message {
list := []*Context{m.target}
for i := 0; i < len(list); i++ {
switch cb := cb.(type) {
case func(*Context, *Context):
cb(list[i].context, list[i])
case func(*Context, *Context, string, *Command):
ls := []string{}
for k := range list[i].Commands {
ls = append(ls, k)
}
sort.Strings(ls)
for _, k := range ls {
cb(list[i].context, list[i], k, list[i].Commands[k])
}
case func(*Context, *Context, string, *Config):
ls := []string{}
for k := range list[i].Configs {
ls = append(ls, k)
}
sort.Strings(ls)
for _, k := range ls {
cb(list[i].context, list[i], k, list[i].Configs[k])
}
}
ls := []string{}
for k := range list[i].contexts {
@ -601,6 +623,15 @@ func (m *Message) Runs(key string, cmd string, arg ...string) *Message {
}
return m
}
func (m *Message) Done() bool {
defer func() { recover() }()
if m.target.context != nil && m.target.context.wg != nil {
m.target.context.wg.Done()
} else {
m.target.root.wg.Done()
}
return true
}
func (m *Message) Start(key string, arg ...string) *Message {
m.Travel(func(p *Context, s *Context) {
if s.Name == key {