diff --git a/etc/go.snippets b/etc/go.snippets index fac4805b..f5e1a17b 100644 --- a/etc/go.snippets +++ b/etc/go.snippets @@ -23,31 +23,36 @@ snippet c import ( "context" + _ "context/cli" ) type `toupper(substitute(expand("%:t"), ".go", "", ""))` struct { *ctx.Context - *ctx.CTX } - func (`Filename()` *`toupper(substitute(expand("%:t"), ".go", "", ""))`) Begin() bool { + func (`Filename()` *`toupper(substitute(expand("%:t"), ".go", "", ""))`) Begin(m *ctx.Message) ctx.Server { + return nil + } + + func (`Filename()` *`toupper(substitute(expand("%:t"), ".go", "", ""))`) Start(m *ctx.Message) bool { return true } - func (`Filename()` *`toupper(substitute(expand("%:t"), ".go", "", ""))`) Start() bool { - return true - } - - func (`Filename()` *`toupper(substitute(expand("%:t"), ".go", "", ""))`) Spawn(c *ctx.Context, arg ...string) ctx.Server { + func (`Filename()` *`toupper(substitute(expand("%:t"), ".go", "", ""))`) 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(`toupper(substitute(expand("%:t"), ".go", "", ""))`) - s.CTX = ctx.Ctx s.Context = c return s } + func (`Filename()` *`toupper(substitute(expand("%:t"), ".go", "", ""))`) Exit(m *ctx.Message, arg ...string) bool { + return true + } + + var Index = &ctx.Context{Name: "`Filename()`", Help: "${1}", Caches: map[string]*ctx.Cache{}, Configs: map[string]*ctx.Config{}, @@ -56,7 +61,6 @@ snippet c func init() { `Filename()` := &`toupper(substitute(expand("%:t"), ".go", "", ""))`{} - `Filename()`.CTX = ctx.Ctx `Filename()`.Context = Index ctx.Index.Register(Index, `Filename()`) } diff --git a/etc/init.sh b/etc/init.sh index 4d429893..be894db1 100644 --- a/etc/init.sh +++ b/etc/init.sh @@ -4,3 +4,6 @@ alias @ config alias $ cache alias & server alias * message + +~web +listen :9393 etc diff --git a/src/context/cli/cli.go b/src/context/cli/cli.go index d623d9ad..e4685969 100644 --- a/src/context/cli/cli.go +++ b/src/context/cli/cli.go @@ -1,10 +1,13 @@ -package cli - +package cli // {{{ +// }}} import ( // {{{ "bufio" "context" + _ "context/tcp" + _ "context/web" "fmt" "io" + "log" "os" "strconv" "strings" @@ -143,7 +146,6 @@ func (cli *CLI) echo(str string, arg ...interface{}) { // {{{ func (cli *CLI) Begin(m *ctx.Message) ctx.Server { // {{{ cli.history = make([]map[string]string, 0, 100) - cli.alias = make(map[string]string, 10) cli.target = cli.Context return cli.Server } @@ -223,14 +225,17 @@ func (cli *CLI) Exit(m *ctx.Message, arg ...string) bool { // {{{ // }}} -var Index = &ctx.Context{Name: "cli", Help: "本地控制", +var Index = &ctx.Context{Name: "cli", Help: "本地控制", // {{{ Caches: map[string]*ctx.Cache{ - "nterm": &ctx.Cache{Name: "nterm", Value: "0", Help: "终端数量"}, + "nterm": &ctx.Cache{Name: "nterm", Value: "0", Help: "终端数量"}, + "status": &ctx.Cache{Name: "status", Value: "stop", Help: "服务状态"}, "nhistory": &ctx.Cache{Name: "nhistory", Value: "0", Help: "终端数量", Hand: func(c *ctx.Context, x *ctx.Cache, arg ...string) string { - if cli, ok := c.Server.(*CLI); ok { + if cli, ok := c.Server.(*CLI); ok { // {{{ return fmt.Sprintf("%d", len(cli.history)) } - return "" + + return x.Value + // }}} }}, }, Configs: map[string]*ctx.Config{ @@ -260,7 +265,8 @@ var Index = &ctx.Context{Name: "cli", Help: "本地控制", cli, ok := c.Server.(*CLI) // {{{ switch len(arg) { case 0: - cs := []*ctx.Context{m.Target} + // cs := []*ctx.Context{m.Target} + cs := []*ctx.Context{m.Target.Root} for i := 0; i < len(cs); i++ { if len(cs[i].Contexts) > 0 { m.Echo("%s: ", cs[i].Name) @@ -360,6 +366,16 @@ var Index = &ctx.Context{Name: "cli", Help: "本地控制", s := m.Target // {{{ switch len(arg) { case 0: + cs := []*ctx.Context{m.Target.Root} + for i := 0; i < len(cs); i++ { + if x, ok := cs[i].Caches["status"]; ok { + m.Echo("%s(%s): %s\n", cs[i].Name, x.Value, cs[i].Help) + } + + for _, v := range cs[i].Contexts { + cs = append(cs, v) + } + } return "server start" case 1: switch arg[0] { @@ -415,6 +431,7 @@ var Index = &ctx.Context{Name: "cli", Help: "本地控制", case 1: 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"] } } @@ -543,8 +560,18 @@ var Index = &ctx.Context{Name: "cli", Help: "本地控制", Messages: make(chan *ctx.Message, 10), } -func init() { - cli := &CLI{} +// }}} +func init() { // {{{ + cli := &CLI{alias: map[string]string{ + "~": "context", + "!": "history", + "@": "config", + "$": "cache", + "&": "server", + "*": "message", + }} cli.Context = Index ctx.Index.Register(Index, cli) } + +// }}} diff --git a/src/context/ctx.go b/src/context/ctx.go index 4c7dedb6..e2801892 100644 --- a/src/context/ctx.go +++ b/src/context/ctx.go @@ -1,5 +1,5 @@ -package ctx - +package ctx // {{{ +// }}} import ( // {{{ "errors" "fmt" @@ -7,6 +7,7 @@ import ( // {{{ "log" "os" "os/exec" + "path" "regexp" "runtime/debug" "strconv" @@ -667,14 +668,15 @@ func (c *Context) Cap(key string, arg ...string) string { // {{{ switch len(arg) { case 0: if x.Hand != nil { - return x.Hand(c, x) + x.Value = x.Hand(c, x) } return x.Value case 1: if x.Hand != nil { - return x.Hand(c, x, x.Value) + x.Value = x.Hand(c, x, x.Value) + } else { + x.Value = arg[0] } - x.Value = arg[0] return x.Value case 3: if s == c { @@ -708,7 +710,8 @@ func (c *Context) Capi(key string, arg ...int) int { // {{{ // }}} -var Index = &Context{Name: "ctx", Help: "根上下文", +var Pulse = &Message{Code: 0, Time: time.Now(), Index: 0, Name: "root"} +var Index = &Context{Name: "ctx", Help: "根上下文", // {{{ Caches: map[string]*Cache{ "status": &Cache{Name: "status", Value: "stop", Help: "服务状态"}, "nserver": &Cache{Name: "nserver", Value: "0", Help: "服务数量"}, @@ -725,14 +728,17 @@ var Index = &Context{Name: "ctx", Help: "根上下文", "cert": &Config{Name: "cert", Value: "etc/cert.pem", Help: "证书文件"}, "key": &Config{Name: "key", Value: "etc/key.pem", Help: "私钥文件"}, + "root": &Config{Name: "root", Value: ".", Help: "工作目录"}, "debug": &Config{Name: "debug", Value: "off", Help: "调试模式"}, "start": &Config{Name: "start", Value: "cli", Help: "默认启动模块"}, "init.sh": &Config{Name: "init.sh", Value: "etc/init.sh", Help: "默认启动脚本"}, "bench.log": &Config{Name: "bench.log", Value: "var/bench.log", Help: "默认日志文件", Hand: func(c *Context, x *Config, arg ...string) string { if len(arg) > 0 { // {{{ - l, e := os.Create(x.Value) - c.Check(e) - log.SetOutput(l) + if e := os.MkdirAll(path.Dir(arg[0]), os.ModePerm); e == nil { + if l, e := os.Create(x.Value); e == nil { + log.SetOutput(l) + } + } } return x.Value // }}} @@ -740,29 +746,42 @@ var Index = &Context{Name: "ctx", Help: "根上下文", }, Commands: map[string]*Command{ "void": &Command{"void", "建立远程连接", func(c *Context, m *Message, key string, arg ...string) string { - m.Echo("hello void!\n") + m.Echo("hello void!\n") // {{{ return "" + // }}} }}, }, Session: map[string]*Message{"root": Pulse}, Resource: []*Message{Pulse}, } -var Pulse = &Message{Code: 0, Time: time.Now(), Index: 0, Name: "root"} +// }}} -func init() { +func init() { // {{{ if len(os.Args) > 1 { - Index.Conf("bench.log", os.Args[1]) + Index.Conf("root", os.Args[1]) + } + if e := os.MkdirAll(Index.Conf("root"), os.ModePerm); e != nil { + fmt.Println(e) + os.Exit(1) + } + if e := os.Chdir(Index.Conf("root")); e != nil { + fmt.Println(e) + os.Exit(1) + } + + if len(os.Args) > 2 { + Index.Conf("bench.log", os.Args[2]) } else { Index.Conf("bench.log", Index.Conf("bench.log")) } - if len(os.Args) > 2 { - Index.Conf("init.sh", os.Args[2]) + if len(os.Args) > 3 { + Index.Conf("init.sh", os.Args[3]) } - if len(os.Args) > 3 { - Index.Conf("start", os.Args[3]) + if len(os.Args) > 4 { + Index.Conf("start", os.Args[4]) } log.Println("\n\n\n") } @@ -783,16 +802,21 @@ func Start() { } } + n := 0 for _, s := range Index.Contexts { if ok, _ := regexp.MatchString(Index.Conf("start"), s.Name); ok { + n++ go s.Start(Pulse.Spawn(s, s.Name).Put("detail", os.Stdout)) } } for { <-Pulse.Wait - if Index.Capi("nserver", 0) == 0 { + n-- + if n <= 0 && Index.Capi("nserver", 0) == 0 { return } } } + +// }}} diff --git a/src/context/web/web.go b/src/context/web/web.go index 85b7567e..25de8861 100644 --- a/src/context/web/web.go +++ b/src/context/web/web.go @@ -1,39 +1,88 @@ -package web - -import ( +package web // {{{ +// }}} +import ( // {{{ "context" - "context/cli" + "log" "net/http" ) +// }}} + type WEB struct { *ctx.Context } -func (web *WEB) Begin() bool { +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() + + log.Println(s.ListenAndServe()) return true } -func (web *WEB) Start() bool { - http.Handle("/", http.FileServer(http.Dir(web.Conf("path")))) - http.ListenAndServe(web.Conf("address"), nil) + +// }}} +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.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 } -func (web *WEB) Spawn(c *ctx.Context, arg ...string) ctx.Server { - return nil -} +// }}} -var Index = &ctx.Context{Name: "web", Help: "网页服务", - Caches: map[string]*ctx.Cache{}, - Configs: map[string]*ctx.Config{ - "path": &ctx.Config{Name: "path", Value: "srv", Help: "监听地址"}, - "address": &ctx.Config{Name: "address", Value: ":9494", Help: "监听地址"}, +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{"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:]...) + } + return "" + // }}} + }}, }, } -func init() { +// }}} + +func init() { // {{{ web := &WEB{} web.Context = Index - cli.Index.Register(Index, web) + ctx.Index.Register(Index, web) } + +// }}} diff --git a/src/example/bench.go b/src/example/bench.go index ec3ee2d9..77cd58b7 100644 --- a/src/example/bench.go +++ b/src/example/bench.go @@ -3,7 +3,6 @@ package main import ( "context" _ "context/cli" - _ "context/tcp" ) func main() {