From 5bd1c9a63bdce0943397366d1f81a1f0f6b5ef26 Mon Sep 17 00:00:00 2001 From: shaoying Date: Tue, 21 Nov 2017 06:08:40 +0800 Subject: [PATCH] =?UTF-8?q?mac=20add=20web=20=E6=B7=BB=E5=8A=A0=E4=BA=86?= =?UTF-8?q?=E7=BD=91=E9=A1=B5=E6=9C=8D=E5=8A=A1=E6=94=AF=E6=8C=81=E5=A4=9A?= =?UTF-8?q?=E4=B8=BB=E6=9C=BA=E5=A4=9A=E8=B7=AF=E7=94=B1=E5=A4=9A=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/context/web/web.go | 184 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 src/context/web/web.go diff --git a/src/context/web/web.go b/src/context/web/web.go new file mode 100644 index 00000000..ea703edb --- /dev/null +++ b/src/context/web/web.go @@ -0,0 +1,184 @@ +package web // {{{ +// }}} +import ( // {{{ + "context" + "html/template" + "log" + "net/http" + "os" + "path" +) + +// }}} + +type WEB struct { + Run bool + Master bool + + *http.ServeMux + *http.Server + + m *ctx.Message + *ctx.Context +} + +type MUX interface { + Handle(string, http.Handler) + HandleFunc(string, func(http.ResponseWriter, *http.Request)) +} + +func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{ + if web.Master { + log.Println() + log.Println(web.Name, r.RemoteAddr, r.Method, r.URL) + defer log.Println() + + if web.m.Conf("logheaders") == "yes" { + for k, v := range r.Header { + log.Println(k+":", v[0]) + } + log.Println() + } + + r.ParseForm() + if len(r.PostForm) > 0 { + for k, v := range r.PostForm { + log.Printf("%s: %s", k, v[0]) + } + log.Println() + } + } + + web.ServeMux.ServeHTTP(w, r) + + if web.Master { + if web.m.Conf("logheaders") == "yes" { + for k, v := range w.Header() { + log.Println(k+":", v[0]) + } + } + } +} + +// }}} + +func (web *WEB) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{ + web.Configs["logheaders"] = &ctx.Config{Name: "logheaders", Value: "yes", Help: "日志输出请求头"} + web.Configs["directory"] = &ctx.Config{Name: "directory", Value: "./", Help: "服务目录"} + web.Configs["protocol"] = &ctx.Config{Name: "protocol", Value: "http", Help: "服务协议"} + web.Configs["address"] = &ctx.Config{Name: "address", Value: ":9393", Help: "监听地址"} + web.Configs["route"] = &ctx.Config{Name: "route", Value: "/" + web.Name + "/", Help: "请求路径"} + + web.ServeMux = http.NewServeMux() + return web +} + +// }}} +func (web *WEB) Start(m *ctx.Message, arg ...string) bool { // {{{ + web.m = m + + if !web.Run { + web.Run = true + + if s, ok := web.Context.Context.Server.(MUX); ok { + log.Println(web.Context.Name, "route:", m.Conf("route"), "->", web.Name) + s.Handle(m.Conf("route"), http.StripPrefix(path.Dir(m.Conf("route")), web)) + } + + if m.Conf("directory") != "" { + log.Println(web.Name, "directory:", m.Conf("directory")) + web.Handle("/", http.FileServer(http.Dir(m.Conf("directory")))) + } + + m.Set("detail", "slaver") + m.Travel(web.Context, func(m *ctx.Message) bool { + if m.Target != web.Context { + m.Target.Start(m) + } + return true + }) + } + if len(arg) > 0 && arg[0] == "slaver" { + return true + } + + if m.Conf("address") == "" { + return true + } + + web.Server = &http.Server{Addr: m.Conf("address"), Handler: web} + log.Println(web.Name, "protocol:", m.Conf("protocol")) + log.Println(web.Name, "address:", m.Conf("address")) + web.Master = true + + if m.Conf("protocol") == "https" { + log.Println(web.Name, "cert:", m.Conf("cert")) + log.Println(web.Name, "key:", m.Conf("key")) + web.Server.ListenAndServeTLS(m.Conf("cert"), m.Conf("key")) + } else { + web.Server.ListenAndServe() + } + + return true +} + +// }}} +func (web *WEB) Spawn(c *ctx.Context, m *ctx.Message, arg ...string) ctx.Server { // {{{ + c.Caches = map[string]*ctx.Cache{} + c.Configs = map[string]*ctx.Config{} + c.Commands = map[string]*ctx.Command{} + + s := new(WEB) + s.Context = c + return s +} + +// }}} +func (web *WEB) Exit(m *ctx.Message, arg ...string) bool { // {{{ + return true +} + +// }}} + +var Index = &ctx.Context{Name: "web", Help: "网页服务", + Caches: map[string]*ctx.Cache{}, + Configs: map[string]*ctx.Config{}, + Commands: map[string]*ctx.Command{ + "listen": &ctx.Command{Name: "listen [address [protocol [directory]]]", Help: "开启网页服务", Hand: func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string { + if len(arg) > 0 { // {{{ + m.Conf("address", arg[0]) + } + if len(arg) > 1 { + m.Conf("protocol", arg[1]) + } + if len(arg) > 2 { + m.Conf("directory", arg[2]) + } + go m.Target.Start(m) + return "" + // }}} + }}, + "content": &ctx.Command{Name: "content route template", Help: "添加响应", Hand: func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string { + mux, ok := m.Target.Server.(MUX) // {{{ + if !ok { + return "" + } + + mux.HandleFunc(arg[0], func(w http.ResponseWriter, r *http.Request) { + if _, e := os.Stat(arg[1]); e == nil { + template.Must(template.ParseGlob(arg[1])).Execute(w, m.Target) + } else { + template.Must(template.New("temp").Parse(arg[1])).Execute(w, m.Target) + } + }) + return "" + // }}} + }}, + }, +} + +func init() { + web := &WEB{} + web.Context = Index + ctx.Index.Register(Index, web) +}