1
0
forked from x/ContextOS

vpn add web.Request 添加请求上下文

This commit is contained in:
shaoying 2017-11-08 08:34:41 +08:00
parent 75a8b5e544
commit 8fe0d03eda
4 changed files with 56 additions and 15 deletions

View File

@ -1,3 +1,4 @@
@debug off @debug off
source etc/mp.sh
~web ~web
listen :9393 server start

View File

@ -325,7 +325,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
case "spawn": case "spawn":
m.Target.Spawn(m, arg[1]) m.Target.Spawn(m, arg[1])
case "find": case "find":
cs := m.Target.Find(strings.Split(arg[1], ".")) cs := m.Target.Find(arg[1])
if cs != nil { if cs != nil {
m.Echo("%s: %s\n", cs.Name, cs.Help) m.Echo("%s: %s\n", cs.Name, cs.Help)
if len(arg) == 4 { if len(arg) == 4 {
@ -548,7 +548,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
} }
} else { } else {
if arg[1] == "listen" { if arg[1] == "listen" {
s := c.Root.Find(strings.Split(arg[3], ".")) s := c.Root.Find(arg[3])
m.Message.Spawn(s, arg[2]).Cmd("listen", arg[2]) m.Message.Spawn(s, arg[2]).Cmd("listen", arg[2])
} else { } else {
} }

View File

@ -60,22 +60,26 @@ type Message struct { // {{{
Root *Message Root *Message
} }
func (m *Message) Spawn(c *Context, key string) *Message { // {{{ func (m *Message) Spawn(c *Context, key ...string) *Message { // {{{
msg := &Message{ msg := &Message{
Code: m.Capi("nmessage", 1), Code: m.Capi("nmessage", 1),
Time: time.Now(), Time: time.Now(),
Name: key,
Index: 0,
Target: c, Target: c,
Message: m, Message: m,
Root: Pulse, Root: Pulse,
} }
msg.Context = m.Target msg.Context = m.Target
if len(key) == 0 {
return msg
}
if msg.Session == nil { if msg.Session == nil {
msg.Session = make(map[string]*Message) msg.Session = make(map[string]*Message)
} }
msg.Session[key] = msg msg.Session[key[0]] = msg
msg.Name = key[0]
if m.Messages == nil { if m.Messages == nil {
m.Messages = make([]*Message, 0, 10) m.Messages = make([]*Message, 0, 10)
@ -183,6 +187,7 @@ func (m *Message) Cmd(arg ...string) string { // {{{
m.Meta["detail"] = arg m.Meta["detail"] = arg
} }
log.Println(m.Meta)
return m.Target.Cmd(m, m.Meta["detail"][0], m.Meta["detail"][1:]...) return m.Target.Cmd(m, m.Meta["detail"][0], m.Meta["detail"][1:]...)
} }
@ -260,7 +265,7 @@ func (c *Context) Check(e error) bool { // {{{
} }
// }}} // }}}
func (c *Context) Safe(m *Message, hand ...func(c *Context, m *Message)) (ok bool) { // {{{ func (c *Context) Safe(m *Message, hand ...func(c *Context, m *Message)) *Context { // {{{
defer func() { defer func() {
if e := recover(); e != nil { if e := recover(); e != nil {
log.Println(c.Name, "error:", e) log.Println(c.Name, "error:", e)
@ -274,10 +279,13 @@ func (c *Context) Safe(m *Message, hand ...func(c *Context, m *Message)) (ok boo
return return
} }
if len(hand) > 0 { if len(hand) > 1 {
c.Safe(m, hand[1:]...) c.Safe(m, hand[1:]...)
} else {
panic(e)
} }
} }
Pulse.Wait <- true Pulse.Wait <- true
}() }()
@ -285,7 +293,7 @@ func (c *Context) Safe(m *Message, hand ...func(c *Context, m *Message)) (ok boo
hand[0](c, m) hand[0](c, m)
} }
return true return c
} }
// }}} // }}}
@ -443,9 +451,10 @@ func (c *Context) Travel(hand func(s *Context) bool) { // {{{
} }
// }}} // }}}
func (c *Context) Find(name []string) (s *Context) { // {{{ func (c *Context) Find(name string) (s *Context) { // {{{
ns := strings.Split(name, ".")
cs := c.Contexts cs := c.Contexts
for _, v := range name { for _, v := range ns {
if x, ok := cs[v]; ok { if x, ok := cs[v]; ok {
s = x s = x
cs = x.Contexts cs = x.Contexts
@ -643,6 +652,20 @@ func (c *Context) Del(arg ...string) { // {{{
// }}} // }}}
func (c *Context) Create(s *Context) *Message { // {{{
msg := &Message{
Time: time.Now(),
Target: c,
Root: Pulse,
}
msg.Context = c
return msg
}
// }}}
func (c *Context) Cmd(m *Message, key string, arg ...string) string { // {{{ func (c *Context) Cmd(m *Message, key string, arg ...string) string { // {{{
if m.Meta == nil { if m.Meta == nil {
m.Meta = make(map[string][]string) m.Meta = make(map[string][]string)

View File

@ -9,9 +9,18 @@ import ( // {{{
// }}} // }}}
type Request struct {
R *http.Request
W http.ResponseWriter
X interface{}
*WEB
}
type WEB struct { type WEB struct {
Run bool Run bool
*Request
*http.ServeMux *http.ServeMux
*http.Server *http.Server
@ -20,7 +29,15 @@ type WEB struct {
func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{ func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{
log.Println() log.Println()
log.Println(web.Name, r.RemoteAddr, r.Method, r.URL.Path) log.Println(web.Name, r.RemoteAddr, r.Method, r.URL)
defer log.Println()
r.ParseForm()
for k, v := range r.PostForm {
log.Printf("%s: %s", k, v[0])
}
log.Println()
web.ServeMux.ServeHTTP(w, r) web.ServeMux.ServeHTTP(w, r)
} }
@ -92,8 +109,8 @@ var Index = &ctx.Context{Name: "web", Help: "网页服务",
}, },
Configs: map[string]*ctx.Config{ Configs: map[string]*ctx.Config{
"directory": &ctx.Config{Name: "directory", Value: "./", Help: "服务目录"}, "directory": &ctx.Config{Name: "directory", Value: "./", Help: "服务目录"},
"protocol": &ctx.Config{Name: "protocol", Value: "http", Help: "服务协议"}, "protocol": &ctx.Config{Name: "protocol", Value: "https", Help: "服务协议"},
"address": &ctx.Config{Name: "address", Value: ":9393", Help: "监听地址"}, "address": &ctx.Config{Name: "address", Value: ":443", Help: "监听地址"},
"route": &ctx.Config{Name: "route", Value: "/", Help: "请求路径"}, "route": &ctx.Config{Name: "route", Value: "/", Help: "请求路径"},
"default": &ctx.Config{Name: "default", Value: "hello web world", Help: "默认响应体"}, "default": &ctx.Config{Name: "default", Value: "hello web world", Help: "默认响应体"},
}, },