From 44e9496f6657d276ba3471d59a099696d8321953 Mon Sep 17 00:00:00 2001 From: shaoying Date: Mon, 6 Nov 2017 21:16:13 +0800 Subject: [PATCH] =?UTF-8?q?mac=20mod=20context/web=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E8=B7=AF=E5=BE=84=E5=88=86=E5=8F=91=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- etc/init.sh | 7 ++-- src/context/ctx.go | 2 +- src/context/web/web.go | 79 +++++++++++++++++++++++++++--------------- src/example/bench.go | 1 + 4 files changed, 58 insertions(+), 31 deletions(-) diff --git a/etc/init.sh b/etc/init.sh index be894db1..4921d557 100644 --- a/etc/init.sh +++ b/etc/init.sh @@ -5,5 +5,8 @@ alias $ cache alias & server alias * message -~web -listen :9393 etc +@debug on + +~mp +listen :9090 ./ + diff --git a/src/context/ctx.go b/src/context/ctx.go index e2801892..b4133999 100644 --- a/src/context/ctx.go +++ b/src/context/ctx.go @@ -235,8 +235,8 @@ func (c *Context) Check(e error) bool { // {{{ func (c *Context) Safe(m *Message, hand ...func(c *Context, m *Message)) (ok bool) { // {{{ defer func() { if e := recover(); e != nil { + log.Println(c.Name, "error:", e) if c.Conf("debug") == "on" { - log.Println(c.Name, "error:", e) if e != io.EOF { debug.PrintStack() } diff --git a/src/context/web/web.go b/src/context/web/web.go index 25de8861..becca39f 100644 --- a/src/context/web/web.go +++ b/src/context/web/web.go @@ -2,46 +2,66 @@ package web // {{{ // }}} import ( // {{{ "context" + "fmt" "log" "net/http" + "path" ) // }}} type WEB struct { + hands map[string]bool + Mux *http.ServeMux *ctx.Context } +func (web *WEB) Handle(p string, h http.Handler) { // {{{ + if web.hands == nil { + web.hands = make(map[string]bool) + } + + if _, ok := web.hands[p]; ok { + panic(fmt.Sprintln(web.Name, "handle exits", p)) + } + + web.Mux.Handle(path.Clean(p)+"/", http.StripPrefix(path.Clean(p), h)) + log.Println(web.Name, "mux:", path.Clean(p)+"/") +} + +// }}} +func (web *WEB) HandleFunc(p string, h func(http.ResponseWriter, *http.Request)) { // {{{ + if web.hands == nil { + web.hands = make(map[string]bool) + } + + if _, ok := web.hands[p]; ok { + panic(fmt.Sprintln(web.Name, "handle exits", p)) + } + + web.Mux.HandleFunc(p, h) + log.Println(web.Name, "mux:", p) +} + +// }}} func (web *WEB) Begin(m *ctx.Message) ctx.Server { // {{{ return web } // }}} func (web *WEB) Start(m *ctx.Message) bool { // {{{ - mux := http.NewServeMux() - mux.Handle("/", http.FileServer(http.Dir(web.Conf("directory")))) - for _, v := range m.Meta["detail"][2:] { - if h, ok := m.Data[v].(func(http.ResponseWriter, *http.Request)); ok { - mux.HandleFunc(v, h) - } - } - - s := &http.Server{Addr: web.Conf("address"), Handler: mux} - s.ListenAndServe() + web.Mux.Handle("/", http.FileServer(http.Dir(web.Conf("directory")))) + s := &http.Server{Addr: web.Conf("address"), Handler: web.Mux} + log.Println(web.Name, "listen:", web.Conf("address")) log.Println(s.ListenAndServe()) return true } // }}} func (web *WEB) Spawn(c *ctx.Context, m *ctx.Message, arg ...string) ctx.Server { // {{{ - c.Caches = map[string]*ctx.Cache{ - "status": &ctx.Cache{Name: "status", Value: "stop", Help: "服务状态"}, - } - c.Configs = map[string]*ctx.Config{ - "address": &ctx.Config{Name: "listen", Value: arg[0], Help: "监听地址"}, - "directory": &ctx.Config{Name: "directory", Value: arg[1], Help: "服务目录"}, - } + c.Caches = map[string]*ctx.Cache{} + c.Configs = map[string]*ctx.Config{} c.Commands = map[string]*ctx.Command{} s := new(WEB) @@ -56,20 +76,23 @@ func (web *WEB) Exit(m *ctx.Message, arg ...string) bool { // {{{ // }}} -var Index = &ctx.Context{Name: "web", Help: "网页服务", // {{{ - Caches: map[string]*ctx.Cache{}, - Configs: map[string]*ctx.Config{}, +var Index = &ctx.Context{Name: "web", Help: "网页服务", + Caches: map[string]*ctx.Cache{ + "status": &ctx.Cache{Name: "status", Value: "stop", Help: "服务状态"}, + }, + Configs: map[string]*ctx.Config{ + "address": &ctx.Config{Name: "listen", Value: ":9090", Help: "监听地址"}, + "directory": &ctx.Config{Name: "directory", Value: "./", Help: "服务目录"}, + "who": &ctx.Config{Name: "who", Value: "shylinux", Help: "服务目录"}, + }, Commands: map[string]*ctx.Command{ "listen": &ctx.Command{"listen address directory", "设置监听地址和目录", func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string { switch len(arg) { // {{{ case 0: default: - m.Add("detail", "/hi") - m.Put("/hi", func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("hello context world!")) - }) - - m.Start(m.Meta["detail"][1:]...) + c.Conf("address", arg[0]) + c.Conf("directory", arg[1]) + go c.Start(m) } return "" // }}} @@ -77,12 +100,12 @@ var Index = &ctx.Context{Name: "web", Help: "网页服务", // {{{ }, } -// }}} - func init() { // {{{ web := &WEB{} web.Context = Index ctx.Index.Register(Index, web) + + web.Mux = http.NewServeMux() } // }}} diff --git a/src/example/bench.go b/src/example/bench.go index 77cd58b7..1616df89 100644 --- a/src/example/bench.go +++ b/src/example/bench.go @@ -3,6 +3,7 @@ package main import ( "context" _ "context/cli" + _ "context/web/mp" ) func main() {