1
0
forked from x/ContextOS

mac add WEB.Trans 将HTTP请求转换成Message

This commit is contained in:
shaoying 2017-11-21 08:45:16 +08:00
parent 5bd1c9a63b
commit 69f3484ddf

View File

@ -27,6 +27,31 @@ type MUX interface {
HandleFunc(string, func(http.ResponseWriter, *http.Request)) HandleFunc(string, func(http.ResponseWriter, *http.Request))
} }
func (web *WEB) Trans(m *ctx.Message, key string, arg ...string) { // {{{
web.HandleFunc(key, func(w http.ResponseWriter, r *http.Request) {
m.Set("detail", key)
for k, v := range r.Form {
m.Add("option", k)
m.Meta[k] = v
}
for _, v := range r.Cookies() {
m.Add("option", v.Name, v.Value)
}
m.Cmd(arg...)
header := w.Header()
for _, k := range m.Meta["append"] {
ce := &http.Cookie{Name: k, Value: m.Get(k)}
header.Add("Set-Cookie", ce.String())
}
for _, v := range m.Meta["result"] {
w.Write([]byte(v))
}
})
}
// }}}
func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{ func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{
if web.Master { if web.Master {
log.Println() log.Println()
@ -39,8 +64,10 @@ func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{
} }
log.Println() log.Println()
} }
}
r.ParseForm() r.ParseForm()
if web.Master {
if len(r.PostForm) > 0 { if len(r.PostForm) > 0 {
for k, v := range r.PostForm { for k, v := range r.PostForm {
log.Printf("%s: %s", k, v[0]) log.Printf("%s: %s", k, v[0])
@ -70,6 +97,12 @@ func (web *WEB) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
web.Configs["route"] = &ctx.Config{Name: "route", Value: "/" + web.Name + "/", Help: "请求路径"} web.Configs["route"] = &ctx.Config{Name: "route", Value: "/" + web.Name + "/", Help: "请求路径"}
web.ServeMux = http.NewServeMux() web.ServeMux = http.NewServeMux()
for k, _ := range web.Commands {
if k[0] == '/' {
web.Trans(m.Spawn(web.Context), k)
}
}
return web return web
} }
@ -174,6 +207,16 @@ var Index = &ctx.Context{Name: "web", Help: "网页服务",
return "" return ""
// }}} // }}}
}}, }},
"/hi": &ctx.Command{Name: "/hi", Help: "添加响应", Hand: func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string {
m.Echo("hello")
m.Echo("hello")
m.Echo("hello")
m.Echo("hello")
m.Add("append", "hi", "hello")
m.Add("append", "hi", "hello")
log.Println(m.Meta)
return "hello"
}},
}, },
} }