forked from x/ContextOS
vpn add web.Request 添加请求上下文
This commit is contained in:
parent
75a8b5e544
commit
8fe0d03eda
@ -1,3 +1,4 @@
|
||||
@debug off
|
||||
source etc/mp.sh
|
||||
~web
|
||||
listen :9393
|
||||
server start
|
||||
|
@ -325,7 +325,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
|
||||
case "spawn":
|
||||
m.Target.Spawn(m, arg[1])
|
||||
case "find":
|
||||
cs := m.Target.Find(strings.Split(arg[1], "."))
|
||||
cs := m.Target.Find(arg[1])
|
||||
if cs != nil {
|
||||
m.Echo("%s: %s\n", cs.Name, cs.Help)
|
||||
if len(arg) == 4 {
|
||||
@ -548,7 +548,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
|
||||
}
|
||||
} else {
|
||||
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])
|
||||
} else {
|
||||
}
|
||||
|
@ -60,22 +60,26 @@ type Message struct { // {{{
|
||||
Root *Message
|
||||
}
|
||||
|
||||
func (m *Message) Spawn(c *Context, key string) *Message { // {{{
|
||||
func (m *Message) Spawn(c *Context, key ...string) *Message { // {{{
|
||||
|
||||
msg := &Message{
|
||||
Code: m.Capi("nmessage", 1),
|
||||
Time: time.Now(),
|
||||
Name: key,
|
||||
Index: 0,
|
||||
Target: c,
|
||||
Message: m,
|
||||
Root: Pulse,
|
||||
}
|
||||
|
||||
msg.Context = m.Target
|
||||
if len(key) == 0 {
|
||||
return msg
|
||||
}
|
||||
|
||||
if msg.Session == nil {
|
||||
msg.Session = make(map[string]*Message)
|
||||
}
|
||||
msg.Session[key] = msg
|
||||
msg.Session[key[0]] = msg
|
||||
msg.Name = key[0]
|
||||
|
||||
if m.Messages == nil {
|
||||
m.Messages = make([]*Message, 0, 10)
|
||||
@ -183,6 +187,7 @@ func (m *Message) Cmd(arg ...string) string { // {{{
|
||||
m.Meta["detail"] = arg
|
||||
}
|
||||
|
||||
log.Println(m.Meta)
|
||||
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() {
|
||||
if e := recover(); e != nil {
|
||||
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
|
||||
}
|
||||
|
||||
if len(hand) > 0 {
|
||||
if len(hand) > 1 {
|
||||
c.Safe(m, hand[1:]...)
|
||||
} else {
|
||||
panic(e)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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
|
||||
for _, v := range name {
|
||||
for _, v := range ns {
|
||||
if x, ok := cs[v]; ok {
|
||||
s = x
|
||||
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 { // {{{
|
||||
if m.Meta == nil {
|
||||
m.Meta = make(map[string][]string)
|
||||
|
@ -9,9 +9,18 @@ import ( // {{{
|
||||
|
||||
// }}}
|
||||
|
||||
type Request struct {
|
||||
R *http.Request
|
||||
W http.ResponseWriter
|
||||
X interface{}
|
||||
*WEB
|
||||
}
|
||||
|
||||
type WEB struct {
|
||||
Run bool
|
||||
|
||||
*Request
|
||||
|
||||
*http.ServeMux
|
||||
*http.Server
|
||||
|
||||
@ -20,7 +29,15 @@ type WEB struct {
|
||||
|
||||
func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
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)
|
||||
}
|
||||
|
||||
@ -92,8 +109,8 @@ var Index = &ctx.Context{Name: "web", Help: "网页服务",
|
||||
},
|
||||
Configs: map[string]*ctx.Config{
|
||||
"directory": &ctx.Config{Name: "directory", Value: "./", Help: "服务目录"},
|
||||
"protocol": &ctx.Config{Name: "protocol", Value: "http", Help: "服务协议"},
|
||||
"address": &ctx.Config{Name: "address", Value: ":9393", Help: "监听地址"},
|
||||
"protocol": &ctx.Config{Name: "protocol", Value: "https", Help: "服务协议"},
|
||||
"address": &ctx.Config{Name: "address", Value: ":443", Help: "监听地址"},
|
||||
"route": &ctx.Config{Name: "route", Value: "/", Help: "请求路径"},
|
||||
"default": &ctx.Config{Name: "default", Value: "hello web world", Help: "默认响应体"},
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user