1
0
forked from x/icebergs
This commit is contained in:
shaoying 2019-12-25 22:02:43 +08:00
parent cba3e395e5
commit bc2aaa1e6d
4 changed files with 40 additions and 37 deletions

View File

@ -100,6 +100,7 @@ func (web *Frame) HandleWSS(m *ice.Message, safe bool, c *websocket.Conn) bool {
msg.Optionv(ice.MSG_TARGET, target)
socket.WriteMessage(t, []byte(msg.Format("meta")))
msg.Info("send %v %v->%v %v", t, source, target, msg.Format("meta"))
msg.Log("cost", "%s: ", msg.Format("cost"))
if msg.Detail() == "exit" {
return true
}
@ -127,12 +128,10 @@ func (web *Frame) HandleCGI(m *ice.Message, alias map[string]interface{}, which
for k, v := range alias {
list := kit.Simple(v)
if v, ok := m.Target().Commands[list[0]]; ok {
m.Log("info", "%v, %v", k, v.Name)
cb(k, list[1:], v)
}
}
for k, v := range m.Target().Commands {
m.Log("info", "%v, %v", k, v.Name)
if strings.HasPrefix(k, "/") || strings.HasPrefix(k, "_") {
continue
}
@ -144,15 +143,12 @@ func (web *Frame) HandleCGI(m *ice.Message, alias map[string]interface{}, which
// tmpl = template.Must(tmpl.ParseGlob(path.Join(m.Conf(ice.WEB_SERVE, ice.Meta("template", "path")), m.Target().Name, "/*.tmpl")))
tmpl = template.Must(tmpl.ParseFiles(which))
m.Confm(ice.WEB_SERVE, ice.Meta("template", "list"), func(index int, value string) { tmpl = template.Must(tmpl.Parse(value)) })
for i, v := range tmpl.Templates() {
m.Log("info", "%v, %v", i, v.Name())
}
return tmpl
}
func (web *Frame) HandleCmd(m *ice.Message, key string, cmd *ice.Command) {
web.HandleFunc(key, func(w http.ResponseWriter, r *http.Request) {
m.TryCatch(m.Spawns(), true, func(msg *ice.Message) {
defer func() { msg.Log("cost", msg.Format("cost")) }()
defer func() { msg.Log("cost", "%s: %s %v", msg.Format("cost"), r.URL.Path, msg.Optionv("cmds")) }()
// 解析请求
msg.Optionv("request", r)
@ -495,12 +491,11 @@ var Index = &ice.Context{Name: "web", Help: "网页模块",
m.Info("send %s %s", id, m.Format("meta"))
// 下发命令
now := time.Now()
m.Target().Server().(*Frame).send[id] = m
socket.WriteMessage(MSG_MAPS, []byte(m.Format("meta")))
m.Call(true, func(msg *ice.Message) *ice.Message {
// 返回结果
m.Copy(msg).Log("cost", "cost: %s", kit.FmtTime(kit.Int64(time.Now().Sub(now))))
m.Copy(msg).Log("cost", "%s: %s %v", m.Format("cost"), arg[0], arg[1:])
return nil
})
}
@ -517,11 +512,12 @@ var Index = &ice.Context{Name: "web", Help: "网页模块",
if len(arg) > 1 {
switch arg[1] {
case "启动":
arg = arg[:1]
case "停止", "stop":
m.Cmd(ice.WEB_SPACE, arg[0], "exit", "1")
time.Sleep(time.Second * 3)
m.Cmd(ice.GDB_EVENT, "action", ice.DREAM_CLOSE, arg[0])
return
arg = arg[:0]
}
}
@ -892,21 +888,13 @@ var Index = &ice.Context{Name: "web", Help: "网页模块",
switch arg[0] {
case "add":
node := kit.Dict(
kit.MDB_TYPE, arg[1], kit.MDB_NAME, arg[2], kit.MDB_TEXT, arg[3],
)
switch arg[1] {
case "story":
node["data"] = arg[3]
default:
node["data"] = m.Cmd(ice.WEB_CACHE, "add", arg[1:]).Append("data")
}
// 创建共享
h := m.Rich(ice.WEB_SHARE, nil, node)
h := m.Rich(ice.WEB_SHARE, nil, kit.Dict(
kit.MDB_TYPE, arg[1], kit.MDB_NAME, arg[2], kit.MDB_TEXT, arg[3],
))
m.Grow(ice.WEB_SHARE, nil, kit.Dict(
kit.MDB_TYPE, arg[1], kit.MDB_NAME, arg[2], kit.MDB_TEXT, arg[3],
"data", node["data"], "share", h,
"share", h,
))
m.Info("share: %s", h)
m.Echo(h)
@ -954,13 +942,13 @@ var Index = &ice.Context{Name: "web", Help: "网页模块",
"share", m.Cmdx(ice.WEB_SHARE, "add", m.Option("node"), m.Option("name"), m.Option("user")),
"socket", s,
))
m.Info("conn %s", m.Option(kit.MDB_NAME))
m.Info("space: %s", m.Option(kit.MDB_NAME))
m.Gos(m, func(m *ice.Message) {
// 监听消息
web := m.Target().Server().(*Frame)
web.HandleWSS(m, false, s)
m.Info("close %s %s", m.Option(kit.MDB_NAME), kit.Format(m.Confv(ice.WEB_SPACE, kit.Keys(kit.MDB_HASH, m.Option(kit.MDB_NAME)))))
m.Log("close", "%s: %s", m.Option(kit.MDB_NAME), kit.Format(m.Confv(ice.WEB_SPACE, kit.Keys(kit.MDB_HASH, h))))
m.Confv(ice.WEB_SPACE, kit.Keys(kit.MDB_HASH, h), "")
})
}

View File

@ -59,6 +59,7 @@ const (
)
const (
LOG_CMD = "cmd"
LOG_COST = "cost"
LOG_INFO = "info"
LOG_WARN = "warn"
LOG_ERROR = "error"

View File

@ -191,7 +191,11 @@ var Index = &ice.Context{Name: "chat", Help: "聊天模块",
// 执行命令
m.Grows(ice.CHAT_GROUP, prefix, kit.MDB_ID, kit.Format(kit.Int(arg[2])+1), func(index int, value map[string]interface{}) {
if meta, ok := kit.Value(value, "meta").(map[string]interface{}); ok {
m.Cmdy(ice.WEB_SPACE, meta["pod"], ice.CTX_COMMAND, meta["ctx"], meta["cmd"], "run", arg[3:])
if kit.Format(meta["pod"]) == m.Conf(ice.CLI_RUNTIME, "node.name") {
m.Cmdy(kit.Keys(meta["ctx"], meta["cmd"]), arg[3:])
} else {
m.Cmdy(ice.WEB_SPACE, meta["pod"], kit.Keys(meta["ctx"], meta["cmd"]), arg[3:])
}
}
})
}},

36
type.go
View File

@ -599,11 +599,11 @@ func (m *Message) Log(level string, str string, arg ...interface{}) *Message {
}
prefix, suffix := "", ""
switch level {
case "cmd", "start", "serve":
case LOG_CMD, "start", "serve":
prefix, suffix = "\033[32m", "\033[0m"
case "cost":
case LOG_COST:
prefix, suffix = "\033[33m", "\033[0m"
case "warn", "close":
case LOG_WARN, LOG_ERROR, "close":
prefix, suffix = "\033[31m", "\033[0m"
}
fmt.Fprintf(os.Stderr, "%s %d %s->%s %s%s %s%s\n",
@ -611,6 +611,10 @@ func (m *Message) Log(level string, str string, arg ...interface{}) *Message {
prefix, level, str, suffix)
return m
}
func (m *Message) Cost(str string, arg ...interface{}) *Message {
m.Log(LOG_COST, "%s: %s", m.Format("cost"), kit.Format(str, arg...))
return m.Log(LOG_INFO, str, arg...)
}
func (m *Message) Info(str string, arg ...interface{}) *Message {
return m.Log(LOG_INFO, str, arg...)
}
@ -657,10 +661,10 @@ func (m *Message) TryCatch(msg *Message, safe bool, hand ...func(msg *Message))
case io.EOF:
case nil:
default:
m.Log(LOG_ERROR, "catch: %s", e)
m.Log(LOG_BENCH, "chain: %s", msg.Format("chain"))
m.Log(LOG_BENCH, "catch: %s", e)
m.Log(LOG_ERROR, "catch: %s", e)
m.Log(LOG_BENCH, "stack: %s", msg.Format("stack"))
if m.Log(LOG_ERROR, "catch: %s", e); len(hand) > 1 {
m.TryCatch(msg, safe, hand[1:]...)
} else if !safe {
@ -935,10 +939,13 @@ func (m *Message) Grow(key string, chain interface{}, data interface{}) int {
least := kit.Int(kit.Select(m.Conf(WEB_CACHE, Meta("least")), meta["least"]))
// 创建文件
name := kit.Select(path.Join(m.Conf(WEB_CACHE, Meta("store")), kit.Keys(key, chain, "csv")), meta["store"])
name := path.Join(kit.Select(m.Conf(WEB_CACHE, Meta("store")), meta["store"]), kit.Keys(key, chain, "csv"))
f, e := os.OpenFile(name, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
if e != nil {
f, _, e = kit.Create(name)
m.Info("%s.%v create: %s", key, chain, name)
} else {
m.Info("%s.%v append: %s", key, chain, name)
}
defer f.Close()
s, e := f.Stat()
@ -991,15 +998,15 @@ func (m *Message) Grow(key string, chain interface{}, data interface{}) int {
}
}
m.Log(LOG_INFO, "save %s offset %v+%v", name, offset, count)
m.Log(LOG_INFO, "%s.%v save %s offset %v+%v", key, chain, name, offset, count)
meta["offset"] = offset + count
list = list[:least]
w.Flush()
}
return id
}
func (m *Message) Grows(key string, args interface{}, match string, value string, cb interface{}) map[string]interface{} {
cache := m.Confm(key, args)
func (m *Message) Grows(key string, chain interface{}, match string, value string, cb interface{}) map[string]interface{} {
cache := m.Confm(key, chain)
if cache == nil {
return nil
}
@ -1021,8 +1028,8 @@ func (m *Message) Grows(key string, args interface{}, match string, value string
}
data := make([]interface{}, 0, limit)
m.Log(LOG_INFO, "read %v-%v from %v-%v", begin, end, current, current+len(list))
if begin < current {
m.Log(LOG_INFO, "%s.%s 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{})
@ -1082,7 +1089,6 @@ func (m *Message) Grows(key string, args interface{}, match string, value string
if begin < current {
begin = current
}
m.Log(LOG_INFO, "cache %v-%v", begin-current, end-current)
for i := begin - current; i < end-current; i++ {
if match == "" || strings.Contains(kit.Format(kit.Value(list[i], match)), value) {
data = append(data, list[i])
@ -1114,9 +1120,13 @@ func (m *Message) Cmd(arg ...interface{}) *Message {
m.Search(list[0], func(p *Context, c *Context, key string, cmd *Command) {
m.TryCatch(m.Spawns(c), true, func(msg *Message) {
m.Hand, m = true, msg
msg.meta[MSG_DETAIL] = list
c.Run(msg, cmd, key, list[1:]...)
m.Hand, msg.Hand, m = true, true, msg
if you := m.Option(kit.Format(kit.Value(cmd.Meta, "remote"))); you != "" {
msg.Copy(msg.Spawns(c).Cmd(WEB_SPACE, you, list[0], list[1:]))
} else {
c.Run(msg, cmd, key, list[1:]...)
}
})
})