diff --git a/src/context/web/web.go b/src/context/web/web.go index 12e31deb..5ac47646 100644 --- a/src/context/web/web.go +++ b/src/context/web/web.go @@ -18,13 +18,14 @@ type WEB struct { *http.ServeMux *http.Server - m *ctx.Message + M *ctx.Message *ctx.Context } type MUX interface { Handle(string, http.Handler) HandleFunc(string, func(http.ResponseWriter, *http.Request)) + Trans(*ctx.Message, string, ...string) } func (web *WEB) Trans(m *ctx.Message, key string, arg ...string) { // {{{ @@ -55,10 +56,10 @@ func (web *WEB) Trans(m *ctx.Message, key string, arg ...string) { // {{{ 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) + web.M.Log("info", "%s: %v %s %s", web.Name, r.RemoteAddr, r.Method, r.URL) defer log.Println() - if web.m.Conf("logheaders") == "yes" { + if web.M.Conf("logheaders") == "yes" { for k, v := range r.Header { log.Println(k+":", v[0]) } @@ -79,7 +80,7 @@ func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{ web.ServeMux.ServeHTTP(w, r) if web.Master { - if web.m.Conf("logheaders") == "yes" { + if web.M.Conf("logheaders") == "yes" { for k, v := range w.Header() { log.Println(k+":", v[0]) } @@ -91,15 +92,16 @@ func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{ 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["directory"] = &ctx.Config{Name: "directory", Value: "usr", 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() + mux := m.Target.Server.(MUX) for k, _ := range web.Commands { if k[0] == '/' { - web.Trans(m.Spawn(web.Context), k) + mux.Trans(m, k) } } @@ -108,18 +110,20 @@ func (web *WEB) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{ // }}} func (web *WEB) Start(m *ctx.Message, arg ...string) bool { // {{{ - web.m = m + 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 s, ok := m.Target.Context.Server.(MUX); ok { + if h, ok := m.Target.Server.(http.Handler); ok { + m.Log("info", "%s: route %s -> %s", web.Context.Context.Name, m.Conf("route"), web.Name) + s.Handle(m.Conf("route"), http.StripPrefix(path.Dir(m.Conf("route")), h)) + } } if m.Conf("directory") != "" { - log.Println(web.Name, "directory:", m.Conf("directory")) + m.Log("info", "%s: directory [%s]", web.Name, m.Conf("directory")) web.Handle("/", http.FileServer(http.Dir(m.Conf("directory")))) } @@ -140,13 +144,14 @@ func (web *WEB) Start(m *ctx.Message, arg ...string) bool { // {{{ } 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")) + + m.Log("info", "%s: protocol [%s]", web.Name, m.Conf("protocol")) + m.Log("info", "%s: address [%s]", web.Name, 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")) + m.Log("info", "%s: cert [%s]", web.Name, m.Conf("cert")) + m.Log("info", "%s: key [%s]", web.Name, m.Conf("key")) web.Server.ListenAndServeTLS(m.Conf("cert"), m.Conf("key")) } else { web.Server.ListenAndServe() @@ -159,7 +164,6 @@ func (web *WEB) Start(m *ctx.Message, arg ...string) bool { // {{{ 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 @@ -208,14 +212,10 @@ var Index = &ctx.Context{Name: "web", Help: "网页服务", // }}} }}, "/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.Echo("hello\n") m.Add("append", "hi", "hello") m.Add("append", "hi", "hello") - log.Println(m.Meta) - return "hello" + return "hello\n" }}, }, }