1
0
forked from x/icebergs

add render cb

This commit is contained in:
shylinux 2020-04-19 00:55:00 +08:00
parent 2b0d1f08d7
commit 44b6ef179c
9 changed files with 151 additions and 98 deletions

View File

@ -174,7 +174,7 @@ var Index = &ice.Context{Name: "aaa", Help: "认证模块",
case "auth":
m.Richs(ice.AAA_SESS, nil, arg[1], func(value map[string]interface{}) {
value["username"], value["userrole"] = arg[2], m.Cmdx(ice.AAA_ROLE, "check", arg[2])
m.Log(ice.LOG_LOGIN, "sessid: %s username: %s userrole: %s", arg[1], arg[2], value["userrole"])
m.Log(ice.LOG_AUTH, "sessid: %s username: %s userrole: %s", arg[1], arg[2], value["userrole"])
m.Echo("%v", value["userrole"])
})

View File

@ -172,6 +172,12 @@ var Index = &ice.Context{Name: "nfs", Help: "存储模块",
Commands: map[string]*ice.Command{
ice.ICE_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Cmd(ice.APP_SEARCH, "add", "dir", "base", m.AddCmd(&ice.Command{Name: "search word", Help: "搜索引擎", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
switch arg[0] {
case "set":
m.Cmdy("nfs.cat", arg[5])
return
}
travel(m, "./", "", func(name string) {
if strings.Contains(name, arg[0]) {
m.Push("pod", m.Option(ice.MSG_USERPOD))

View File

@ -108,7 +108,7 @@ func (web *Frame) Login(msg *ice.Message, w http.ResponseWriter, r *http.Request
if msg.Options(ice.MSG_SESSID) {
// 会话认证
sub := msg.Cmd(ice.AAA_SESS, "check", msg.Option(ice.MSG_SESSID))
msg.Log(ice.LOG_LOGIN, "role: %s user: %s", msg.Option(ice.MSG_USERROLE, sub.Append("userrole")),
msg.Log(ice.LOG_AUTH, "role: %s user: %s", msg.Option(ice.MSG_USERROLE, sub.Append("userrole")),
msg.Option(ice.MSG_USERNAME, sub.Append("username")))
}
@ -120,7 +120,7 @@ func (web *Frame) Login(msg *ice.Message, w http.ResponseWriter, r *http.Request
msg.Option(ice.MSG_SESSID, msg.Cmdx(ice.AAA_SESS, "create", msg.Option(ice.MSG_USERNAME), msg.Option(ice.MSG_USERROLE)))
msg.Render("cookie", msg.Option(ice.MSG_SESSID))
}
msg.Log(ice.LOG_LOGIN, "user: %s role: %s sess: %s", msg.Option(ice.MSG_USERNAME), msg.Option(ice.MSG_USERROLE), msg.Option(ice.MSG_SESSID))
msg.Log(ice.LOG_AUTH, "user: %s role: %s sess: %s", msg.Option(ice.MSG_USERNAME), msg.Option(ice.MSG_USERROLE), msg.Option(ice.MSG_SESSID))
}
if s, ok := msg.Target().Commands[ice.WEB_LOGIN]; ok {
@ -251,8 +251,9 @@ func (web *Frame) HandleCmd(m *ice.Message, key string, cmd *ice.Command) {
defer func() { msg.Cost("%s %v %v", r.URL.Path, msg.Optionv("cmds"), msg.Format("append")) }()
// 请求地址
msg.Option(ice.MSG_USERUA, r.Header.Get("User-Agent"))
msg.Option(ice.MSG_USERWEB, m.Conf(ice.WEB_SHARE, "meta.domain"))
msg.Option(ice.MSG_USERIP, r.Header.Get(ice.MSG_USERIP))
msg.Option(ice.MSG_USERUA, r.Header.Get("User-Agent"))
msg.Option(ice.MSG_USERURL, r.URL.Path)
msg.Option(ice.MSG_USERPOD, "")
msg.Option(ice.MSG_SESSID, "")
@ -517,6 +518,21 @@ var Index = &ice.Context{Name: "web", Help: "网络模块",
m.Conf(ice.WEB_SHARE, "meta.template", share_template)
m.Cmd(ice.APP_SEARCH, "add", "favor", "base", m.AddCmd(&ice.Command{Name: "search word", Help: "搜索引擎", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
switch arg[0] {
case "set":
m.Richs(ice.WEB_FAVOR, nil, arg[1], func(key string, value map[string]interface{}) {
m.Grows(ice.WEB_FAVOR, kit.Keys(kit.MDB_HASH, key), "id", arg[2], func(index int, value map[string]interface{}) {
if cmd := m.Conf(ice.WEB_FAVOR, kit.Keys("meta.render", value["type"])); cmd != "" {
m.Optionv("value", value)
m.Cmdy(cmd, arg[1:])
} else {
m.Push("detail", value)
}
})
})
return
}
m.Option("cache.limit", -2)
wg := &sync.WaitGroup{}
m.Richs(ice.WEB_FAVOR, nil, "*", func(key string, val map[string]interface{}) {
@ -540,6 +556,12 @@ var Index = &ice.Context{Name: "web", Help: "网络模块",
}}))
m.Cmd(ice.APP_SEARCH, "add", "story", "base", m.AddCmd(&ice.Command{Name: "search word", Help: "搜索引擎", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
switch arg[0] {
case "set":
m.Cmdy(ice.WEB_STORY, "index", arg[2])
return
}
m.Richs(ice.WEB_STORY, "head", "*", func(key string, val map[string]interface{}) {
if val["story"] == arg[0] {
m.Push("pod", m.Option(ice.MSG_USERPOD))
@ -555,6 +577,12 @@ var Index = &ice.Context{Name: "web", Help: "网络模块",
}}))
m.Cmd(ice.APP_SEARCH, "add", "share", "base", m.AddCmd(&ice.Command{Name: "search word", Help: "搜索引擎", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
switch arg[0] {
case "set":
m.Cmdy(ice.WEB_SHARE, arg[2])
return
}
m.Option("cache.limit", -2)
m.Grows(ice.WEB_SHARE, nil, "", "", func(index int, value map[string]interface{}) {
if value["share"] == arg[0] || value["type"] == arg[0] ||
@ -1026,18 +1054,6 @@ var Index = &ice.Context{Name: "web", Help: "网络模块",
ice.WEB_FAVOR: {Name: "favor favor=auto id=auto auto", Help: "收藏夹", Meta: kit.Dict(
"exports", []string{"hot", "favor"}, "detail", []string{"编辑", "收藏", "收录", "导出", "删除"},
), Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
switch m.Option("_action") {
case "渲染":
m.Option("render", "spide")
m.Richs(ice.WEB_FAVOR, nil, kit.Select(m.Option("favor"), arg, 0), func(key string, value map[string]interface{}) {
m.Option("render", kit.Select("spide", kit.Value(value, "meta.render")))
})
defer m.Render(m.Conf(ice.WEB_FAVOR, kit.Keys("meta.template", m.Option("render"))))
case "回放":
return
}
if len(arg) > 1 && arg[0] == "action" {
favor, id := m.Option("favor"), m.Option("id")
switch arg[2] {

View File

@ -33,8 +33,8 @@ const ( // MSG
MSG_USERIP = "user.ip"
MSG_USERUA = "user.ua"
MSG_USERURL = "user.url"
MSG_USERPOD = "user.pod"
MSG_USERWEB = "user.web"
MSG_USERPOD = "user.pod"
MSG_USERNICK = "user.nick"
MSG_USERNAME = "user.name"
MSG_USERROLE = "user.role"
@ -115,8 +115,7 @@ const ( // LOG
LOG_CLOSE = "close"
// 权限
LOG_LOGIN = "login"
LOG_LOGOUT = "logout"
LOG_AUTH = "auth"
// 分类
LOG_CMDS = "cmds"

View File

@ -176,7 +176,7 @@ var Index = &ice.Context{Name: "chat", Help: "聊天中心",
m.Option(ice.MSG_STORM, arg[1])
})
}
m.Log(ice.LOG_LOGIN, "river: %s storm: %s", m.Option(ice.MSG_RIVER), m.Option(ice.MSG_STORM))
m.Log(ice.LOG_AUTH, "river: %s storm: %s", m.Option(ice.MSG_RIVER), m.Option(ice.MSG_STORM))
})
})
}
@ -200,41 +200,6 @@ var Index = &ice.Context{Name: "chat", Help: "聊天中心",
}
}},
"search": {Name: "search label kind word", Help: "搜索引擎", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
switch arg[0] {
case "add":
if m.Richs("search", nil, arg[1], nil) == nil {
m.Rich("search", nil, kit.Data(kit.MDB_NAME, arg[1]))
}
m.Richs("search", nil, arg[1], func(key string, value map[string]interface{}) {
m.Grow("search", kit.Keys(kit.MDB_HASH, key), kit.Dict(
kit.MDB_NAME, arg[2], kit.MDB_TEXT, arg[3:],
))
})
case "get":
wg := &sync.WaitGroup{}
m.Richs("search", nil, arg[1], func(key string, value map[string]interface{}) {
wg.Add(1)
m.Gos(m, func(m *ice.Message) {
m.Grows("search", kit.Keys(kit.MDB_HASH, key), "", "", func(index int, value map[string]interface{}) {
m.Cmdy(value[kit.MDB_TEXT], arg[2:])
})
wg.Done()
})
})
wg.Wait()
default:
m.Cmdy(ice.WEB_LABEL, arg[0], arg[1], "web.chat.search", "get", arg[2:])
}
}},
"commend": {Name: "commend label=some word=请求响应 auto", Help: "推荐引擎", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
if len(arg) < 2 {
m.Cmdy(ice.WEB_LABEL, arg)
return
}
m.Cmdy(ice.WEB_LABEL, arg[0], "*", "favor", "search", arg[1:])
}},
"/toast": {Name: "/toast", Help: "提示", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {}},
"/carte": {Name: "/carte", Help: "菜单", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {}},
"/tutor": {Name: "/tutor", Help: "向导", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {}},
@ -578,6 +543,52 @@ var Index = &ice.Context{Name: "chat", Help: "聊天中心",
// 执行命令
m.Cmdy(proxy, cmds).Option("cmds", cmds)
}},
"search": {Name: "search label pod engine word", Help: "搜索引擎", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
switch arg[0] {
case "add":
if m.Richs("search", nil, arg[1], nil) == nil {
m.Rich("search", nil, kit.Data(kit.MDB_NAME, arg[1]))
}
m.Richs("search", nil, arg[1], func(key string, value map[string]interface{}) {
m.Grow("search", kit.Keys(kit.MDB_HASH, key), kit.Dict(
kit.MDB_NAME, arg[2], kit.MDB_TEXT, arg[3:],
))
})
case "get":
wg := &sync.WaitGroup{}
m.Richs("search", nil, arg[1], func(key string, value map[string]interface{}) {
wg.Add(1)
m.Gos(m, func(m *ice.Message) {
m.Grows("search", kit.Keys(kit.MDB_HASH, key), "", "", func(index int, value map[string]interface{}) {
m.Cmdy(value[kit.MDB_TEXT], arg[2:])
})
wg.Done()
})
})
wg.Wait()
case "set":
if arg[1] != "" {
// m.Cmdy(ice.WEB_SPACE, arg[1], "web.chat.search", "set", "", arg[2:])
// break
}
m.Richs("search", nil, arg[2], func(key string, value map[string]interface{}) {
m.Grows("search", kit.Keys(kit.MDB_HASH, key), "", "", func(index int, value map[string]interface{}) {
m.Cmdy(value[kit.MDB_TEXT], "set", arg[3:])
})
})
default:
m.Cmdy(ice.WEB_LABEL, arg[0], arg[1], "web.chat.search", "get", arg[2:])
}
}},
"commend": {Name: "commend label=some word=请求响应 auto", Help: "推荐引擎", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
if len(arg) < 2 {
m.Cmdy(ice.WEB_LABEL, arg)
return
}
m.Cmdy(ice.WEB_LABEL, arg[0], "*", "favor", "search", arg[1:])
}},
},
}

View File

@ -167,7 +167,7 @@ var Index = &ice.Context{Name: "code", Help: "编程中心",
if m.Option("sid") != "" && m.Conf(cmd, []string{kit.MDB_HASH, m.Option("sid"), "status"}) != "" {
// 复用会话
m.Conf(cmd, []string{kit.MDB_HASH, m.Option("sid"), "status"}, "login")
m.Logs(ice.LOG_LOGIN, "sid", m.Option("sid"))
m.Logs(ice.LOG_AUTH, "sid", m.Option("sid"))
m.Echo(m.Option("sid"))
return
}
@ -184,13 +184,13 @@ var Index = &ice.Context{Name: "code", Help: "编程中心",
"hostname", m.Option("hostname"),
"username", m.Option("username"),
))
m.Logs(ice.LOG_LOGIN, "sid", h, "you", you)
m.Logs(ice.LOG_AUTH, "sid", h, "you", you)
m.Echo(h)
case "exit":
// 退出会话
m.Richs(cmd, nil, m.Option("sid"), func(key string, value map[string]interface{}) {
m.Logs(ice.LOG_LOGOUT, "sid", m.Option("sid"))
m.Logs(ice.LOG_AUTH, "sid", m.Option("sid"))
value["status"] = "logout"
m.Echo(key)
})

View File

@ -25,6 +25,23 @@ var Index = &ice.Context{Name: "vim", Help: "编辑器",
)},
},
Commands: map[string]*ice.Command{
ice.ICE_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Conf(ice.WEB_FAVOR, "meta.render.vimrc", m.AddCmd(&ice.Command{Name: "render favor id", Help: "渲染引擎", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
value := m.Optionv("value").(map[string]interface{})
switch value["name"] {
case "read":
p := path.Join(kit.Format(kit.Value(value, "extra.pwd")), kit.Format(kit.Value(value, "extra.buf")))
f, e := os.Open(p)
m.Assert(e)
defer f.Close()
b, e := ioutil.ReadAll(f)
m.Assert(e)
m.Echo(string(b))
case "write":
case "exec":
}
}}))
}},
ice.CODE_INSTALL: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
p := path.Join(m.Conf("install", "meta.path"), m.Conf("vim", "meta.version"))
if _, e := os.Stat(p); e != nil {
@ -74,7 +91,7 @@ var Index = &ice.Context{Name: "vim", Help: "编辑器",
m.Option("you", value["you"])
})
m.Logs(ice.LOG_LOGIN, "you", m.Option("you"), "url", m.Option(ice.MSG_USERURL), "cmd", m.Optionv("cmds"), "sub", m.Optionv("sub"))
m.Logs(ice.LOG_AUTH, "you", m.Option("you"), "url", m.Option(ice.MSG_USERURL), "cmd", m.Optionv("cmds"), "sub", m.Optionv("sub"))
m.Option(ice.MSG_OUTPUT, ice.RENDER_RESULT)
}},
"/help": {Name: "/help", Help: "帮助", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {

View File

@ -31,7 +31,7 @@ var Index = &ice.Context{Name: "zsh", Help: "命令行",
m.Option("you", value["you"])
})
m.Logs(ice.LOG_LOGIN, "you", m.Option("you"), "url", m.Option(ice.MSG_USERURL), "cmd", m.Optionv("cmds"), "sub", m.Optionv("sub"))
m.Logs(ice.LOG_AUTH, "you", m.Option("you"), "url", m.Option(ice.MSG_USERURL), "cmd", m.Optionv("cmds"), "sub", m.Optionv("sub"))
m.Option(ice.MSG_OUTPUT, ice.RENDER_RESULT)
}},

80
type.go
View File

@ -57,7 +57,7 @@ type Context struct {
server Server
wg *sync.WaitGroup
id int64
id int32
}
type Server interface {
Spawn(m *Message, c *Context, arg ...string) Server
@ -66,8 +66,8 @@ type Server interface {
Close(m *Message, arg ...string) bool
}
func (c *Context) ID() int64 {
return atomic.AddInt64(&c.id, 1)
func (c *Context) ID() int32 {
return atomic.AddInt32(&c.id, 1)
}
func (c *Context) Cap(key string, arg ...interface{}) string {
if len(arg) > 0 {
@ -315,7 +315,7 @@ func (m *Message) Formats(key string) string {
func (m *Message) Spawns(arg ...interface{}) *Message {
msg := m.Spawn(arg...)
msg.code = int(m.target.root.ID())
m.messages = append(m.messages, msg)
// m.messages = append(m.messages, msg)
return msg
}
func (m *Message) Spawn(arg ...interface{}) *Message {
@ -827,7 +827,7 @@ func (m *Message) Log(level string, str string, arg ...interface{}) *Message {
if os.Getenv("ctx_mod") != "" && m != nil {
// 输出日志
fmt.Fprintf(os.Stderr, "%s %02d %9s %s%s %s%s\n",
m.time.Format(ICE_TIME), m.code, fmt.Sprintf("%s->%s", m.source.Name, m.target.Name),
m.time.Format(ICE_TIME), m.code, fmt.Sprintf("%4s->%-4s", m.source.Name, m.target.Name),
prefix, level, str, suffix)
}
return m
@ -1453,40 +1453,38 @@ func (m *Message) Grows(key string, chain interface{}, match string, value strin
order := 0
if begin < current {
// 读取磁盘
// 读取文件
m.Log(LOG_INFO, "%s.%v read %v-%v from %v-%v", key, chain, begin, end, current, current+len(list))
store, _ := meta["record"].([]interface{})
for s := len(store) - 1; s > -1; s-- {
// 查找索引
item, _ := store[s].(map[string]interface{})
line := kit.Int(item["offset"])
m.Log(LOG_INFO, "check history %v %v %v", s, line, item)
m.Logs(LOG_INFO, "action", "check", "record", s, "offset", line, "count", item["count"])
if begin < line && s > 0 {
if kit.Int(item["count"]) != 0 {
s -= (line - begin) / kit.Int(item["count"])
}
// 向后查找
continue
}
for ; s < len(store); s++ {
if begin >= end {
break
}
// 查找偏移
for ; begin < end && s < len(store); s++ {
item, _ := store[s].(map[string]interface{})
name := kit.Format(item["file"])
pos := kit.Int(item["position"])
offset := kit.Int(item["offset"])
if offset+kit.Int(item["count"]) <= begin {
m.Log(LOG_INFO, "skip store %v %d", item, begin)
m.Logs(LOG_INFO, "action", "check", "record", s, "offset", line, "count", item["count"])
// 向前查找
continue
}
// 打开文件
m.Log(LOG_IMPORT, "load history %v %v %v", s, offset, item)
if f, e := os.Open(name); m.Assert(e) {
defer f.Close()
// 打开文件
r := csv.NewReader(f)
heads, _ := r.Read()
m.Log(LOG_IMPORT, "load head %v", heads)
m.Logs(LOG_IMPORT, "head", heads)
f.Seek(int64(pos), os.SEEK_SET)
r = csv.NewReader(f)
@ -1496,29 +1494,35 @@ func (m *Message) Grows(key string, chain interface{}, match string, value strin
m.Log(LOG_IMPORT, "load head %v", e)
break
}
if i < begin {
m.Logs(LOG_INFO, "action", "skip", "offset", i)
continue
}
if i >= begin {
item := map[string]interface{}{}
for i := range heads {
// 读取数据
item := map[string]interface{}{}
for i := range heads {
if heads[i] == "extra" {
item[heads[i]] = kit.UnMarshal(lines[i])
} else {
item[heads[i]] = lines[i]
}
m.Log(LOG_INFO, "load line %v %v %v", i, order, item)
if match == "" || strings.Contains(kit.Format(item[match]), value) {
// 读取文件
switch cb := cb.(type) {
case func(int, map[string]interface{}):
cb(order, item)
case func(int, map[string]interface{}) bool:
if cb(order, item) {
return meta
}
}
order++
}
begin = i + 1
} else {
m.Log(LOG_INFO, "skip line %v", i)
}
m.Logs(LOG_IMPORT, "offset", i, "type", item["type"], "name", item["name"], "text", item["text"])
if match == "" || strings.Contains(kit.Format(item[match]), value) {
// 匹配成功
switch cb := cb.(type) {
case func(int, map[string]interface{}):
cb(order, item)
case func(int, map[string]interface{}) bool:
if cb(order, item) {
return meta
}
}
order++
}
begin = i + 1
}
}
}
@ -1530,8 +1534,8 @@ func (m *Message) Grows(key string, chain interface{}, match string, value strin
begin = current
}
for i := begin - current; i < end-current; i++ {
// 读取缓存
if match == "" || strings.Contains(kit.Format(kit.Value(list[i], match)), value) {
// 读取缓存
switch cb := cb.(type) {
case func(int, map[string]interface{}):
cb(order, list[i].(map[string]interface{}))