forked from x/ContextOS
tce mod web.get 添加了参数模板
This commit is contained in:
parent
abfa04eabc
commit
1b72e597c6
@ -1,2 +1,5 @@
|
|||||||
~aaa login root root
|
~aaa login root root
|
||||||
source etc/local.shy
|
|
||||||
|
~web serve ./ ":9090"
|
||||||
|
# source etc/local.shy
|
||||||
|
|
||||||
|
@ -102,7 +102,7 @@ func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
|||||||
yac.Cmd("train", "num", "num", "mul{", "[1-9][0-9]*", "0[0-9]+", "0x[0-9]+", "}")
|
yac.Cmd("train", "num", "num", "mul{", "[1-9][0-9]*", "0[0-9]+", "0x[0-9]+", "}")
|
||||||
yac.Cmd("train", "str", "str", "mul{", "\"[^\"]*\"", "'[^']*'", "}")
|
yac.Cmd("train", "str", "str", "mul{", "\"[^\"]*\"", "'[^']*'", "}")
|
||||||
|
|
||||||
yac.Cmd("train", "tran", "tran", "mul{", "@", "$", "}", "opt{", "[a-zA-Z0-9]+", "}")
|
yac.Cmd("train", "tran", "tran", "mul{", "@", "$", "}", "opt{", "$", "[a-zA-Z0-9]+", "}")
|
||||||
yac.Cmd("train", "word", "word", "mul{", "~", "!", "tran", "str", "[a-zA-Z0-9_/.]+", "}")
|
yac.Cmd("train", "word", "word", "mul{", "~", "!", "tran", "str", "[a-zA-Z0-9_/.]+", "}")
|
||||||
|
|
||||||
yac.Cmd("train", "op1", "op1", "mul{", "$", "@", "}")
|
yac.Cmd("train", "op1", "op1", "mul{", "$", "@", "}")
|
||||||
|
@ -2,6 +2,7 @@ package mdb // {{{
|
|||||||
// }}}
|
// }}}
|
||||||
import ( // {{{
|
import ( // {{{
|
||||||
"contexts"
|
"contexts"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
@ -17,6 +18,8 @@ type MDB struct {
|
|||||||
|
|
||||||
list map[string][]string
|
list map[string][]string
|
||||||
list_key []string
|
list_key []string
|
||||||
|
|
||||||
|
table []string
|
||||||
*ctx.Context
|
*ctx.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,9 +172,17 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
|
|||||||
// }}}
|
// }}}
|
||||||
}},
|
}},
|
||||||
"table": &ctx.Command{Name: "table", Help: "执行查询语句", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
"table": &ctx.Command{Name: "table", Help: "执行查询语句", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
msg := m.Spawn(m.Target()) // {{{
|
mdb, ok := m.Target().Server.(*MDB) // {{{
|
||||||
|
m.Assert(ok)
|
||||||
|
msg := m.Spawn(m.Target())
|
||||||
if len(arg) > 0 {
|
if len(arg) > 0 {
|
||||||
msg.Cmd("query", fmt.Sprintf("desc %s", arg[0]))
|
table := arg[0]
|
||||||
|
index, e := strconv.Atoi(arg[0])
|
||||||
|
if e == nil && index < len(mdb.table) {
|
||||||
|
table = mdb.table[index]
|
||||||
|
}
|
||||||
|
msg.Cmd("query", fmt.Sprintf("desc %s", table))
|
||||||
|
|
||||||
if len(arg) > 1 {
|
if len(arg) > 1 {
|
||||||
for i, v := range msg.Meta[msg.Meta["append"][0]] {
|
for i, v := range msg.Meta[msg.Meta["append"][0]] {
|
||||||
if v == arg[1] {
|
if v == arg[1] {
|
||||||
@ -187,8 +198,10 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
msg.Cmd("query", "show tables")
|
msg.Cmd("query", "show tables")
|
||||||
for _, v := range msg.Meta[msg.Meta["append"][0]] {
|
mdb.table = []string{}
|
||||||
m.Echo("%s\n", v)
|
for i, v := range msg.Meta[msg.Meta["append"][0]] {
|
||||||
|
mdb.table = append(mdb.table, v)
|
||||||
|
m.Echo("%d: %s\n", i, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// }}}
|
// }}}
|
||||||
@ -208,9 +221,9 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
|
|||||||
if mdb.list == nil {
|
if mdb.list == nil {
|
||||||
mdb.list = make(map[string][]string)
|
mdb.list = make(map[string][]string)
|
||||||
}
|
}
|
||||||
m.Capi("count", 1)
|
|
||||||
mdb.list[m.Cap("count")] = arg[1:]
|
mdb.list[m.Cap("count")] = arg[1:]
|
||||||
mdb.list_key = append(mdb.list_key, m.Cap("count"))
|
mdb.list_key = append(mdb.list_key, m.Cap("count"))
|
||||||
|
m.Capi("count", 1)
|
||||||
case "set":
|
case "set":
|
||||||
mdb.list[arg[1]] = arg[2:]
|
mdb.list[arg[1]] = arg[2:]
|
||||||
default:
|
default:
|
||||||
|
@ -35,7 +35,7 @@ type WEB struct {
|
|||||||
client *http.Client
|
client *http.Client
|
||||||
cookie map[string]*http.Cookie
|
cookie map[string]*http.Cookie
|
||||||
|
|
||||||
list map[string]string
|
list map[string][]string
|
||||||
list_key []string
|
list_key []string
|
||||||
|
|
||||||
*ctx.Message
|
*ctx.Message
|
||||||
@ -230,7 +230,7 @@ func (web *WEB) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
web.list = map[string]string{}
|
web.list = map[string][]string{}
|
||||||
|
|
||||||
return web
|
return web
|
||||||
}
|
}
|
||||||
@ -396,7 +396,9 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
|
|||||||
}
|
}
|
||||||
// }}}
|
// }}}
|
||||||
}},
|
}},
|
||||||
"get": &ctx.Command{Name: "get url arg...", Help: "访问URL", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
"get": &ctx.Command{Name: "get [method GET|POST] [file filename] arg...", Help: "访问URL",
|
||||||
|
Formats: map[string]int{"method": 1, "file": 1},
|
||||||
|
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
web, ok := m.Target().Server.(*WEB) // {{{
|
web, ok := m.Target().Server.(*WEB) // {{{
|
||||||
m.Assert(ok)
|
m.Assert(ok)
|
||||||
|
|
||||||
@ -407,14 +409,38 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
|
|||||||
web.client = &http.Client{}
|
web.client = &http.Client{}
|
||||||
}
|
}
|
||||||
|
|
||||||
req, e := http.NewRequest("GET", uri, nil)
|
method := "GET"
|
||||||
|
if m.Options("method") {
|
||||||
|
method = m.Option("method")
|
||||||
|
}
|
||||||
|
|
||||||
|
var body io.Reader
|
||||||
|
index := strings.Index(uri, "?")
|
||||||
|
switch method {
|
||||||
|
case "POST":
|
||||||
|
if index > 0 {
|
||||||
|
body = strings.NewReader(uri[index+1:])
|
||||||
|
uri = uri[:index]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
req, e := http.NewRequest(method, uri, body)
|
||||||
m.Assert(e)
|
m.Assert(e)
|
||||||
|
|
||||||
|
switch method {
|
||||||
|
case "POST":
|
||||||
|
if index > 0 {
|
||||||
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for _, v := range web.cookie {
|
for _, v := range web.cookie {
|
||||||
req.AddCookie(v)
|
req.AddCookie(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
res, e := web.client.Do(req)
|
res, e := web.client.Do(req)
|
||||||
m.Assert(e)
|
m.Assert(e)
|
||||||
|
|
||||||
if web.cookie == nil {
|
if web.cookie == nil {
|
||||||
web.cookie = make(map[string]*http.Cookie)
|
web.cookie = make(map[string]*http.Cookie)
|
||||||
}
|
}
|
||||||
@ -467,15 +493,29 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
|
|||||||
default:
|
default:
|
||||||
switch arg[0] {
|
switch arg[0] {
|
||||||
case "add":
|
case "add":
|
||||||
m.Capi("count", 1)
|
web.list[m.Cap("count")] = arg[1:]
|
||||||
web.list[m.Cap("count")] = arg[1]
|
|
||||||
web.list_key = append(web.list_key, m.Cap("count"))
|
web.list_key = append(web.list_key, m.Cap("count"))
|
||||||
|
m.Capi("count", 1)
|
||||||
case "del":
|
case "del":
|
||||||
delete(web.list, arg[1])
|
delete(web.list, arg[1])
|
||||||
case "set":
|
case "set":
|
||||||
web.list[arg[1]] = arg[2]
|
web.list[arg[1]] = arg[2:]
|
||||||
default:
|
default:
|
||||||
msg := m.Spawn(m.Target()).Cmd("get", web.list[arg[0]], arg[1:])
|
list := []string{}
|
||||||
|
j := 1
|
||||||
|
for _, v := range web.list[arg[0]] {
|
||||||
|
if v == "_" && j < len(arg) {
|
||||||
|
list = append(list, arg[j])
|
||||||
|
j++
|
||||||
|
} else {
|
||||||
|
list = append(list, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for ; j < len(arg); j++ {
|
||||||
|
list = append(list, arg[j])
|
||||||
|
}
|
||||||
|
|
||||||
|
msg := m.Spawn(m.Target()).Cmd("get", list)
|
||||||
m.Copy(msg, "result")
|
m.Copy(msg, "result")
|
||||||
}
|
}
|
||||||
} // }}}
|
} // }}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user