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,60 +396,86 @@ 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",
|
||||||
web, ok := m.Target().Server.(*WEB) // {{{
|
Formats: map[string]int{"method": 1, "file": 1},
|
||||||
m.Assert(ok)
|
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
|
web, ok := m.Target().Server.(*WEB) // {{{
|
||||||
|
m.Assert(ok)
|
||||||
|
|
||||||
uri := web.generate(m, arg[0], arg[1:]...)
|
uri := web.generate(m, arg[0], arg[1:]...)
|
||||||
m.Log("info", nil, "GET %s", uri)
|
m.Log("info", nil, "GET %s", uri)
|
||||||
|
|
||||||
if web.client == nil {
|
if web.client == nil {
|
||||||
web.client = &http.Client{}
|
web.client = &http.Client{}
|
||||||
}
|
|
||||||
|
|
||||||
req, e := http.NewRequest("GET", uri, nil)
|
|
||||||
m.Assert(e)
|
|
||||||
for _, v := range web.cookie {
|
|
||||||
req.AddCookie(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
res, e := web.client.Do(req)
|
|
||||||
m.Assert(e)
|
|
||||||
if web.cookie == nil {
|
|
||||||
web.cookie = make(map[string]*http.Cookie)
|
|
||||||
}
|
|
||||||
for _, v := range res.Cookies() {
|
|
||||||
web.cookie[v.Name] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
for k, v := range res.Header {
|
|
||||||
m.Log("info", nil, "%s: %v", k, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
if m.Confs("output") {
|
|
||||||
if _, e := os.Stat(m.Conf("output")); e == nil {
|
|
||||||
name := path.Join(m.Conf("output"), fmt.Sprintf("%d", time.Now().Unix()))
|
|
||||||
f, e := os.Create(name)
|
|
||||||
m.Assert(e)
|
|
||||||
io.Copy(f, res.Body)
|
|
||||||
if m.Confs("editor") {
|
|
||||||
cmd := exec.Command(m.Conf("editor"), name)
|
|
||||||
cmd.Stdin = os.Stdin
|
|
||||||
cmd.Stdout = os.Stdout
|
|
||||||
cmd.Stderr = os.Stderr
|
|
||||||
cmd.Run()
|
|
||||||
} else {
|
|
||||||
m.Echo("write to %s", name)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
buf, e := ioutil.ReadAll(res.Body)
|
method := "GET"
|
||||||
m.Assert(e)
|
if m.Options("method") {
|
||||||
m.Echo(string(buf))
|
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)
|
||||||
|
|
||||||
|
switch method {
|
||||||
|
case "POST":
|
||||||
|
if index > 0 {
|
||||||
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range web.cookie {
|
||||||
|
req.AddCookie(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
res, e := web.client.Do(req)
|
||||||
|
m.Assert(e)
|
||||||
|
|
||||||
|
if web.cookie == nil {
|
||||||
|
web.cookie = make(map[string]*http.Cookie)
|
||||||
|
}
|
||||||
|
for _, v := range res.Cookies() {
|
||||||
|
web.cookie[v.Name] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range res.Header {
|
||||||
|
m.Log("info", nil, "%s: %v", k, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.Confs("output") {
|
||||||
|
if _, e := os.Stat(m.Conf("output")); e == nil {
|
||||||
|
name := path.Join(m.Conf("output"), fmt.Sprintf("%d", time.Now().Unix()))
|
||||||
|
f, e := os.Create(name)
|
||||||
|
m.Assert(e)
|
||||||
|
io.Copy(f, res.Body)
|
||||||
|
if m.Confs("editor") {
|
||||||
|
cmd := exec.Command(m.Conf("editor"), name)
|
||||||
|
cmd.Stdin = os.Stdin
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
cmd.Run()
|
||||||
|
} else {
|
||||||
|
m.Echo("write to %s", name)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
buf, e := ioutil.ReadAll(res.Body)
|
||||||
|
m.Assert(e)
|
||||||
|
m.Echo(string(buf))
|
||||||
|
// }}}
|
||||||
|
}},
|
||||||
"list": &ctx.Command{Name: "list [set|add|del [url]]", Help: "查看、访问、添加url", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
"list": &ctx.Command{Name: "list [set|add|del [url]]", Help: "查看、访问、添加url", 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)
|
||||||
@ -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