From 5cb1f6e5ae1a9edc82277601765534aafd327e19 Mon Sep 17 00:00:00 2001 From: shaoying Date: Tue, 7 Nov 2017 08:51:03 +0800 Subject: [PATCH] =?UTF-8?q?mac=20mod=20context/web=20=E6=94=AF=E6=8C=81htt?= =?UTF-8?q?ps?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- etc/init.sh | 13 ++++--------- src/context/cli/cli.go | 4 +--- src/context/ctx.go | 6 ++++++ src/context/web/web.go | 21 ++++++++++++++++++--- src/example/bench.go | 3 ++- 5 files changed, 31 insertions(+), 16 deletions(-) diff --git a/etc/init.sh b/etc/init.sh index 4921d557..2dbb6bd5 100644 --- a/etc/init.sh +++ b/etc/init.sh @@ -1,12 +1,7 @@ -alias ~ context -alias ! history -alias @ config -alias $ cache -alias & server -alias * message - @debug on -~mp -listen :9090 ./ +~web +source etc/mp.sh +~mdb +& start diff --git a/src/context/cli/cli.go b/src/context/cli/cli.go index e4685969..4df61fe5 100644 --- a/src/context/cli/cli.go +++ b/src/context/cli/cli.go @@ -478,9 +478,7 @@ var Index = &ctx.Context{Name: "cli", Help: "本地控制", // {{{ delete(m.Target.Configs, arg[1]) } default: - if _, ok := m.Target.Configs[arg[0]]; ok { - m.Target.Conf(arg[0], arg[1:]...) - } + m.Target.Conf(arg[0], arg[1]) } case 4: m.Target.Conf(arg[0], arg[1:]...) diff --git a/src/context/ctx.go b/src/context/ctx.go index b4133999..b8a11997 100644 --- a/src/context/ctx.go +++ b/src/context/ctx.go @@ -601,6 +601,12 @@ func (c *Context) Del(arg ...string) { // {{{ // }}} func (c *Context) Cmd(m *Message, key string, arg ...string) string { // {{{ + if m.Meta == nil { + m.Meta = make(map[string][]string) + } + m.Meta["detail"] = []string{key} + m.Meta["detail"] = append(m.Meta["detail"], arg...) + for s := c; s != nil; s = s.Context { if x, ok := s.Commands[key]; ok { log.Printf("%s cmd(%s->%s): %v", c.Name, m.Context.Name, m.Target.Name, m.Meta["detail"]) diff --git a/src/context/web/web.go b/src/context/web/web.go index becca39f..cbb0cff2 100644 --- a/src/context/web/web.go +++ b/src/context/web/web.go @@ -52,9 +52,24 @@ func (web *WEB) Begin(m *ctx.Message) ctx.Server { // {{{ func (web *WEB) Start(m *ctx.Message) bool { // {{{ 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()) + log.Println(web.Name, "https:", web.Conf("https")) + + defer func() { + if e := recover(); e != nil { + log.Println(e) + } + }() + + if web.Conf("https") == "yes" { + log.Println(web.Name, "cert:", web.Conf("cert")) + log.Println(web.Name, "key:", web.Conf("key")) + http.ListenAndServeTLS(web.Conf("address"), web.Conf("cert"), web.Conf("key"), web.Mux) + } else { + http.ListenAndServe(web.Conf("address"), web.Mux) + } + return true } @@ -83,7 +98,7 @@ var Index = &ctx.Context{Name: "web", 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: "服务目录"}, + "https": &ctx.Config{Name: "https", Value: "yes", Help: "开启安全连接"}, }, Commands: map[string]*ctx.Command{ "listen": &ctx.Command{"listen address directory", "设置监听地址和目录", func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string { diff --git a/src/example/bench.go b/src/example/bench.go index 1616df89..25e0fd3f 100644 --- a/src/example/bench.go +++ b/src/example/bench.go @@ -3,7 +3,8 @@ package main import ( "context" _ "context/cli" - _ "context/web/mp" + _ "context/mdb" + // _ "context/web/mp" ) func main() {