1
0
mirror of https://shylinux.com/x/icebergs synced 2025-06-26 18:37:29 +08:00

add conf.go

This commit is contained in:
shaoying 2019-12-19 14:41:28 +08:00
parent 21e814083b
commit e31594f131
5 changed files with 155 additions and 114 deletions

16
base.go
View File

@ -19,6 +19,7 @@ func (f *Frame) Spawn(m *Message, c *Context, arg ...string) Server {
func (f *Frame) Begin(m *Message, arg ...string) Server { func (f *Frame) Begin(m *Message, arg ...string) Server {
list := map[*Context]*Message{m.target: m} list := map[*Context]*Message{m.target: m}
m.Travel(func(p *Context, s *Context) { m.Travel(func(p *Context, s *Context) {
s.root = m.target
if msg, ok := list[p]; ok && msg != nil { if msg, ok := list[p]; ok && msg != nil {
sub := msg.Spawns(s) sub := msg.Spawns(s)
s.Begin(sub, arg...) s.Begin(sub, arg...)
@ -92,7 +93,8 @@ var Index = &Context{Name: "ice", Help: "冰山模块",
m.Echo(strings.Join(kit.Simple(m.Confv("help", "index")), "\n")) m.Echo(strings.Join(kit.Simple(m.Confv("help", "index")), "\n"))
}}, }},
"exit": {Name: "exit", Help: "hello", Hand: func(m *Message, c *Context, cmd string, arg ...string) { "exit": {Name: "exit", Help: "hello", Hand: func(m *Message, c *Context, cmd string, arg ...string) {
Code = kit.Int(kit.Select("0", arg, 0)) f := m.root.target.server.(*Frame)
f.code = kit.Int(kit.Select("0", arg, 0))
m.root.Cmd("_exit") m.root.Cmd("_exit")
}}, }},
"_exit": {Name: "_init", Help: "hello", Hand: func(m *Message, c *Context, cmd string, arg ...string) { "_exit": {Name: "_init", Help: "hello", Hand: func(m *Message, c *Context, cmd string, arg ...string) {
@ -116,14 +118,12 @@ var Pulse = &Message{
source: Index, target: Index, Hand: true, source: Index, target: Index, Hand: true,
} }
func init() { var Log func(*Message, string, string)
Index.root = Index
Pulse.root = Pulse
}
var Code = 0
func Run(arg ...string) string { func Run(arg ...string) string {
Index.root = Index
Pulse.root = Pulse
if len(arg) == 0 { if len(arg) == 0 {
arg = os.Args[1:] arg = os.Args[1:]
} }
@ -138,6 +138,6 @@ func Run(arg ...string) string {
frame.Close(Pulse.Spawns(), arg...) frame.Close(Pulse.Spawns(), arg...)
} }
time.Sleep(time.Second) time.Sleep(time.Second)
os.Exit(Code) os.Exit(frame.code)
return Pulse.Result() return Pulse.Result()
} }

View File

@ -33,13 +33,13 @@ func Cookie(msg *ice.Message, sessid string) string {
w := msg.Optionv("response").(http.ResponseWriter) w := msg.Optionv("response").(http.ResponseWriter)
expire := time.Now().Add(kit.Duration(msg.Conf("aaa.sess", "meta.expire"))) expire := time.Now().Add(kit.Duration(msg.Conf("aaa.sess", "meta.expire")))
msg.Log("cookie", "expire %v sessid %s", kit.Format(expire), sessid) msg.Log("cookie", "expire %v sessid %s", kit.Format(expire), sessid)
http.SetCookie(w, &http.Cookie{Name: "sessid", Value: sessid, Path: "/", Expires: expire}) http.SetCookie(w, &http.Cookie{Name: ice.WEB_SESS, Value: sessid, Path: "/", Expires: expire})
return sessid return sessid
} }
func (web *WEB) Login(msg *ice.Message, w http.ResponseWriter, r *http.Request) bool { func (web *WEB) Login(msg *ice.Message, w http.ResponseWriter, r *http.Request) bool {
if msg.Options("sessid") { if msg.Options(ice.WEB_SESS) {
sub := msg.Cmd("aaa.sess", "check", msg.Option("sessid")) sub := msg.Cmd("aaa.sess", "check", msg.Option(ice.WEB_SESS))
msg.Log("info", "role: %s user: %s", msg.Option("userrole", sub.Append("userrole")), msg.Log("info", "role: %s user: %s", msg.Option("userrole", sub.Append("userrole")),
msg.Option("username", sub.Append("username"))) msg.Option("username", sub.Append("username")))
} }
@ -159,7 +159,7 @@ func (web *WEB) HandleCmd(m *ice.Message, key string, cmd *ice.Command) {
msg.Option("accept", r.Header.Get("Accept")) msg.Option("accept", r.Header.Get("Accept"))
msg.Option("method", r.Method) msg.Option("method", r.Method)
msg.Option("url", r.URL.Path) msg.Option("url", r.URL.Path)
msg.Option("sessid", "") msg.Option(ice.WEB_SESS, "")
// 请求环境 // 请求环境
for _, v := range r.Cookies() { for _, v := range r.Cookies() {
@ -273,7 +273,7 @@ func (web *WEB) Start(m *ice.Message, arg ...string) bool {
}) })
port := kit.Select(m.Conf("spide", "self.port"), arg, 0) port := kit.Select(m.Conf("spide", "self.port"), arg, 0)
m.Cap("stream", port) m.Cap(ice.CTX_STREAM, port)
web.m = m web.m = m
web.Server = &http.Server{Addr: port, Handler: web} web.Server = &http.Server{Addr: port, Handler: web}
m.Log("serve", "node %v", m.Conf("cli.runtime", "node")) m.Log("serve", "node %v", m.Conf("cli.runtime", "node"))
@ -289,7 +289,7 @@ var Index = &ice.Context{Name: "web", Help: "网页模块",
Caches: map[string]*ice.Cache{}, Caches: map[string]*ice.Cache{},
Configs: map[string]*ice.Config{ Configs: map[string]*ice.Config{
"spide": {Name: "客户端", Value: map[string]interface{}{ "spide": {Name: "客户端", Value: map[string]interface{}{
"self": map[string]interface{}{"port": "127.0.0.1:9020"}, "self": map[string]interface{}{"port": ice.WEB_PORT},
}}, }},
"serve": {Name: "服务端", Value: map[string]interface{}{ "serve": {Name: "服务端", Value: map[string]interface{}{
"static": map[string]interface{}{"/": "usr/volcanos/", "static": map[string]interface{}{"/": "usr/volcanos/",
@ -307,16 +307,16 @@ var Index = &ice.Context{Name: "web", Help: "网页模块",
}, },
}}, }},
"space": {Name: "空间端", Value: map[string]interface{}{ "space": {Name: "空间端", Value: map[string]interface{}{
"meta": map[string]interface{}{"buffer": 4096, "redial": 3000}, ice.MDB_META: map[string]interface{}{"buffer": 4096, "redial": 3000},
"hash": map[string]interface{}{}, ice.MDB_HASH: map[string]interface{}{},
"list": map[string]interface{}{}, ice.MDB_LIST: map[string]interface{}{},
}}, }},
}, },
Commands: map[string]*ice.Command{ Commands: map[string]*ice.Command{
"_init": {Name: "_init", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { ice.ICE_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Echo("hello %s world", c.Name) m.Echo("hello %s world", c.Name)
}}, }},
"_exit": {Name: "_exit", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { ice.ICE_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Done() m.Done()
}}, }},
"serve": {Name: "hi", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { "serve": {Name: "hi", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
@ -401,7 +401,7 @@ var Index = &ice.Context{Name: "web", Help: "网页模块",
id := kit.Format(c.ID()) id := kit.Format(c.ID())
m.Optionv("_source", []string{id, target[0]}) m.Optionv("_source", []string{id, target[0]})
m.Optionv("_target", target[1:]) m.Optionv("_target", target[1:])
m.Set("detail", arg[1:]...) m.Set(ice.MSG_DETAIL, arg[1:]...)
web := m.Target().Server().(*WEB) web := m.Target().Server().(*WEB)
web.send[id] = m web.send[id] = m

42
conf.go Normal file
View File

@ -0,0 +1,42 @@
package ice
const (
ICE_INIT = "_init"
ICE_EXIT = "_exit"
ICE_TIME = "2006-01-02 15:04:05"
)
const (
CTX_STATUS = "status"
CTX_STREAM = "stream"
)
const (
MSG_DETAIL = "detail"
MSG_OPTION = "option"
MSG_APPEND = "append"
MSG_RESULT = "result"
)
const (
MDB_META = "meta"
MDB_LIST = "hash"
MDB_HASH = "list"
)
const (
WEB_PORT = ":9020"
WEB_SESS = "sessid"
)
const (
LOG_CMD = "cmd"
LOG_INFO = "info"
LOG_WARN = "warn"
LOG_ERROR = "error"
LOG_BEGIN = "begin"
LOG_START = "start"
LOG_BENCH = "bench"
LOG_CLOSE = "close"
)

View File

@ -12,19 +12,24 @@ var Index = &ice.Context{Name: "chat", Help: "聊天模块",
Caches: map[string]*ice.Cache{}, Caches: map[string]*ice.Cache{},
Configs: map[string]*ice.Config{ Configs: map[string]*ice.Config{
"group": {Name: "group", Value: map[string]interface{}{ "group": {Name: "group", Value: map[string]interface{}{
"meta": map[string]interface{}{}, ice.MDB_META: map[string]interface{}{},
"list": map[string]interface{}{}, ice.MDB_LIST: map[string]interface{}{},
"hash": map[string]interface{}{}, ice.MDB_HASH: map[string]interface{}{},
}}, }},
}, },
Commands: map[string]*ice.Command{ Commands: map[string]*ice.Command{
"_init": {Name: "_init", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { ice.ICE_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Cmd("ctx.config", "load", "var/conf/aaa.json") m.Cmd("ctx.config", "load", "var/conf/aaa.json")
m.Cmd("ctx.config", "load", "var/conf/chat.json") m.Cmd("ctx.config", "load", "var/conf/chat.json")
}}, }},
ice.ICE_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Cmd("ctx.config", "save", "var/conf/chat.json", "web.chat.group")
m.Cmd("ctx.config", "save", "var/conf/aaa.json", "aaa.role", "aaa.user", "aaa.sess")
}},
"_login": {Name: "_login", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { "_login": {Name: "_login", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
if cmd != "/login" { if cmd != "/login" {
if !m.Options("sessid") || !m.Options("username") { if !m.Options(ice.WEB_SESS) || !m.Options("username") {
m.Option("path", "") m.Option("path", "")
m.Log("warn", "not login") m.Log("warn", "not login")
m.Echo("error").Echo("not login") m.Echo("error").Echo("not login")
@ -39,10 +44,6 @@ var Index = &ice.Context{Name: "chat", Help: "聊天模块",
} }
m.Log("info", "river: %s storm: %s", m.Option("sess.river"), m.Option("sess.storm")) m.Log("info", "river: %s storm: %s", m.Option("sess.river"), m.Option("sess.storm"))
}}, }},
"_exit": {Name: "_init", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Cmd("ctx.config", "save", "var/conf/chat.json", "web.chat.group")
m.Cmd("ctx.config", "save", "var/conf/aaa.json", "aaa.role", "aaa.user", "aaa.sess")
}},
"/login": {Name: "/login", Help: "登录", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { "/login": {Name: "/login", Help: "登录", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
switch arg[0] { switch arg[0] {
@ -60,7 +61,7 @@ var Index = &ice.Context{Name: "chat", Help: "聊天模块",
"/ocean": {Name: "/ocean", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { "/ocean": {Name: "/ocean", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
if len(arg) == 0 { if len(arg) == 0 {
m.Confm("aaa.user", "hash", func(key string, value map[string]interface{}) { m.Confm("aaa.user", ice.MDB_HASH, func(key string, value map[string]interface{}) {
m.Push("key", key) m.Push("key", key)
m.Push("user.route", m.Conf("cli.runtime", "node.name")) m.Push("user.route", m.Conf("cli.runtime", "node.name"))
}) })
@ -69,17 +70,17 @@ var Index = &ice.Context{Name: "chat", Help: "聊天模块",
switch arg[0] { switch arg[0] {
case "spawn": case "spawn":
if arg[1] == "" { if arg[1] == "" {
arg[1] = kit.ShortKey(m.Confm("group", "hash"), 6) arg[1] = kit.ShortKey(m.Confm("group", ice.MDB_HASH), 6)
} }
user := map[string]interface{}{ user := map[string]interface{}{
"meta": map[string]interface{}{}, ice.MDB_META: map[string]interface{}{},
"hash": map[string]interface{}{}, ice.MDB_HASH: map[string]interface{}{},
"list": []interface{}{}, ice.MDB_LIST: []interface{}{},
} }
tool := map[string]interface{}{ tool := map[string]interface{}{
"meta": map[string]interface{}{}, ice.MDB_META: map[string]interface{}{},
"hash": map[string]interface{}{}, ice.MDB_HASH: map[string]interface{}{},
"list": []interface{}{}, ice.MDB_LIST: []interface{}{},
} }
for _, v := range arg[3:] { for _, v := range arg[3:] {
kit.Value(user, "hash."+v, map[string]interface{}{ kit.Value(user, "hash."+v, map[string]interface{}{
@ -93,7 +94,7 @@ var Index = &ice.Context{Name: "chat", Help: "聊天模块",
} }
m.Conf("group", "hash."+arg[1], map[string]interface{}{ m.Conf("group", "hash."+arg[1], map[string]interface{}{
"meta": map[string]interface{}{ ice.MDB_META: map[string]interface{}{
"create_time": m.Time(), "create_time": m.Time(),
"name": arg[2], "name": arg[2],
}, },
@ -107,7 +108,7 @@ var Index = &ice.Context{Name: "chat", Help: "聊天模块",
}}, }},
"/river": {Name: "/river", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { "/river": {Name: "/river", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
if len(arg) == 0 { if len(arg) == 0 {
m.Confm("group", "hash", func(key string, value map[string]interface{}) { m.Confm("group", ice.MDB_HASH, func(key string, value map[string]interface{}) {
m.Push("key", key) m.Push("key", key)
m.Push("name", kit.Value(value["meta"], "name")) m.Push("name", kit.Value(value["meta"], "name"))
}) })
@ -151,7 +152,7 @@ var Index = &ice.Context{Name: "chat", Help: "聊天模块",
if len(arg) < 2 { if len(arg) < 2 {
m.Push("user", m.Conf("cli.runtime", "boot.username")) m.Push("user", m.Conf("cli.runtime", "boot.username"))
m.Push("node", m.Conf("cli.runtime", "node.name")) m.Push("node", m.Conf("cli.runtime", "node.name"))
m.Confm("web.space", "hash", func(key string, value map[string]interface{}) { m.Confm("web.space", ice.MDB_HASH, func(key string, value map[string]interface{}) {
m.Push("user", m.Conf("cli.runtime", "boot.username")) m.Push("user", m.Conf("cli.runtime", "boot.username"))
m.Push("node", value["name"]) m.Push("node", value["name"])
}) })
@ -172,11 +173,11 @@ var Index = &ice.Context{Name: "chat", Help: "聊天模块",
}) })
} }
m.Conf("group", "hash."+arg[0]+".tool.hash."+arg[2], map[string]interface{}{ m.Conf("group", "hash."+arg[0]+".tool.hash."+arg[2], map[string]interface{}{
"meta": map[string]interface{}{ ice.MDB_META: map[string]interface{}{
"create_time": m.Time(), "create_time": m.Time(),
}, },
"hash": map[string]interface{}{}, ice.MDB_HASH: map[string]interface{}{},
"list": list, ice.MDB_LIST: list,
}) })
m.Log("info", "steam spawn %v %v %v", arg[0], arg[2], list) m.Log("info", "steam spawn %v %v %v", arg[0], arg[2], list)

140
type.go
View File

@ -87,10 +87,10 @@ func (c *Context) Register(s *Context, x Server) *Context {
} }
func (c *Context) Begin(m *Message, arg ...string) *Context { func (c *Context) Begin(m *Message, arg ...string) *Context {
c.Caches["status"] = &Cache{Name: "status", Value: ""} c.Caches[CTX_STATUS] = &Cache{Name: CTX_STATUS, Value: ""}
c.Caches["stream"] = &Cache{Name: "stream", Value: ""} c.Caches[CTX_STREAM] = &Cache{Name: CTX_STREAM, Value: ""}
m.Log("begin", "%s", c.Name) m.Log(LOG_BEGIN, "%s", c.Name)
if c.begin = m; c.server != nil { if c.begin = m; c.server != nil {
m.TryCatch(m, true, func(m *Message) { m.TryCatch(m, true, func(m *Message) {
c.server.Begin(m, arg...) c.server.Begin(m, arg...)
@ -108,19 +108,19 @@ func (c *Context) Start(m *Message, arg ...string) bool {
wait := make(chan bool) wait := make(chan bool)
m.Gos(m, func(m *Message) { m.Gos(m, func(m *Message) {
m.Log("start", "%s", c.Name) m.Log(LOG_START, "%s", c.Name)
c.Cap("status", "start") c.Cap(CTX_STATUS, "start")
wait <- true wait <- true
c.server.Start(m, arg...) c.server.Start(m, arg...)
c.Cap("status", "close") c.Cap(CTX_STATUS, "close")
m.Done() m.Done()
}) })
<-wait <-wait
return true return true
} }
func (c *Context) Close(m *Message, arg ...string) bool { func (c *Context) Close(m *Message, arg ...string) bool {
m.Log("close", "%s", c.Name) m.Log(LOG_CLOSE, "%s", c.Name)
if c.server != nil { if c.server != nil {
return c.server.Close(m, arg...) return c.server.Close(m, arg...)
} }
@ -154,7 +154,7 @@ func (m *Message) Time(args ...interface{}) string {
} }
} }
} }
f := "2006-01-02 15:04:05" f := ICE_TIME
if len(args) > 0 { if len(args) > 0 {
switch arg := args[0].(type) { switch arg := args[0].(type) {
case string: case string:
@ -194,13 +194,13 @@ func (m *Message) Format(key interface{}) string {
msg := ms[i] msg := ms[i]
meta = append(meta, fmt.Sprintf("%s ", msg.Format("prefix"))) meta = append(meta, fmt.Sprintf("%s ", msg.Format("prefix")))
if len(msg.meta["detail"]) > 0 { if len(msg.meta[MSG_DETAIL]) > 0 {
meta = append(meta, fmt.Sprintf("detail:%d %v", len(msg.meta["detail"]), msg.meta["detail"])) meta = append(meta, fmt.Sprintf("detail:%d %v", len(msg.meta[MSG_DETAIL]), msg.meta[MSG_DETAIL]))
} }
if len(msg.meta["option"]) > 0 { if len(msg.meta[MSG_OPTION]) > 0 {
meta = append(meta, fmt.Sprintf("option:%d %v\n", len(msg.meta["option"]), msg.meta["option"])) meta = append(meta, fmt.Sprintf("option:%d %v\n", len(msg.meta[MSG_OPTION]), msg.meta[MSG_OPTION]))
for _, k := range msg.meta["option"] { for _, k := range msg.meta[MSG_OPTION] {
if v, ok := msg.meta[k]; ok { if v, ok := msg.meta[k]; ok {
meta = append(meta, fmt.Sprintf(" %s: %d %v\n", k, len(v), v)) meta = append(meta, fmt.Sprintf(" %s: %d %v\n", k, len(v), v))
} }
@ -209,16 +209,16 @@ func (m *Message) Format(key interface{}) string {
meta = append(meta, "\n") meta = append(meta, "\n")
} }
if len(msg.meta["append"]) > 0 { if len(msg.meta[MSG_APPEND]) > 0 {
meta = append(meta, fmt.Sprintf(" append:%d %v\n", len(msg.meta["append"]), msg.meta["append"])) meta = append(meta, fmt.Sprintf(" append:%d %v\n", len(msg.meta[MSG_APPEND]), msg.meta[MSG_APPEND]))
for _, k := range msg.meta["append"] { for _, k := range msg.meta[MSG_APPEND] {
if v, ok := msg.meta[k]; ok { if v, ok := msg.meta[k]; ok {
meta = append(meta, fmt.Sprintf(" %s: %d %v\n", k, len(v), v)) meta = append(meta, fmt.Sprintf(" %s: %d %v\n", k, len(v), v))
} }
} }
} }
if len(msg.meta["result"]) > 0 { if len(msg.meta[MSG_RESULT]) > 0 {
meta = append(meta, fmt.Sprintf(" result:%d %v\n", len(msg.meta["result"]), msg.meta["result"])) meta = append(meta, fmt.Sprintf(" result:%d %v\n", len(msg.meta[MSG_RESULT]), msg.meta[MSG_RESULT]))
} }
} }
return strings.Join(meta, "") return strings.Join(meta, "")
@ -242,7 +242,7 @@ func (m *Message) Format(key interface{}) string {
case []byte: case []byte:
json.Unmarshal(key, &m.meta) json.Unmarshal(key, &m.meta)
} }
return m.time.Format("2006-01-02 15:04:05") return m.time.Format(ICE_TIME)
} }
func (m *Message) Formats(key string) string { func (m *Message) Formats(key string) string {
switch key { switch key {
@ -251,7 +251,7 @@ func (m *Message) Formats(key string) string {
default: default:
return m.Format(key) return m.Format(key)
} }
return m.time.Format("2006-01-02 15:04:05") return m.time.Format(ICE_TIME)
} }
func (m *Message) Spawn(arg ...interface{}) *Message { func (m *Message) Spawn(arg ...interface{}) *Message {
msg := &Message{ msg := &Message{
@ -287,10 +287,10 @@ func (m *Message) Spawns(arg ...interface{}) *Message {
func (m *Message) Add(key string, arg ...string) *Message { func (m *Message) Add(key string, arg ...string) *Message {
switch key { switch key {
case "detail", "result": case MSG_DETAIL, MSG_RESULT:
m.meta[key] = append(m.meta[key], arg...) m.meta[key] = append(m.meta[key], arg...)
case "option", "append": case MSG_OPTION, MSG_APPEND:
if len(arg) > 0 { if len(arg) > 0 {
if kit.IndexOf(m.meta[key], arg[0]) == -1 { if kit.IndexOf(m.meta[key], arg[0]) == -1 {
m.meta[key] = append(m.meta[key], arg[0]) m.meta[key] = append(m.meta[key], arg[0])
@ -302,9 +302,9 @@ func (m *Message) Add(key string, arg ...string) *Message {
} }
func (m *Message) Set(key string, arg ...string) *Message { func (m *Message) Set(key string, arg ...string) *Message {
switch key { switch key {
case "detail", "result": case MSG_DETAIL, MSG_RESULT:
delete(m.meta, key) delete(m.meta, key)
case "option", "append": case MSG_OPTION, MSG_APPEND:
if len(arg) > 0 { if len(arg) > 0 {
delete(m.meta, arg[0]) delete(m.meta, arg[0])
} else { } else {
@ -318,16 +318,16 @@ func (m *Message) Set(key string, arg ...string) *Message {
return m.Add(key, arg...) return m.Add(key, arg...)
} }
func (m *Message) Copy(msg *Message) *Message { func (m *Message) Copy(msg *Message) *Message {
for _, k := range msg.meta["append"] { for _, k := range msg.meta[MSG_APPEND] {
if kit.IndexOf(m.meta["append"], k) == -1 { if kit.IndexOf(m.meta[MSG_APPEND], k) == -1 {
m.meta["append"] = append(m.meta["append"], k) m.meta[MSG_APPEND] = append(m.meta[MSG_APPEND], k)
} }
for _, v := range msg.meta[k] { for _, v := range msg.meta[k] {
m.meta[k] = append(m.meta[k], v) m.meta[k] = append(m.meta[k], v)
} }
} }
for _, v := range msg.meta["result"] { for _, v := range msg.meta[MSG_RESULT] {
m.meta["result"] = append(m.meta["result"], v) m.meta[MSG_RESULT] = append(m.meta[MSG_RESULT], v)
} }
return m return m
} }
@ -345,17 +345,17 @@ func (m *Message) Push(key string, value interface{}, arg ...interface{}) *Messa
} }
for _, k := range list { for _, k := range list {
if k == "key" { if k == "key" {
m.Add("append", k, k) m.Add(MSG_APPEND, k, k)
} else { } else {
m.Add("append", k, kit.Format(value[k])) m.Add(MSG_APPEND, k, kit.Format(value[k]))
} }
} }
return m return m
} }
return m.Add("append", key, kit.Format(value)) return m.Add(MSG_APPEND, key, kit.Format(value))
} }
func (m *Message) Echo(str string, arg ...interface{}) *Message { func (m *Message) Echo(str string, arg ...interface{}) *Message {
m.meta["result"] = append(m.meta["result"], fmt.Sprintf(str, arg...)) m.meta[MSG_RESULT] = append(m.meta[MSG_RESULT], fmt.Sprintf(str, arg...))
return m return m
} }
func (m *Message) Sort(key string, arg ...string) *Message { func (m *Message) Sort(key string, arg ...string) *Message {
@ -412,13 +412,13 @@ func (m *Message) Sort(key string, arg ...string) *Message {
} }
} }
for _, k := range m.meta["append"] { for _, k := range m.meta[MSG_APPEND] {
delete(m.meta, k) delete(m.meta, k)
} }
for _, v := range table { for _, v := range table {
for _, k := range m.meta["append"] { for _, k := range m.meta[MSG_APPEND] {
m.Add("append", k, v[k]) m.Add(MSG_APPEND, k, v[k])
} }
} }
return m return m
@ -428,17 +428,17 @@ func (m *Message) Table(cbs ...interface{}) *Message {
switch cb := cbs[0].(type) { switch cb := cbs[0].(type) {
case func(int, map[string]string, []string): case func(int, map[string]string, []string):
nrow := 0 nrow := 0
for _, k := range m.meta["append"] { for _, k := range m.meta[MSG_APPEND] {
if len(m.meta[k]) > nrow { if len(m.meta[k]) > nrow {
nrow = len(m.meta[k]) nrow = len(m.meta[k])
} }
} }
for i := 0; i < nrow; i++ { for i := 0; i < nrow; i++ {
line := map[string]string{} line := map[string]string{}
for _, k := range m.meta["append"] { for _, k := range m.meta[MSG_APPEND] {
line[k] = kit.Select("", m.meta[k], i) line[k] = kit.Select("", m.meta[k], i)
} }
cb(i, line, m.meta["append"]) cb(i, line, m.meta[MSG_APPEND])
} }
} }
return m return m
@ -447,7 +447,7 @@ func (m *Message) Table(cbs ...interface{}) *Message {
//计算列宽 //计算列宽
space := kit.Select(m.Conf("table", "space"), m.Option("table.space")) space := kit.Select(m.Conf("table", "space"), m.Option("table.space"))
depth, width := 0, map[string]int{} depth, width := 0, map[string]int{}
for _, k := range m.meta["append"] { for _, k := range m.meta[MSG_APPEND] {
if len(m.meta[k]) > depth { if len(m.meta[k]) > depth {
depth = len(m.meta[k]) depth = len(m.meta[k])
} }
@ -465,7 +465,7 @@ func (m *Message) Table(cbs ...interface{}) *Message {
compact := kit.Select(m.Conf("table", "compact"), m.Option("table.compact")) == "true" compact := kit.Select(m.Conf("table", "compact"), m.Option("table.compact")) == "true"
cb := func(maps map[string]string, lists []string, line int) bool { cb := func(maps map[string]string, lists []string, line int) bool {
for i, v := range lists { for i, v := range lists {
if k := m.meta["append"][i]; compact { if k := m.meta[MSG_APPEND][i]; compact {
v = maps[k] v = maps[k]
} }
@ -480,7 +480,7 @@ func (m *Message) Table(cbs ...interface{}) *Message {
// 输出表头 // 输出表头
row := map[string]string{} row := map[string]string{}
wor := []string{} wor := []string{}
for _, k := range m.meta["append"] { for _, k := range m.meta[MSG_APPEND] {
row[k], wor = k, append(wor, k+strings.Repeat(space, width[k]-kit.Width(k, len(space)))) row[k], wor = k, append(wor, k+strings.Repeat(space, width[k]-kit.Width(k, len(space))))
} }
if !cb(row, wor, -1) { if !cb(row, wor, -1) {
@ -491,7 +491,7 @@ func (m *Message) Table(cbs ...interface{}) *Message {
for i := 0; i < depth; i++ { for i := 0; i < depth; i++ {
row := map[string]string{} row := map[string]string{}
wor := []string{} wor := []string{}
for _, k := range m.meta["append"] { for _, k := range m.meta[MSG_APPEND] {
data := "" data := ""
if i < len(m.meta[k]) { if i < len(m.meta[k]) {
data = m.meta[k][i] data = m.meta[k][i]
@ -513,8 +513,8 @@ func (m *Message) Options(key string, arg ...interface{}) bool {
} }
func (m *Message) Optionv(key string, arg ...interface{}) interface{} { func (m *Message) Optionv(key string, arg ...interface{}) interface{} {
if len(arg) > 0 { if len(arg) > 0 {
if kit.IndexOf(m.meta["option"], key) == -1 { if kit.IndexOf(m.meta[MSG_OPTION], key) == -1 {
m.meta["option"] = append(m.meta["option"], key) m.meta[MSG_OPTION] = append(m.meta[MSG_OPTION], key)
} }
switch arg := arg[0].(type) { switch arg := arg[0].(type) {
@ -544,20 +544,18 @@ func (m *Message) Appendv(key string, arg ...interface{}) []string {
return m.meta[key] return m.meta[key]
} }
func (m *Message) Resultv(arg ...interface{}) []string { func (m *Message) Resultv(arg ...interface{}) []string {
return m.meta["result"] return m.meta[MSG_RESULT]
} }
func (m *Message) Result(arg ...interface{}) string { func (m *Message) Result(arg ...interface{}) string {
return strings.Join(m.Resultv(), "") return strings.Join(m.Resultv(), "")
} }
func (m *Message) Detailv(arg ...interface{}) []string { func (m *Message) Detailv(arg ...interface{}) []string {
return m.meta["detail"] return m.meta[MSG_DETAIL]
} }
func (m *Message) Detail(arg ...interface{}) string { func (m *Message) Detail(arg ...interface{}) string {
return kit.Select("", m.meta["detail"], 0) return kit.Select("", m.meta[MSG_DETAIL], 0)
} }
var Log func(*Message, string, string)
func (m *Message) Log(level string, str string, arg ...interface{}) *Message { func (m *Message) Log(level string, str string, arg ...interface{}) *Message {
if Log != nil { if Log != nil {
Log(m, level, fmt.Sprintf(str, arg...)) Log(m, level, fmt.Sprintf(str, arg...))
@ -572,7 +570,7 @@ func (m *Message) Log(level string, str string, arg ...interface{}) *Message {
prefix, suffix = "\033[31m", "\033[0m" prefix, suffix = "\033[31m", "\033[0m"
} }
fmt.Fprintf(os.Stderr, "%s %d %s->%s %s%s %s%s\n", fmt.Fprintf(os.Stderr, "%s %d %s->%s %s%s %s%s\n",
time.Now().Format("2006-01-02 15:04:05"), m.code, m.source.Name, m.target.Name, time.Now().Format(ICE_TIME), m.code, m.source.Name, m.target.Name,
prefix, level, fmt.Sprintf(str, arg...), suffix) prefix, level, fmt.Sprintf(str, arg...), suffix)
return m return m
} }
@ -594,11 +592,11 @@ func (m *Message) TryCatch(msg *Message, safe bool, hand ...func(msg *Message))
case io.EOF: case io.EOF:
case nil: case nil:
default: default:
m.Log("bench", "chain: %s", msg.Format("chain")) m.Log(LOG_BENCH, "chain: %s", msg.Format("chain"))
m.Log("bench", "catch: %s", e) m.Log(LOG_BENCH, "catch: %s", e)
m.Log("bench", "stack: %s", msg.Format("stack")) m.Log(LOG_BENCH, "stack: %s", msg.Format("stack"))
if m.Log("error", "catch: %s", e); len(hand) > 1 { if m.Log(LOG_ERROR, "catch: %s", e); len(hand) > 1 {
m.TryCatch(msg, safe, hand[1:]...) m.TryCatch(msg, safe, hand[1:]...)
} else if !safe { } else if !safe {
m.Assert(e) m.Assert(e)
@ -666,7 +664,7 @@ func (m *Message) Search(key interface{}, cb func(p *Context, s *Context, key st
} }
} }
if p == nil { if p == nil {
m.Log("warn", "not found %s", key) m.Log(LOG_WARN, "not found %s", key)
break break
} }
cb(p.context, p, list[len(list)-1]) cb(p.context, p, list[len(list)-1])
@ -686,8 +684,8 @@ func (m *Message) Run(arg ...string) *Message {
} }
func (m *Message) Runs(key string, cmd string, arg ...string) *Message { func (m *Message) Runs(key string, cmd string, arg ...string) *Message {
if s, ok := m.Target().Commands[key]; ok { if s, ok := m.Target().Commands[key]; ok {
m.meta["detail"] = append([]string{cmd}, arg...) m.meta[MSG_DETAIL] = append([]string{cmd}, arg...)
m.Log("cmd", "%s.%s %s %v", m.Target().Name, key, cmd, arg) m.Log(LOG_CMD, "%s.%s %s %v", m.Target().Name, key, cmd, arg)
s.Hand(m, m.Target(), cmd, arg...) s.Hand(m, m.Target(), cmd, arg...)
} }
return m return m
@ -804,7 +802,7 @@ func (m *Message) Grow(key string, args interface{}, data interface{}) map[strin
} }
} }
m.Log("info", "save %s offset %v+%v", name, offset, count) m.Log(LOG_INFO, "save %s offset %v+%v", name, offset, count)
meta["offset"] = offset + count meta["offset"] = offset + count
list = list[:least] list = list[:least]
w.Flush() w.Flush()
@ -843,13 +841,13 @@ func (m *Message) Grows(key string, args interface{}, cb interface{}) map[string
begin := end - limit begin := end - limit
data := make([]interface{}, 0, limit) data := make([]interface{}, 0, limit)
m.Log("info", "read %v-%v from %v-%v", begin, end, current, current+len(list)) m.Log(LOG_INFO, "read %v-%v from %v-%v", begin, end, current, current+len(list))
if begin < current { if begin < current {
store, _ := meta["record"].([]interface{}) store, _ := meta["record"].([]interface{})
for s := len(store) - 1; s > -1; s-- { for s := len(store) - 1; s > -1; s-- {
item, _ := store[s].(map[string]interface{}) item, _ := store[s].(map[string]interface{})
line := kit.Int(item["offset"]) line := kit.Int(item["offset"])
m.Log("info", "check history %v %v %v", s, line, item) m.Log(LOG_INFO, "check history %v %v %v", s, line, item)
if begin < line && s > 0 { if begin < line && s > 0 {
continue continue
} }
@ -866,12 +864,12 @@ func (m *Message) Grows(key string, args interface{}, cb interface{}) map[string
name := kit.Format(item["file"]) name := kit.Format(item["file"])
pos := kit.Int(item["position"]) pos := kit.Int(item["position"])
line := kit.Int(item["offset"]) line := kit.Int(item["offset"])
m.Log("info", "load history %v %v %v", s, line, item) m.Log(LOG_INFO, "load history %v %v %v", s, line, item)
if f, e := os.Open(name); m.Assert(e) { if f, e := os.Open(name); m.Assert(e) {
defer f.Close() defer f.Close()
r := csv.NewReader(f) r := csv.NewReader(f)
heads, _ := r.Read() heads, _ := r.Read()
m.Log("info", "load head %v", heads) m.Log(LOG_INFO, "load head %v", heads)
f.Seek(int64(pos), os.SEEK_SET) f.Seek(int64(pos), os.SEEK_SET)
r = csv.NewReader(f) r = csv.NewReader(f)
@ -886,13 +884,13 @@ func (m *Message) Grows(key string, args interface{}, cb interface{}) map[string
for i := range heads { for i := range heads {
item[heads[i]] = lines[i] item[heads[i]] = lines[i]
} }
m.Log("info", "load line %v %v %v", i, len(data), item) m.Log(LOG_INFO, "load line %v %v %v", i, len(data), item)
if match == "" || strings.Contains(kit.Format(item[match]), value) { if match == "" || strings.Contains(kit.Format(item[match]), value) {
data = append(data, item) data = append(data, item)
} }
begin = i + 1 begin = i + 1
} else { } else {
m.Log("info", "skip line %v", i) m.Log(LOG_INFO, "skip line %v", i)
} }
} }
} }
@ -904,7 +902,7 @@ func (m *Message) Grows(key string, args interface{}, cb interface{}) map[string
if begin < current { if begin < current {
begin = current begin = current
} }
m.Log("info", "cache %v-%v", begin-current, end-current) m.Log(LOG_INFO, "cache %v-%v", begin-current, end-current)
for i := begin - current; i < end-current; i++ { for i := begin - current; i < end-current; i++ {
if match == "" || strings.Contains(kit.Format(kit.Value(list[i], match)), value) { if match == "" || strings.Contains(kit.Format(kit.Value(list[i], match)), value) {
data = append(data, list[i]) data = append(data, list[i])
@ -920,15 +918,15 @@ func (m *Message) Cmdy(arg ...interface{}) *Message {
return m return m
} }
func (m *Message) Cmdx(arg ...interface{}) string { func (m *Message) Cmdx(arg ...interface{}) string {
return kit.Select("", m.Cmd(arg...).meta["result"], 0) return kit.Select("", m.Cmd(arg...).meta[MSG_RESULT], 0)
} }
func (m *Message) Cmds(arg ...interface{}) bool { func (m *Message) Cmds(arg ...interface{}) bool {
return kit.Select("", m.Cmd(arg...).meta["result"], 0) != "" return kit.Select("", m.Cmd(arg...).meta[MSG_RESULT], 0) != ""
} }
func (m *Message) Cmd(arg ...interface{}) *Message { func (m *Message) Cmd(arg ...interface{}) *Message {
list := kit.Simple(arg...) list := kit.Simple(arg...)
if len(list) == 0 { if len(list) == 0 {
list = m.meta["detail"] list = m.meta[MSG_DETAIL]
} }
if len(list) == 0 { if len(list) == 0 {
return m return m
@ -939,8 +937,8 @@ func (m *Message) Cmd(arg ...interface{}) *Message {
if cmd, ok := c.Commands[key]; ok { if cmd, ok := c.Commands[key]; ok {
m.TryCatch(m.Spawns(s), true, func(msg *Message) { m.TryCatch(m.Spawns(s), true, func(msg *Message) {
msg.Log("cmd", "%s.%s %v", c.Name, key, list[1:]) msg.Log(LOG_CMD, "%s.%s %v", c.Name, key, list[1:])
msg.meta["detail"] = list msg.meta[MSG_DETAIL] = list
cmd.Hand(msg, c, key, list[1:]...) cmd.Hand(msg, c, key, list[1:]...)
msg.Hand = true msg.Hand = true
m.Hand = true m.Hand = true