From 69f3484ddf2f2e1677cbf439a2bf0eb7fb0197e8 Mon Sep 17 00:00:00 2001 From: shaoying Date: Tue, 21 Nov 2017 08:45:16 +0800 Subject: [PATCH] =?UTF-8?q?mac=20add=20WEB.Trans=20=E5=B0=86HTTP=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E8=BD=AC=E6=8D=A2=E6=88=90Message?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/context/web/web.go | 45 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/context/web/web.go b/src/context/web/web.go index ea703edb..12e31deb 100644 --- a/src/context/web/web.go +++ b/src/context/web/web.go @@ -27,6 +27,31 @@ type MUX interface { 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) { // {{{ if web.Master { log.Println() @@ -39,8 +64,10 @@ func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{ } log.Println() } + } - r.ParseForm() + r.ParseForm() + if web.Master { if len(r.PostForm) > 0 { for k, v := range r.PostForm { 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.ServeMux = http.NewServeMux() + for k, _ := range web.Commands { + if k[0] == '/' { + web.Trans(m.Spawn(web.Context), k) + } + } + return web } @@ -174,6 +207,16 @@ var Index = &ctx.Context{Name: "web", Help: "网页服务", 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" + }}, }, }