diff --git a/src/context/mdb/mdb.go b/src/context/mdb/mdb.go index 9cfd4c31..3ff097d7 100644 --- a/src/context/mdb/mdb.go +++ b/src/context/mdb/mdb.go @@ -1,6 +1,6 @@ -package mdb // {{{ -// }}} -import ( // {{{ +package mdb + +import ( "context" "database/sql" @@ -9,14 +9,12 @@ import ( // {{{ "fmt" ) -// }}} - type MDB struct { *sql.DB *ctx.Context } -func (mdb *MDB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{ +func (mdb *MDB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { c.Caches = map[string]*ctx.Cache{ "source": &ctx.Cache{Name: "数据库参数", Value: "", Help: "数据库参数"}, "driver": &ctx.Cache{Name: "数据库驱动", Value: "", Help: "数据库驱动"}, @@ -28,56 +26,48 @@ func (mdb *MDB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server return s } -// }}} -func (mdb *MDB) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{ +func (mdb *MDB) Begin(m *ctx.Message, arg ...string) ctx.Server { + if mdb.Context == Index { + Pulse = m + } return mdb } -// }}} -func (mdb *MDB) Start(m *ctx.Message, arg ...string) bool { // {{{ +func (mdb *MDB) Start(m *ctx.Message, arg ...string) bool { if len(arg) > 0 { m.Cap("source", arg[0]) } if len(arg) > 1 { m.Cap("driver", arg[1]) } else { - m.Cap("driver", m.Conf("driver")) + m.Cap("driver", Pulse.Conf("driver")) } - if m.Cap("source") == "" || m.Cap("driver") == "" { return false } - m.Cap("stream", m.Cap("source")) db, e := sql.Open(m.Cap("driver"), m.Cap("source")) m.Assert(e) mdb.DB = db - m.Log("info", nil, "%d open %s %s", m.Capi("nsource", 1), m.Cap("driver"), m.Cap("source")) + m.Log("info", nil, "%d open %s %s", Pulse.Capi("nsource"), m.Cap("driver"), m.Cap("stream", m.Cap("source"))) return false } -// }}} -func (mdb *MDB) Close(m *ctx.Message, arg ...string) bool { // {{{ +func (mdb *MDB) Close(m *ctx.Message, arg ...string) bool { switch mdb.Context { case m.Target: - if mdb.Context == Index { - return false - } - if mdb.DB != nil { - m.Log("info", nil, "%d close %s %s", m.Capi("nsource", -1)+1, m.Cap("driver"), m.Cap("source")) + m.Log("info", nil, "%d close %s %s", Pulse.Capi("nsource", -1)+1, m.Cap("driver"), m.Cap("source")) mdb.DB.Close() mdb.DB = nil } case m.Source: } - return true } -// }}} - +var Pulse *ctx.Message var Index = &ctx.Context{Name: "mdb", Help: "数据中心", Caches: map[string]*ctx.Cache{ "nsource": &ctx.Cache{Name: "数据源数量", Value: "0", Help: "已打开数据库的数量"}, @@ -86,18 +76,15 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心", "driver": &ctx.Config{Name: "数据库驱动(mysql)", Value: "mysql", Help: "数据库驱动"}, }, Commands: map[string]*ctx.Command{ - "open": &ctx.Command{Name: "open name help source [driver]", Help: "打开数据库", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { - m.Assert(len(arg) > 2, "缺少参数") // {{{ - m.Master, m.Target = c, c - m.Cap("stream", m.Cap("nsource")) - m.Start(arg[0], "数据存储", arg[2:]...) - return "" - // }}} + "open": &ctx.Command{Name: "open source [driver]", Help: "打开数据库", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + m.Assert(len(arg) > 0, "缺少参数") + m.Start(fmt.Sprintf("db%d", Pulse.Capi("nsource", 1)), "数据存储", arg...) + Pulse.Cap("stream", Pulse.Cap("nsource")) }}, "exec": &ctx.Command{Name: "exec sql [arg]", Help: "操作数据库", Appends: map[string]string{"LastInsertId": "最后插入元组的标识", "RowsAffected": "修改元组的数量"}, - Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { - mdb, ok := m.Target.Server.(*MDB) // {{{ + Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + mdb, ok := m.Target.Server.(*MDB) m.Assert(ok, "目标模块类型错误") m.Assert(len(arg) > 0, "缺少参数") m.Assert(mdb.DB != nil, "数据库未打开") @@ -118,11 +105,9 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心", m.Add("append", "LastInsertId", fmt.Sprintf("%d", id)) m.Add("append", "RowsAffected", fmt.Sprintf("%d", n)) m.Log("info", nil, "last(%d) rows(%d)", id, n) - return "" - // }}} }}, - "query": &ctx.Command{Name: "query sql [arg]", Help: "执行查询语句", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { - mdb, ok := m.Target.Server.(*MDB) // {{{ + "query": &ctx.Command{Name: "query sql [arg]", Help: "执行查询语句", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + mdb, ok := m.Target.Server.(*MDB) m.Assert(ok, "目标模块类型错误") m.Assert(len(arg) > 0, "缺少参数") m.Assert(mdb.DB != nil, "数据库未打开") @@ -161,10 +146,15 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心", } m.Log("info", nil, "rows(%d) cols(%d)", len(m.Meta[m.Meta["append"][0]]), len(m.Meta["append"])) - return "" - // }}} }}, }, + Index: map[string]*ctx.Context{ + "void": &ctx.Context{Name: "void", + Commands: map[string]*ctx.Command{ + "open": &ctx.Command{}, + }, + }, + }, } func init() { diff --git a/src/context/nfs/nfs.go b/src/context/nfs/nfs.go index 02cd72b6..9b2e2201 100644 --- a/src/context/nfs/nfs.go +++ b/src/context/nfs/nfs.go @@ -1,6 +1,6 @@ -package nfs // {{{ -// }}} -import ( // {{{ +package nfs + +import ( "context" "fmt" @@ -9,69 +9,59 @@ import ( // {{{ "strconv" ) -// }}} - type NFS struct { file *os.File *ctx.Context } -func (nfs *NFS) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{ - info, ok := m.Data["info"].(os.FileInfo) - m.Assert(ok) +func (nfs *NFS) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { + file, e := os.OpenFile(arg[0], os.O_RDWR|os.O_CREATE, os.ModePerm) + m.Assert(e) + info, e := os.Stat(arg[0]) + m.Assert(e) c.Caches = map[string]*ctx.Cache{ "name": &ctx.Cache{Name: "name", Value: info.Name(), Help: "文件名"}, - "mode": &ctx.Cache{Name: "mode", Value: info.Mode().String(), Help: "文件名"}, - "time": &ctx.Cache{Name: "time", Value: info.ModTime().Format("15:03:04"), Help: "文件名"}, - "size": &ctx.Cache{Name: "size", Value: fmt.Sprintf("%d", info.Size()), Help: "文件名"}, - "pos": &ctx.Cache{Name: "pos", Value: "0", Help: "文件名"}, + "mode": &ctx.Cache{Name: "mode", Value: info.Mode().String(), Help: "文件权限"}, + "time": &ctx.Cache{Name: "time", Value: info.ModTime().Format("15:03:04"), Help: "创建时间"}, + "size": &ctx.Cache{Name: "size", Value: fmt.Sprintf("%d", info.Size()), Help: "文件大小"}, + "pos": &ctx.Cache{Name: "pos", Value: "0", Help: "读写位置"}, } c.Configs = map[string]*ctx.Config{} s := new(NFS) s.Context = c + s.file = file return s } -// }}} -func (nfs *NFS) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{ +func (nfs *NFS) Begin(m *ctx.Message, arg ...string) ctx.Server { if nfs.Context == Index { Pulse = m } return nfs - } -// }}} -func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{ - m.Log("info", nil, "%d open %s", Pulse.Capi("nfile", 1), m.Cap("name")) +func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { + m.Log("info", nil, "%d open %s", Pulse.Capi("nfile"), m.Cap("name")) m.Cap("stream", m.Cap("name")) return false } -// }}} -func (nfs *NFS) Close(m *ctx.Message, arg ...string) bool { // {{{ +func (nfs *NFS) Close(m *ctx.Message, arg ...string) bool { switch nfs.Context { case m.Target: - if nfs.Context == Index { - return false - } - if nfs.file != nil { m.Log("info", nil, "%d close %s", Pulse.Capi("nfile", -1)+1, m.Cap("name")) nfs.file.Close() - return true + nfs.file = nil } case m.Source: } - return true } -// }}} - var Pulse *ctx.Message var Index = &ctx.Context{Name: "nfs", Help: "存储中心", Caches: map[string]*ctx.Cache{ @@ -81,22 +71,11 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心", "size": &ctx.Config{Name: "size", Value: "1024", Help: "读取文件的默认大小值"}, }, Commands: map[string]*ctx.Command{ - "open": &ctx.Command{Name: "open file", Help: "打开文件, file: 文件名", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { - file, e := os.OpenFile(arg[0], os.O_RDWR|os.O_CREATE, os.ModePerm) // {{{ - m.Assert(e) - info, e := os.Stat(arg[0]) - m.Assert(e) - m.Put("option", "info", info).Start("file"+m.Cap("nfile"), "打开文件", arg...) - - nfs, ok := m.Target.Server.(*NFS) - m.Assert(ok) - - nfs.file = file - return "" - // }}} + "open": &ctx.Command{Name: "open file", Help: "打开文件, file: 文件名", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + m.Start(fmt.Sprintf("file%d", Pulse.Capi("nfile", 1)), "打开文件", arg...) }}, - "read": &ctx.Command{Name: "read [size [pos]]", Help: "读取文件, size: 读取大小, pos: 读取位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { - nfs, ok := m.Target.Server.(*NFS) // {{{ + "read": &ctx.Command{Name: "read [size [pos]]", Help: "读取文件, size: 读取大小, pos: 读取位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + nfs, ok := m.Target.Server.(*NFS) m.Assert(ok) var e error @@ -105,45 +84,47 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心", n, e = strconv.Atoi(arg[0]) m.Assert(e) } - - buf := make([]byte, n) if len(arg) > 1 { m.Cap("pos", arg[1]) } + buf := make([]byte, n) if n, e = nfs.file.ReadAt(buf, int64(m.Capi("pos"))); e != io.EOF { m.Assert(e) } + m.Echo(string(buf)) if m.Capi("pos", n); m.Capi("pos") == m.Capi("size") { m.Cap("pos", "0") } - - return string(buf) - // }}} }}, - "write": &ctx.Command{Name: "write string [pos]", Help: "写入文件, string: 写入内容, pos: 写入位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { - nfs, ok := m.Target.Server.(*NFS) // {{{ + "write": &ctx.Command{Name: "write string [pos]", Help: "写入文件, string: 写入内容, pos: 写入位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + nfs, ok := m.Target.Server.(*NFS) if m.Assert(ok); len(arg) > 1 { m.Cap("pos", arg[1]) } if len(arg[0]) == 0 { - e := nfs.file.Truncate(0) - m.Assert(e) - m.Cap("size", "0") + m.Assert(nfs.file.Truncate(int64(m.Capi("pos")))) + m.Cap("size", m.Cap("pos")) m.Cap("pos", "0") - return "" + } else { + n, e := nfs.file.WriteAt([]byte(arg[0]), int64(m.Capi("pos"))) + if m.Assert(e) && m.Capi("pos", n) > m.Capi("size") { + m.Cap("size", m.Cap("pos")) + } } - n, e := nfs.file.WriteAt([]byte(arg[0]), int64(m.Capi("pos"))) - if m.Assert(e); m.Capi("pos", n) > m.Capi("size") { - m.Cap("size", m.Cap("pos")) - } - return m.Cap("pos") - // }}} + m.Echo(m.Cap("pos")) }}, }, + Index: map[string]*ctx.Context{ + "void": &ctx.Context{Name: "void", + Commands: map[string]*ctx.Command{ + "open": &ctx.Command{}, + }, + }, + }, } func init() { diff --git a/src/context/tcp/tcp.go b/src/context/tcp/tcp.go index b8162a1d..c6b7d118 100644 --- a/src/context/tcp/tcp.go +++ b/src/context/tcp/tcp.go @@ -1,6 +1,6 @@ -package tcp // {{{ -// }}} -import ( // {{{ +package tcp + +import ( "context" "fmt" @@ -8,15 +8,13 @@ import ( // {{{ "strconv" ) -// }}} - type TCP struct { net.Conn net.Listener *ctx.Context } -func (tcp *TCP) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{ +func (tcp *TCP) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { c.Caches = map[string]*ctx.Cache{ "protocol": &ctx.Cache{Name: "protocol(tcp/tcp4/tcp6)", Value: m.Conf("protocol"), Help: "监听地址"}, "security": &ctx.Cache{Name: "security(true/false)", Value: m.Conf("security"), Help: "加密通信"}, @@ -27,20 +25,17 @@ func (tcp *TCP) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server s := new(TCP) s.Context = c return s - } -// }}} -func (tcp *TCP) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{ - if m.Target == Index { +func (tcp *TCP) Begin(m *ctx.Message, arg ...string) ctx.Server { + if tcp.Context == Index { Pulse = m } return tcp } -// }}} -func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool { // {{{ +func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool { if len(arg) > 1 { m.Cap("address", arg[1]) } @@ -53,92 +48,64 @@ func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool { // {{{ c, e := net.Dial(m.Cap("protocol"), m.Cap("address")) m.Assert(e) tcp.Conn = c - m.Log("info", nil, "dial(%d) %v->%v", m.Capi("nclient", 1), tcp.LocalAddr(), tcp.RemoteAddr()) - m.Cap("stream", fmt.Sprintf("%s->%s", tcp.LocalAddr(), tcp.RemoteAddr())) + m.Log("info", nil, "%s dial %s", Pulse.Cap("nclient"), m.Cap("stream", fmt.Sprintf("%s->%s", tcp.LocalAddr(), tcp.RemoteAddr()))) - msg := m.Spawn(m.Source).Put("option", "io", c) + msg := m.Reply("open").Put("option", "io", c) msg.Cmd("open") - msg.Cap("stream", tcp.RemoteAddr().String()) - - if tcp.Sessions == nil { - tcp.Sessions = make(map[string]*ctx.Message) - } - tcp.Sessions["open"] = msg - msg.Name = "open" - - // m.Reply(c.LocalAddr().String()).Put("option", "io", c).Cmd("open") + msg.Cap("stream", tcp.LocalAddr().String()) return false case "accept": c, e := m.Data["io"].(net.Conn) m.Assert(e) tcp.Conn = c - m.Log("info", nil, "accept(%d) %v<-%v", m.Capi("nclient", 1), tcp.LocalAddr(), tcp.RemoteAddr()) - m.Cap("stream", fmt.Sprintf("%s<-%s", tcp.LocalAddr(), tcp.RemoteAddr())) + m.Log("info", nil, "%s accept %s", Pulse.Cap("nclient"), m.Cap("stream", fmt.Sprintf("%s<-%s", tcp.LocalAddr(), tcp.RemoteAddr()))) - s, e := m.Data["source"].(*ctx.Context) - m.Assert(e) - msg := m.Spawn(s).Put("option", "io", c) + msg := m.Spawn(m.Data["source"].(*ctx.Context), "open").Put("option", "io", c) msg.Cmd("open") msg.Cap("stream", tcp.RemoteAddr().String()) - - if tcp.Sessions == nil { - tcp.Sessions = make(map[string]*ctx.Message) - } - tcp.Sessions["open"] = msg - msg.Name = "open" - - // m.Reply(c.RemoteAddr().String()) return false } l, e := net.Listen(m.Cap("protocol"), m.Cap("address")) m.Assert(e) tcp.Listener = l - m.Log("info", nil, "listen(%d) %v", m.Capi("nlisten", 1), l.Addr()) - m.Cap("stream", fmt.Sprintf("%s", l.Addr())) + m.Log("info", nil, "%d listen %v", Pulse.Capi("nlisten"), m.Cap("stream", fmt.Sprintf("%s", l.Addr()))) for { c, e := l.Accept() m.Assert(e) - m.Spawn(Index).Put("option", "io", c).Put("option", "source", m.Source).Start(fmt.Sprintf("com%d", m.Capi("nclient", 1)), "网络连接", "accept", c.RemoteAddr().String()) + m.Spawn(Index).Put("option", "io", c).Put("option", "source", m.Source).Start(fmt.Sprintf("com%d", Pulse.Capi("nclient", 1)), "网络连接", "accept", c.RemoteAddr().String()) } return true } -// }}} -func (tcp *TCP) Close(m *ctx.Message, arg ...string) bool { // {{{ +func (tcp *TCP) Close(m *ctx.Message, arg ...string) bool { switch tcp.Context { case m.Target: - if tcp.Context == Index { - return false - } - if tcp.Listener != nil { - m.Log("info", nil, "%d close %v", Pulse.Capi("nlisten", -1)+1, tcp.Listener.Addr()) + m.Log("info", nil, "%d close %v", Pulse.Capi("nlisten", -1)+1, m.Cap("stream")) tcp.Listener.Close() tcp.Listener = nil } + if tcp.Conn != nil { + m.Log("info", nil, "%d close %v", Pulse.Capi("nclient", -1)+1, m.Cap("stream")) + tcp.Conn.Close() + tcp.Conn = nil + } case m.Source: if tcp.Conn != nil { msg := m.Spawn(tcp.Context) - msg.Master = tcp.Context - if !tcp.Context.Close(msg, arg...) { + if msg.Master = tcp.Context; !tcp.Context.Close(msg, arg...) { return false } } } - if tcp.Conn != nil { - m.Log("info", nil, "%d close %v", Pulse.Capi("nclient", -1)+1, tcp.Conn.LocalAddr()) - tcp.Conn.Close() - tcp.Conn = nil - } return true } -// }}} - +var Pulse *ctx.Message var Index = &ctx.Context{Name: "tcp", Help: "网络中心", Caches: map[string]*ctx.Cache{ "nlisten": &ctx.Cache{Name: "nlisten", Value: "0", Help: "监听数量"}, @@ -149,53 +116,29 @@ var Index = &ctx.Context{Name: "tcp", Help: "网络中心", "security": &ctx.Config{Name: "security(true/false)", Value: "false", Help: "加密通信"}, }, Commands: map[string]*ctx.Command{ - "listen": &ctx.Command{Name: "listen [address [security]]", Help: "监听连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { - switch len(arg) { // {{{ - case 0: - m.Travel(nil, func(m *ctx.Message) bool { - if tcp, ok := m.Target.Server.(*TCP); ok && tcp.Listener != nil { - m.Echo("%s %v\n", m.Target.Name, tcp.Addr()) - } - return true - }) - default: - m.Start(fmt.Sprintf("pub%d", m.Capi("nlisten")+1), "网络监听", m.Meta["detail"]...) - } - return "" - // }}} + "listen": &ctx.Command{Name: "listen address [security]", Help: "监听连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + m.Start(fmt.Sprintf("pub%d", Pulse.Capi("nlisten", 1)), "网络监听", m.Meta["detail"]...) }}, - "dial": &ctx.Command{Name: "dial [address [security]]", Help: "建立连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { - switch len(arg) { // {{{ - case 0: - m.Travel(nil, func(m *ctx.Message) bool { - if tcp, ok := m.Target.Server.(*TCP); ok && tcp.Conn != nil { - m.Echo("%s %v<->%v\n", m.Target.Name, tcp.LocalAddr(), tcp.RemoteAddr()) - } - return true - }) - default: - m.Start(fmt.Sprintf("com%d", m.Capi("nclient")+1), "网络连接", m.Meta["detail"]...) - } - return "" - // }}} + "dial": &ctx.Command{Name: "dial [address [security]]", Help: "建立连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + m.Start(fmt.Sprintf("com%d", Pulse.Capi("nclient", 1)), "网络连接", m.Meta["detail"]...) }}, - "send": &ctx.Command{Name: "send message", Help: "发送消息", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { - if tcp, ok := m.Target.Server.(*TCP); ok && tcp.Conn != nil { // {{{ - tcp.Conn.Write([]byte(arg[0])) - } - return "" - // }}} + "send": &ctx.Command{Name: "send message", Help: "发送消息", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + tcp, ok := m.Target.Server.(*TCP) + m.Assert(ok && tcp.Conn != nil) + + tcp.Conn.Write([]byte(arg[0])) + }}, - "recv": &ctx.Command{Name: "recv size", Help: "接收消息", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { + "recv": &ctx.Command{Name: "recv size", Help: "接收消息", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + tcp, ok := m.Target.Server.(*TCP) + m.Assert(ok && tcp.Conn != nil) + size, e := strconv.Atoi(arg[0]) m.Assert(e) - if tcp, ok := m.Target.Server.(*TCP); ok && tcp.Conn != nil { // {{{ - buf := make([]byte, size) - tcp.Conn.Read(buf) - return string(buf) - } - return "" - // }}} + + buf := make([]byte, size) + tcp.Conn.Read(buf) + m.Echo(string(buf)) }}, }, Index: map[string]*ctx.Context{ @@ -208,8 +151,6 @@ var Index = &ctx.Context{Name: "tcp", Help: "网络中心", }, } -var Pulse *ctx.Message - func init() { tcp := &TCP{} tcp.Context = Index diff --git a/src/context/web/web.go b/src/context/web/web.go index e7c6453c..d4fa7171 100644 --- a/src/context/web/web.go +++ b/src/context/web/web.go @@ -1,6 +1,6 @@ -package web // {{{ -// }}} -import ( // {{{ +package web + +import ( "context" "html/template" @@ -13,12 +13,10 @@ import ( // {{{ "strings" ) -// }}} - type MUX interface { Handle(string, http.Handler) HandleFunc(string, func(http.ResponseWriter, *http.Request)) - Trans(*ctx.Message, string, func(*ctx.Message, *ctx.Context, string, ...string) string) + Trans(*ctx.Message, string, func(*ctx.Message, *ctx.Context, string, ...string)) } type WEB struct { @@ -29,10 +27,9 @@ type WEB struct { *ctx.Context } -func (web *WEB) Trans(m *ctx.Message, key string, hand func(*ctx.Message, *ctx.Context, string, ...string) string) { // {{{ +func (web *WEB) Trans(m *ctx.Message, key string, hand func(*ctx.Message, *ctx.Context, string, ...string)) { web.HandleFunc(key, func(w http.ResponseWriter, r *http.Request) { - msg := m.Spawn(m.Target) - msg.Set("detail", key) + msg := m.Spawn(m.Target).Set("detail", key) for k, v := range r.Form { msg.Add("option", k) msg.Meta[k] = v @@ -44,16 +41,11 @@ func (web *WEB) Trans(m *ctx.Message, key string, hand func(*ctx.Message, *ctx.C msg.Log("cmd", nil, "%s %v", key, msg.Meta["option"]) msg.Put("option", "request", r) msg.Put("option", "response", w) - - ret := hand(msg, msg.Target, key) - if ret != "" { - msg.Echo(ret) - } + hand(msg, msg.Target, key) header := w.Header() for _, k := range msg.Meta["append"] { - ce := &http.Cookie{Name: k, Value: msg.Get(k)} - header.Add("Set-Cookie", ce.String()) + header.Add("Set-Cookie", (&http.Cookie{Name: k, Value: msg.Get(k)}).String()) } for _, v := range msg.Meta["result"] { w.Write([]byte(v)) @@ -61,12 +53,10 @@ func (web *WEB) Trans(m *ctx.Message, key string, hand func(*ctx.Message, *ctx.C }) } -// }}} -func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{ +func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { if web.Message != nil { log.Println() web.Log("cmd", nil, "%v %s %s", r.RemoteAddr, r.Method, r.URL) - defer log.Println() if web.Cap("logheaders") == "yes" { for k, v := range r.Header { @@ -75,8 +65,7 @@ func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{ log.Println() } - r.ParseForm() - if len(r.PostForm) > 0 { + if r.ParseForm(); len(r.PostForm) > 0 { for k, v := range r.PostForm { log.Printf("%s: %s", k, v[0]) } @@ -86,21 +75,16 @@ func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{ web.ServeMux.ServeHTTP(w, r) - if web.Message != nil { - if web.Cap("logheaders") == "yes" { - for k, v := range w.Header() { - log.Println(k+":", v[0]) - } + if web.Message != nil && web.Cap("logheaders") == "yes" { + for k, v := range w.Header() { + log.Println(k+":", v[0]) } + log.Println() } } -// }}} - -func (web *WEB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{ - c.Caches = map[string]*ctx.Cache{ - "directory": &ctx.Cache{Name: "directory", Value: "usr", Help: "服务目录"}, - } +func (web *WEB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { + c.Caches = map[string]*ctx.Cache{} c.Configs = map[string]*ctx.Config{} s := new(WEB) @@ -108,15 +92,14 @@ func (web *WEB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server return s } -// }}} -func (web *WEB) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{ - if len(arg) > 0 { - m.Cap("directory", arg[0]) - } - +func (web *WEB) Begin(m *ctx.Message, arg ...string) ctx.Server { + web.Caches["directory"] = &ctx.Cache{Name: "directory", Value: "usr", Help: "服务目录"} web.Caches["route"] = &ctx.Cache{Name: "route", Value: "/" + web.Context.Name + "/", Help: "请求路径"} web.Caches["register"] = &ctx.Cache{Name: "已初始化(yes/no)", Value: "no", Help: "模块是否已注册"} web.Caches["master"] = &ctx.Cache{Name: "master(yes/no)", Value: "no", Help: "日志输出请求头"} + if len(arg) > 0 { + m.Cap("directory", arg[0]) + } web.ServeMux = http.NewServeMux() if mux, ok := m.Target.Server.(MUX); ok { @@ -130,8 +113,7 @@ func (web *WEB) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{ return web } -// }}} -func (web *WEB) Start(m *ctx.Message, arg ...string) bool { // {{{ +func (web *WEB) Start(m *ctx.Message, arg ...string) bool { if len(arg) > 0 { m.Cap("directory", arg[0]) } @@ -192,32 +174,23 @@ func (web *WEB) Start(m *ctx.Message, arg ...string) bool { // {{{ return true } -// }}} -func (web *WEB) Close(m *ctx.Message, arg ...string) bool { // {{{ +func (web *WEB) Close(m *ctx.Message, arg ...string) bool { switch web.Context { case m.Target: case m.Source: } - return true } -// }}} - var Index = &ctx.Context{Name: "web", Help: "应用中心", - Caches: map[string]*ctx.Cache{ - "directory": &ctx.Cache{Name: "directory", Value: "usr", Help: "服务目录"}, - }, + Caches: map[string]*ctx.Cache{}, Configs: map[string]*ctx.Config{}, Commands: map[string]*ctx.Command{ - "listen": &ctx.Command{Name: "listen [directory [address [protocol]]]", Help: "开启网页服务", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { - m.Meta["detail"] = arg // {{{ - m.Target.Start(m) - return "" - // }}} + "listen": &ctx.Command{Name: "listen [directory [address [protocol]]]", Help: "开启网页服务", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + m.Set("detail", arg...).Target.Start(m) }}, - "route": &ctx.Command{Name: "route [directory|template|script] route string", Help: "添加响应", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { - mux, ok := m.Target.Server.(MUX) // {{{ + "route": &ctx.Command{Name: "route [directory|template|script] route string", Help: "添加响应", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + mux, ok := m.Target.Server.(MUX) m.Assert(ok, "模块类型错误") m.Assert(len(arg) == 3, "缺少参数") @@ -225,7 +198,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", case "directory": mux.Handle(arg[1], http.FileServer(http.Dir(arg[2]))) case "template": - mux.Trans(m, arg[1], func(m *ctx.Message, c *ctx.Context, key string, a ...string) string { // {{{ + mux.Trans(m, arg[1], func(m *ctx.Message, c *ctx.Context, key string, a ...string) { w := m.Data["response"].(http.ResponseWriter) if _, e := os.Stat(arg[2]); e == nil { @@ -234,16 +207,13 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", template.Must(template.New("temp").Parse(arg[2])).Execute(w, m) } - return "" }) - // }}} case "script": - cli := m.Find("cli", true) // {{{ + cli := m.Find("cli", true) lex := m.Find("lex", true) - mux.Trans(m, arg[1], func(m *ctx.Message, c *ctx.Context, key string, a ...string) string { + mux.Trans(m, arg[1], func(m *ctx.Message, c *ctx.Context, key string, a ...string) { f, e := os.Open(arg[2]) line, bio := "", bufio.NewReader(f) - if e != nil { line = arg[2] } @@ -260,18 +230,12 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", break } } - - return "" }) - // }}} } - - return "" - // }}} }}, - "/hi": &ctx.Command{Name: "/hi", Help: "添加响应", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { + "/hi": &ctx.Command{Name: "/hi", Help: "添加响应", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { m.Add("append", "hi", "hello") - return "hello" + m.Echo("hello\n") }}, }, }