diff --git a/src/context/cli/cli.go b/src/context/cli/cli.go index dc93efeb..05c0998d 100644 --- a/src/context/cli/cli.go +++ b/src/context/cli/cli.go @@ -77,7 +77,11 @@ func (cli *CLI) parse() bool { // {{{ } if len(line) == 1 { - return true + if len(cli.ins) == 1 { + line = cli.history[len(cli.history)-1]["cli"] + } else { + return true + } } } else { line = cli.next @@ -89,6 +93,7 @@ func (cli *CLI) parse() bool { // {{{ } } +back: line = strings.TrimSpace(line) if line[0] == '#' { return true @@ -118,6 +123,7 @@ func (cli *CLI) parse() bool { // {{{ for i := 0; i < len(ls); i++ { ls[i] = strings.TrimSpace(ls[i]) + if ls[i][0] == '#' { break } @@ -126,6 +132,13 @@ func (cli *CLI) parse() bool { // {{{ } } + ls = msg.Meta["detail"] + if n, e := strconv.Atoi(ls[0]); e == nil && 0 <= n && n < len(cli.history) && ls[0] != cli.history[n]["cli"] { + line = cli.history[n]["cli"] + msg.Meta["detail"] = nil + goto back + } + msg.Post(cli.Context) for _, v := range msg.Meta["result"] { @@ -426,6 +439,9 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端", cli.alias[arg[0]] = arg[1] m.Echo("%s: %s\n", arg[0], cli.alias[arg[0]]) } + default: + cli.alias[arg[0]] = strings.Join(arg[1:], " ") + m.Echo("%s: %s\n", arg[0], cli.alias[arg[0]]) } return "" // }}} @@ -441,7 +457,12 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端", n, e := strconv.Atoi(arg[0]) if e == nil && 0 <= n && n < len(cli.history) { log.Println("shy log why:", cli.history[n]["cli"]) - return cli.history[n]["cli"] + cli.next = cli.history[n]["cli"] + } + default: + n, e := strconv.Atoi(arg[0]) + if e == nil && 0 <= n && n < len(cli.history) { + cli.history[n]["cli"] = strings.Join(arg[1:], " ") } } return "" @@ -482,6 +503,8 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端", } case 2: switch arg[0] { + case "void": + m.Target.Conf(arg[1], "") case "delete": if _, ok := m.Target.Configs[arg[1]]; ok { delete(m.Target.Configs, arg[1]) @@ -569,7 +592,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端", Messages: make(chan *ctx.Message, 10), } -func init() { // {{{ +func init() { cli := &CLI{alias: map[string]string{ "~": "context", "!": "history", @@ -581,5 +604,3 @@ func init() { // {{{ cli.Context = Index ctx.Index.Register(Index, cli) } - -// }}} diff --git a/src/context/ctx.go b/src/context/ctx.go index 61e5359e..33b9d298 100644 --- a/src/context/ctx.go +++ b/src/context/ctx.go @@ -354,7 +354,11 @@ func (c *Context) Start(m *Message) bool { // {{{ func (c *Context) Spawn(m *Message, arg ...string) *Context { // {{{ s := &Context{Name: arg[0], Help: c.Help} m.Target = s - c.Register(s, c.Server.Spawn(s, m, arg...)).Begin(m) + if c.Server != nil { + c.Register(s, c.Server.Spawn(s, m, arg...)).Begin(m) + } else { + c.Register(s, nil).Begin(m) + } return s } @@ -652,7 +656,7 @@ func (c *Context) Del(arg ...string) { // {{{ // }}} -func (c *Context) Create(s *Context) *Message { // {{{ +func (c *Context) Storm(s *Context) *Message { // {{{ msg := &Message{ Time: time.Now(), @@ -716,6 +720,9 @@ func (c *Context) Conf(key string, arg ...string) string { // {{{ if s == c { panic(errors.New(x.Name + "配置项已存在")) } + if c.Configs == nil { + c.Configs = make(map[string]*Config) + } c.Configs[key] = &Config{Name: arg[0], Value: arg[1], Help: arg[2], Hand: x.Hand} return arg[1] default: @@ -752,7 +759,7 @@ func (c *Context) Cap(key string, arg ...string) string { // {{{ return x.Value case 1: if x.Hand != nil { - x.Value = x.Hand(c, x, x.Value) + x.Value = x.Hand(c, x, arg[0]) } else { x.Value = arg[0] } @@ -761,6 +768,9 @@ func (c *Context) Cap(key string, arg ...string) string { // {{{ if s == c { panic(errors.New(key + "缓存项已存在")) } + if c.Caches == nil { + c.Caches = make(map[string]*Cache) + } c.Caches[key] = &Cache{arg[0], arg[1], arg[2], x.Hand} return arg[1] default: diff --git a/src/context/web/web.go b/src/context/web/web.go index 8cce8c5f..be066363 100644 --- a/src/context/web/web.go +++ b/src/context/web/web.go @@ -32,13 +32,28 @@ func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{ 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]) + if web.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() } - log.Println() web.ServeMux.ServeHTTP(w, r) + + if web.Conf("logheaders") == "yes" { + for k, v := range w.Header() { + log.Println(k+":", v[0]) + } + } } // }}} @@ -108,11 +123,12 @@ var Index = &ctx.Context{Name: "web", Help: "网页服务", "status": &ctx.Cache{Name: "status", Value: "stop", Help: "服务状态"}, }, Configs: map[string]*ctx.Config{ - "directory": &ctx.Config{Name: "directory", Value: "./", 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: "默认响应体"}, + "logheaders": &ctx.Config{Name: "logheaders", Value: "yes", Help: "日志输出请求头"}, + "directory": &ctx.Config{Name: "directory", Value: "./", 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: "默认响应体"}, }, Commands: map[string]*ctx.Command{ "listen": &ctx.Command{Name: "listen [route [address protocol [directory]]]", Help: "开启网页服务", Hand: func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string { diff --git a/src/toolkit/kit.go b/src/toolkit/kit.go index 6d38b17e..e2a5f289 100644 --- a/src/toolkit/kit.go +++ b/src/toolkit/kit.go @@ -24,6 +24,22 @@ func FmtSize(size int64) string { return fmt.Sprintf("%dB", size) } +func FmtNano(nano int64) string { + if nano > 1000000000 { + return fmt.Sprintf("%d.%ds", nano/1000000000, nano/100000000%100) + } + + if nano > 1000000 { + return fmt.Sprintf("%d.%dms", nano/100000, nano/100000%100) + } + + if nano > 1000 { + return fmt.Sprintf("%d.%dus", nano/1000, nano/100%100) + } + + return fmt.Sprintf("%dns", nano) +} + func Check(e error) bool { if e != nil { panic(e)