diff --git a/base/tcp/client.go b/base/tcp/client.go index 7256fae8..418f5cb1 100644 --- a/base/tcp/client.go +++ b/base/tcp/client.go @@ -39,8 +39,10 @@ func (c *Conn) Close() error { const ( OPEN = "open" - CLOSE = "close" + START = "start" ERROR = "error" + CLOSE = "close" + STOP = "stop" ) const ( DIAL_CB = "dial.cb" diff --git a/base/web/render.go b/base/web/render.go index 6ae82469..3d8c8e25 100644 --- a/base/web/render.go +++ b/base/web/render.go @@ -8,83 +8,85 @@ import ( "fmt" "net/http" - "os" "path" "strings" "time" ) const ( - STATUS = "status" - COOKIE = "cookie" + REDIRECT = "redirect" + REFRESH = "refresh" + STATUS = "status" + COOKIE = "cookie" ) func Render(msg *ice.Message, cmd string, args ...interface{}) { if cmd != "" { - defer func() { msg.Log(ice.LOG_EXPORT, "%s: %v", cmd, args) }() + defer func() { msg.Log_EXPORT(cmd, args) }() } + switch arg := kit.Simple(args...); cmd { - case ice.RENDER_VOID: - case ice.RENDER_OUTPUT: - case "redirect": + case REDIRECT: // url [arg...] http.Redirect(msg.W, msg.R, kit.MergeURL(arg[0], arg[1:]), 307) - case "refresh": + case REFRESH: // [delay [text]] arg = []string{"200", fmt.Sprintf(`%s`, kit.Int(kit.Select("3", arg, 0)), kit.Select("请稍后,系统初始化中...", arg, 1), )} fallthrough - case STATUS: + case STATUS: // [code [text]] RenderStatus(msg, kit.Int(kit.Select("200", arg, 0)), kit.Select("", arg, 1)) - case COOKIE: + case COOKIE: // value [name [path [expire]]] RenderCookie(msg, arg[0], arg[1:]...) - case ice.RENDER_DOWNLOAD: + case ice.RENDER_DOWNLOAD: // file [type [name]] msg.W.Header().Set("Content-Disposition", fmt.Sprintf("filename=%s", kit.Select(path.Base(arg[0]), arg, 2))) - msg.W.Header().Set("Content-Type", kit.Select("text/html", arg, 1)) + if RenderType(msg.W, arg[0], kit.Select("", arg, 1)); !ice.DumpBinPack(msg.W, arg[0], nil) { + http.ServeFile(msg.W, msg.R, arg[0]) + } - if !ice.DumpBinPack(msg.W, arg[0], func(name string) { RenderType(msg.W, name) }) { - if _, e := os.Stat(arg[0]); e == nil { - http.ServeFile(msg.W, msg.R, arg[0]) - } + case ice.RENDER_QRCODE: // text [size] + if qr, e := qrcode.New(arg[0], qrcode.Medium); msg.Assert(e) { + msg.W.Header().Set(ContentType, ContentPNG) + msg.Assert(qr.Write(kit.Int(kit.Select("256", arg, 1)), msg.W)) } case ice.RENDER_RESULT: - if len(arg) > 0 { + if len(arg) > 0 { // [str [arg...]] msg.W.Write([]byte(kit.Format(arg[0], args[1:]...))) } else { args = append(args, "length:", len(msg.Result())) msg.W.Write([]byte(msg.Result())) } - case ice.RENDER_QRCODE: - if qr, e := qrcode.New(arg[0], qrcode.Medium); msg.Assert(e) { - msg.W.Header().Set("Content-Type", "image/png") - msg.Assert(qr.Write(kit.Int(kit.Select("256", arg, 1)), msg.W)) - } + case ice.RENDER_VOID: + // no output default: - if cmd != "" { + if cmd != "" { // [str [arg...]] msg.Echo(kit.Format(cmd, args...)) } - msg.W.Header().Set("Content-Type", "application/json") - fmt.Fprint(msg.W, msg.Formats("meta")) - } - msg.Append(ice.MSG_OUTPUT, ice.RENDER_OUTPUT) -} -func RenderType(w http.ResponseWriter, name string) { - if strings.HasSuffix(name, ".css") { - w.Header().Set("Content-Type", "text/css; charset=utf-8") + msg.W.Header().Set(ContentType, ContentJSON) + fmt.Fprint(msg.W, msg.Formats(kit.MDB_META)) } } -func RenderCookie(msg *ice.Message, value string, arg ...string) { // name path expire - expire := time.Now().Add(kit.Duration(kit.Select(msg.Conf(aaa.SESS, "meta.expire"), arg, 2))) - http.SetCookie(msg.W, &http.Cookie{Value: value, Name: kit.Select(ice.MSG_SESSID, arg, 0), Path: kit.Select("/", arg, 1), Expires: expire}) -} func RenderStatus(msg *ice.Message, code int, text string) { msg.W.WriteHeader(code) msg.W.Write([]byte(text)) } +func RenderCookie(msg *ice.Message, value string, arg ...string) { // name path expire + expire := time.Now().Add(kit.Duration(kit.Select(msg.Conf(aaa.SESS, "meta.expire"), arg, 2))) + http.SetCookie(msg.W, &http.Cookie{Value: value, Name: kit.Select(ice.MSG_SESSID, arg, 0), Path: kit.Select("/", arg, 1), Expires: expire}) +} +func RenderType(w http.ResponseWriter, name, mime string) { + if mime != "" { + w.Header().Set(ContentType, mime) + } else if strings.HasSuffix(name, ".css") { + w.Header().Set(ContentType, "text/css; charset=utf-8") + } else { + w.Header().Set(ContentType, ContentHTML) + } +} diff --git a/base/web/serve.go b/base/web/serve.go index 916569e9..554d1868 100644 --- a/base/web/serve.go +++ b/base/web/serve.go @@ -68,7 +68,7 @@ func _serve_main(m *ice.Message, w http.ResponseWriter, r *http.Request) bool { } // 内置文件 - return !ice.DumpBinPack(w, r.URL.Path, func(name string) { RenderType(w, name) }) + return !ice.DumpBinPack(w, r.URL.Path, func(name string) { RenderType(w, name, "") }) } func _serve_handle(key string, cmd *ice.Command, msg *ice.Message, w http.ResponseWriter, r *http.Request) { defer func() { @@ -219,16 +219,9 @@ func init() { }, Commands: map[string]*ice.Command{ SERVE: {Name: "serve name auto start", Help: "服务器", Action: map[string]*ice.Action{ - gdb.START: {Name: "start dev= name=self proto=http host= port=9020", Help: "启动", Hand: func(m *ice.Message, arg ...string) { - if cli.NodeInfo(m, SERVER, ice.Info.HostName); m.Option(tcp.PORT) == "random" { - m.Option(tcp.PORT, m.Cmdx(tcp.PORT, aaa.RIGHT)) - } - - m.Target().Start(m, kit.MDB_NAME, m.Option(kit.MDB_NAME), tcp.HOST, m.Option(tcp.HOST), tcp.PORT, m.Option(tcp.PORT)) - m.Sleep("1s") - - for _, k := range kit.Split(m.Option(SPIDE_DEV)) { - m.Cmd(SPACE, tcp.DIAL, SPIDE_DEV, k, kit.MDB_NAME, ice.Info.NodeName) + aaa.BLACK: {Name: "black", Help: "黑名单", Hand: func(m *ice.Message, arg ...string) { + for _, k := range arg { + m.Conf(SERVE, kit.Keys(kit.MDB_META, aaa.BLACK, k), true) } }}, aaa.WHITE: {Name: "white", Help: "白名单", Hand: func(m *ice.Message, arg ...string) { @@ -236,9 +229,16 @@ func init() { m.Conf(SERVE, kit.Keys(kit.MDB_META, aaa.WHITE, k), true) } }}, - aaa.BLACK: {Name: "black", Help: "黑名单", Hand: func(m *ice.Message, arg ...string) { - for _, k := range arg { - m.Conf(SERVE, kit.Keys(kit.MDB_META, aaa.BLACK, k), true) + gdb.START: {Name: "start dev= name=self proto=http host= port=9020", Help: "启动", Hand: func(m *ice.Message, arg ...string) { + if cli.NodeInfo(m, SERVER, ice.Info.HostName); m.Option(tcp.PORT) == "random" { + m.Option(tcp.PORT, m.Cmdx(tcp.PORT, aaa.RIGHT)) + } + + m.Target().Start(m, kit.MDB_NAME, m.Option(kit.MDB_NAME), tcp.HOST, m.Option(tcp.HOST), tcp.PORT, m.Option(tcp.PORT)) + m.Sleep(ice.MOD_TICK) + + for _, k := range kit.Split(m.Option(SPIDE_DEV)) { + m.Cmd(SPACE, tcp.DIAL, SPIDE_DEV, k, kit.MDB_NAME, ice.Info.NodeName) } }}, }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { @@ -253,10 +253,10 @@ func init() { m.Render(ice.RENDER_DOWNLOAD, path.Join(m.Conf(SERVE, "meta.volcanos.path"), path.Join(arg...))) }}, "/publish/": {Name: "/publish/", Help: "源码", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - _share_local(m, m.Conf(SERVE, "meta.publish"), path.Join(arg...)) + _share_local(m, m.Conf(SERVE, kit.Keym("publish")), path.Join(arg...)) }}, "/plugin/github.com/": {Name: "/plugin/github.com/", Help: "源码", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - _share_repos(m, "github.com/"+arg[0]+"/"+arg[1], arg[2:]...) + _share_repos(m, path.Join("github.com", arg[0], arg[1]), arg[2:]...) }}, }}) } diff --git a/base/web/spide.go b/base/web/spide.go index ce41eda8..484e32de 100644 --- a/base/web/spide.go +++ b/base/web/spide.go @@ -73,6 +73,7 @@ const ( ContentFORM = "application/x-www-form-urlencoded" ContentJSON = "application/json" ContentHTML = "text/html" + ContentPNG = "image/png" ) const ( ADDRESS = "address" diff --git a/base/web/web.go b/base/web/web.go index 80457e07..4d925323 100644 --- a/base/web/web.go +++ b/base/web/web.go @@ -71,11 +71,12 @@ func (web *Frame) Start(m *ice.Message, arg ...string) bool { web.m, web.Server = m, &http.Server{Handler: web} m.Option(tcp.LISTEN_CB, func(l net.Listener) { - m.Cmdy(mdb.INSERT, SERVE, "", mdb.HASH, arg, kit.MDB_STATUS, "start", "proto", m.Option("proto"), "dev", m.Option("dev")) + m.Cmdy(mdb.INSERT, SERVE, "", mdb.HASH, arg, kit.MDB_STATUS, tcp.START, kit.MDB_PROTO, m.Option(kit.MDB_PROTO), SPIDE_DEV, m.Option(SPIDE_DEV)) m.Event(SERVE_START, arg...) defer m.Event(SERVE_CLOSE, arg...) - defer m.Cmd(mdb.MODIFY, SERVE, "", mdb.HASH, kit.MDB_NAME, m.Option(kit.MDB_NAME), kit.MDB_STATUS, "stop") + defer m.Cmd(mdb.MODIFY, SERVE, "", mdb.HASH, kit.MDB_NAME, m.Option(kit.MDB_NAME), kit.MDB_STATUS, tcp.STOP) + // 启动服务 m.Warn(true, SERVE, ": ", web.Server.Serve(l)) }) @@ -97,7 +98,7 @@ var Index = &ice.Context{Name: WEB, Help: "网络模块", m.Cmd(SPIDE, mdb.CREATE, SPIDE_DEV, kit.Select("http://:9020", m.Conf(cli.RUNTIME, "conf.ctx_dev"))) m.Cmd(SPIDE, mdb.CREATE, SPIDE_SELF, kit.Select("http://:9020", m.Conf(cli.RUNTIME, "conf.ctx_self"))) m.Cmd(SPIDE, mdb.CREATE, SPIDE_SHY, kit.Select("https://shylinux.com:443", m.Conf(cli.RUNTIME, "conf.ctx_shy"))) - m.Cmd(mdb.SEARCH, mdb.CREATE, SPACE, SPACE, WEB) + m.Cmd(mdb.SEARCH, mdb.CREATE, SPACE, m.Prefix(SPACE)) }}, ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { m.Save() @@ -112,6 +113,7 @@ var Index = &ice.Context{Name: WEB, Help: "网络模块", func init() { ice.Index.Register(Index, &Frame{}, - SPIDE, SERVE, SPACE, DREAM, ROUTE, SHARE, CACHE, STORY, + SPIDE, SERVE, SPACE, DREAM, + ROUTE, CACHE, SHARE, STORY, ) } diff --git a/base/web/web.shy b/base/web/web.shy index a1017653..528275be 100644 --- a/base/web/web.shy +++ b/base/web/web.shy @@ -8,6 +8,5 @@ field "梦想家" web.dream field "路由器" web.route field "共享链" web.share field "缓存池" web.cache -return field "故事会" web.story diff --git a/conf.go b/conf.go index 3e1a28c3..9ff866f4 100644 --- a/conf.go +++ b/conf.go @@ -30,8 +30,8 @@ const ( // MSG MSG_PROCESS = "_process" MSG_CMDS = "cmds" - MSG_SESSID = "sessid" MSG_DOMAIN = "domain" + MSG_SESSID = "sessid" MSG_USERIP = "user.ip" MSG_USERUA = "user.ua" @@ -43,7 +43,6 @@ const ( // MSG MSG_USERNAME = "user.name" MSG_USERZONE = "user.zone" MSG_USERROLE = "user.role" - MSG_USERDATA = "user.data" MSG_USERADDR = "user.addr" @@ -51,6 +50,13 @@ const ( // MSG MSG_STORM = "sess.storm" MSG_LOCAL = "sess.local" ) +const ( // RENDER + RENDER_VOID = "_void" + RENDER_RESULT = "_result" + RENDER_QRCODE = "_qrcode" + RENDER_DOWNLOAD = "_download" + RENDER_TEMPLATE = "_template" +) const ( CONTROL_PAGE = "_page" @@ -98,11 +104,3 @@ const ( // LOG LOG_ERROR = "error" LOG_DEBUG = "debug" ) -const ( // RENDER - RENDER_VOID = "_void" - RENDER_OUTPUT = "_output" - RENDER_RESULT = "_result" - RENDER_QRCODE = "_qrcode" - RENDER_DOWNLOAD = "_download" - RENDER_TEMPLATE = "_template" -) diff --git a/meta.go b/meta.go index 2c756cbd..db22ce5e 100644 --- a/meta.go +++ b/meta.go @@ -347,7 +347,7 @@ func (m *Message) Render(cmd string, args ...interface{}) *Message { m.Optionv(MSG_ARGS, args) switch cmd { - case RENDER_TEMPLATE: + case RENDER_TEMPLATE: // text [data [type]] if len(args) == 1 { args = append(args, m) } diff --git a/misc/git/server.go b/misc/git/server.go index 2a7dc3ce..ca7692a9 100644 --- a/misc/git/server.go +++ b/misc/git/server.go @@ -20,10 +20,10 @@ func init() { }, Commands: map[string]*ice.Command{ web.LOGIN: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - m.Option(ice.RENDER_OUTPUT, ice.RENDER_RESULT) + m.Render(ice.RENDER_RESULT) }}, "/github.com/": {Name: "github.com", Help: "github.com", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - m.Render(ice.RENDER_OUTPUT, ice.RENDER_VOID) + m.Render(ice.RENDER_VOID) p := kit.Path(".ish/pluged") m.Debug("what %v", p) git := githttp.New(p)