diff --git a/src/contexts/ctx.go b/src/contexts/ctx.go index 51cb497f..67643074 100644 --- a/src/contexts/ctx.go +++ b/src/contexts/ctx.go @@ -1956,11 +1956,11 @@ var Index = &Context{Name: "ctx", Help: "模块中心", m.Echo(" ARM Linux HTTP\n") m.Echo("\n") - m.Color(31, "Content ").Color(32, "Message\n") + m.Color(31, "Context ").Color(32, "Message\n") m.Color(32, "ctx ").Color(33, "cli ").Color(31, "aaa ").Color(33, "web\n") m.Color(32, "lex ").Color(33, "yac ").Color(31, "log ").Color(33, "gdb\n") m.Color(32, "tcp ").Color(33, "nfs ").Color(31, "ssh ").Color(33, "mdb\n") - m.Color(31, "template ").Color(32, "script\n") + m.Color(31, "script ").Color(32, "template\n") return } diff --git a/src/contexts/web/web.go b/src/contexts/web/web.go index f31db6a1..e9397686 100644 --- a/src/contexts/web/web.go +++ b/src/contexts/web/web.go @@ -153,14 +153,17 @@ func (web *WEB) generate(m *ctx.Message, uri string, arg ...string) string { // } // }}} - func (web *WEB) AppendJson(msg *ctx.Message) string { // {{{ meta := map[string][]string{} if !msg.Has("result") && !msg.Has("append") { return "" } - if meta["result"] = msg.Meta["result"]; msg.Has("append") { + if len(msg.Meta["result"]) > 0 { + meta["result"] = msg.Meta["result"] + } + + if len(msg.Meta["append"]) > 0 { meta["append"] = msg.Meta["append"] for _, v := range msg.Meta["append"] { meta[v] = msg.Meta[v] @@ -603,6 +606,88 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", m.Copy(msg, "result") // }}} }}, + "/travel": &ctx.Command{Name: "/travel", Help: "文件上传", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + if !m.Options("module") { //{{{ + m.Option("module", "ctx") + } + + r := m.Data["request"].(*http.Request) + w := m.Data["response"].(http.ResponseWriter) + + // 权限检查 + if m.Option("method") == "POST" { + if m.Options("domain") { + msg := m.Find("ssh", true) + msg.Detail(0, "send", "domain", m.Option("domain"), "context", "find", m.Option("module"), m.Option("ccc")) + if m.Options("name") { + msg.Add("detail", m.Option("name")) + } + if m.Options("value") { + msg.Add("detail", m.Option("value")) + } + + msg.CallBack(true, func(sub *ctx.Message) *ctx.Message { + m.Copy(sub, "result").Copy(sub, "append") + return nil + }) + return + } + + msg := m.Find(m.Option("module"), true) + if msg == nil { + return + } + + switch m.Option("ccc") { + case "cache": + m.Echo(msg.Cap(m.Option("name"))) + case "config": + if m.Options("value") { + m.Echo(msg.Conf(m.Option("name"), m.Option("value"))) + } else { + m.Echo(msg.Conf(m.Option("name"))) + } + case "command": + msg = msg.Spawn(msg.Target()) + msg.Detail(0, m.Option("name")) + if m.Options("value") { + msg.Add("detail", m.Option("value")) + } + + msg.Cmd() + m.Copy(msg, "result").Copy(msg, "append") + } + return + } + + // 解析模板 + render := m.Spawn(m.Target()).Put("option", "request", r).Put("option", "response", w) + defer render.Cmd(m.Conf("travel_main"), m.Conf("travel_tmpl")) + + if msg := m.Find(m.Option("module"), true); msg != nil { + m.Option("tmpl", "") + for _, v := range []string{"cache", "config", "command", "module", "domain"} { + if m.Options("domain") { + msg = m.Find("ssh", true) + msg.Detail(0, "send", "domain", m.Option("domain"), "context", "find", m.Option("module"), v) + msg.CallBack(true, func(sub *ctx.Message) *ctx.Message { + msg.Copy(sub, "result").Copy(sub, "append") + return nil + }) + } else { + msg = msg.Spawn(msg.Target()) + msg.Cmd("context", "find", m.Option("module"), v) + } + + if len(msg.Meta["append"]) > 0 { + render.Option("current_module", m.Option("module")) + render.Option("current_domain", m.Option("domain")) + render.Sesss(v, msg).Add("option", "tmpl", v) + } + } + } + // }}} + }}, "/upload": &ctx.Command{Name: "/upload", Help: "文件上传", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { r := m.Data["request"].(*http.Request) // {{{ w := m.Data["response"].(http.ResponseWriter) @@ -611,37 +696,28 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", m.Option("file", m.Cap("directory")) } - // 权限检查 - m.Option("right", "") - m.Option("message", "") - m.Option("username", "") - aaa := m.Find("aaa").Cmd("login", m.Option("sessid")) - if aaa.Result(0) == "error: " { - m.Option("sessid", "") - m.Option("message", "login failure") - } else { - m.Option("username", aaa.Result(0)) - msg := m.Spawn(m.Target()).Cmd("right", "check", aaa.Cap("group"), "command", "/upload", "file", m.Option("file")) - if msg.Result(0) == "ok" { - m.Option("right", aaa.Cap("group")) - } else { - m.Option("message", "your do not have the right of ", m.Option("file")) + auth := m.Spawn(m.Target()).Put("option", "request", r).Put("option", "response", w) + auth.CallBack(true, func(aaa *ctx.Message) *ctx.Message { + m.Log("fuck", nil, "upload-----------%v", aaa.Meta) + if aaa != nil { + m.Sesss("aaa", aaa) } + return aaa + }, "/check", "command", "/upload", "file", m.Option("file")) + m.Log("fuck", nil, "upload>>>>>>>>>>>%v", auth.Meta) + + if auth.Append("right") != "ok" { + return } if m.Option("method") == "POST" { - if !m.Options("right") { - return - } - if m.Options("notshareto") { // 取消共享 msg := m.Spawn(m.Target()) msg.Cmd("right", "del", m.Option("notshareto"), "command", "/upload", "file", m.Option("sharefile")) m.Append("link", "hello") return } else if m.Options("shareto") { //共享目录 - msg := aaa.Spawn(m.Target()) - msg.Sesss("aaa", aaa) + msg := m.Spawn(m.Target()) //TODO msg.Cmd("right", "add", m.Option("shareto"), "command", "/upload", "file", m.Option("sharefile")) m.Append("link", "hello") return @@ -684,18 +760,8 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", } // 解析模板 - tmpl := template.Must(template.New("fuck").Funcs(ctx.CGI).ParseGlob(path.Join(m.Conf("template_dir"), m.Conf("common_tmpl")))) - tmpl = template.Must(tmpl.ParseGlob(path.Join(m.Conf("template_dir"), m.Conf("upload_tmpl")))) - defer func() { - w.Header().Add("Content-Type", "text/html") - m.Assert(tmpl.ExecuteTemplate(w, m.Conf("upload_main"), m)) - }() - - if !m.Options("right") { - m.Option("title", "login") - m.Option("tmpl", "login") - return - } + render := m.Spawn(m.Target()).Put("option", "request", r).Put("option", "response", w) + defer render.Cmd(m.Conf("upload_main"), m.Conf("upload_tmpl")) // 输出文件 s, e := os.Stat(m.Option("file")) @@ -707,8 +773,8 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", // 共享列表 share := m.Sesss("share", m.Target()) index := share.Target().Index - if index != nil && index[m.Option("right")] != nil { - for k, v := range index[m.Option("right")].Index { + if index != nil && index[m.Option("group")] != nil { + for k, v := range index[m.Option("group")].Index { for i, j := range v.Commands { for v, n := range j.Shares { for _, nn := range n { @@ -803,118 +869,64 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", m.Option("tmpl", "userinfo", "share", "list", "git", "upload", "create") // }}} }}, - "/travel": &ctx.Command{Name: "/travel", Help: "文件上传", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { - w := m.Data["response"].(http.ResponseWriter) // {{{ + "/render": &ctx.Command{Name: "/render [main [tmpl]]", Help: "生成模板", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + w := m.Data["response"].(http.ResponseWriter) - if m.Option("method") == "POST" { - if m.Options("domain") { - msg := m.Find("ssh", true) - msg.Detail(0, "send", "domain", m.Option("domain"), "context", "find", m.Option("module"), m.Option("ccc")) - if m.Options("name") { - msg.Add("detail", m.Option("name")) - } - if m.Options("value") { - msg.Add("detail", m.Option("value")) - } - - msg.CallBack(true, func(sub *ctx.Message) *ctx.Message { - m.Copy(sub, "result").Copy(sub, "append") - return nil - }) - return - } - - module := "ctx" - if m.Options("module") { - module = m.Option("module") - } - msg := m.Find(module, true) - if msg == nil { - return - } - - switch m.Option("ccc") { - case "cache": - m.Echo(msg.Cap(m.Option("name"))) - case "config": - if m.Options("value") { - m.Echo(msg.Conf(m.Option("name"), m.Option("value"))) - } else { - m.Echo(msg.Conf(m.Option("name"))) - } - case "command": - msg = msg.Spawn(msg.Target()) - msg.Detail(0, m.Option("name")) - if m.Options("value") { - msg.Add("detail", m.Option("value")) - } - - msg.Cmd() - m.Copy(msg, "result").Copy(msg, "append") - } - return + tpl := template.Must(template.New("fuck").Funcs(ctx.CGI).ParseGlob(path.Join(m.Conf("template_dir"), m.Conf("common_tmpl")))) + if len(arg) > 1 { + tpl = template.Must(tpl.ParseGlob(path.Join(m.Conf("template_dir"), arg[1]))) } - module := "ctx" - if m.Options("module") { - module = m.Option("module") + main := m.Conf("common_main") + if len(arg) > 0 { + main = arg[0] } w.Header().Add("Content-Type", "text/html") - tmpl := template.Must(template.Must(template.New("fuck").Funcs(ctx.CGI).ParseGlob(m.Conf("template_dir") + "/common/*.html")).ParseGlob(m.Conf("template_dir") + "travel.html")) - m.Assert(tmpl) - - m.Assert(tmpl.ExecuteTemplate(w, "head", m.Meta)) - - if msg := m.Find(module, true); msg != nil { - for _, v := range []string{"cache", "config", "command", "module", "domain"} { - if m.Options("domain") { - msg = m.Find("ssh", true) - msg.Detail(0, "send", "domain", m.Option("domain"), "context", "find", module, v) - msg.CallBack(true, func(sub *ctx.Message) *ctx.Message { - msg.Copy(sub, "result").Copy(sub, "append") - return nil - }) - } else { - msg = msg.Spawn(msg.Target()) - msg.Cmd("context", "find", module, v) - } - - if len(msg.Meta["append"]) > 0 { - msg.Option("current_module", module) - msg.Option("current_domain", m.Option("domain")) - m.Assert(tmpl.ExecuteTemplate(w, v, msg.Meta)) - } - } - } - - m.Assert(tmpl.ExecuteTemplate(w, "tail", m.Meta)) - delete(m.Meta, "result") - delete(m.Meta, "append") - // }}} + m.Assert(tpl.ExecuteTemplate(w, main, m)) }}, - "/check": &ctx.Command{Name: "/check", Help: "文件上传", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { - m.Option("right", "") - m.Option("message", "") - m.Option("username", "") - aaa := m.Find("aaa").Cmd("login", m.Option("sessid")) - if aaa.Result(0) == "error: " { - m.Option("sessid", "") - m.Option("message", "login failure") - } else { - m.Option("username", aaa.Result(0)) - msg := m.Spawn(m.Target()).Cmd("right", "check", aaa.Cap("group"), "command", "/upload", "file", m.Option("file")) - if msg.Result(0) == "ok" { - m.Option("right", aaa.Cap("group")) - } else { - m.Option("message", "your do not have the right of ", m.Option("file")) - } - } - }}, - "/login": &ctx.Command{Name: "/login", Help: "文件上传", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + "/check": &ctx.Command{Name: "/check sessid", Help: "权限检查", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + r := m.Data["request"].(*http.Request) w := m.Data["response"].(http.ResponseWriter) - msg := m.Find("aaa").Cmd("login", m.Option("username"), m.Option("password")) - http.SetCookie(w, &http.Cookie{Name: "sessid", Value: msg.Result(0)}) + + user := m.Spawn(m.Target()).Put("option", "request", r).Put("option", "response", w) + user.CallBack(true, func(aaa *ctx.Message) *ctx.Message { + if aaa.Caps("group") { + msg := aaa.Spawn(m.Target()).Cmd("right", "check", aaa.Cap("group"), arg) + m.Append("right", msg.Result(0)) + } + m.Log("fuck", nil, "aaa: %v", aaa) + return aaa + }, "/login") + m.Log("fuck", nil, "aaa----: %v", m) + }}, + "/login": &ctx.Command{Name: "/login", Help: "用户登录", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + r := m.Data["request"].(*http.Request) + w := m.Data["response"].(http.ResponseWriter) + + if m.Options("sessid") { + if aaa := m.Find("aaa").Cmd("login", m.Option("sessid")); aaa.Result(0) != "error: " { + m.Append("username", aaa.Cap("username")) + m.Append("group", aaa.Cap("group")) + m.Back(aaa) + return + } + } + + sessid := "" + if m.Options("password") { + msg := m.Find("aaa").Cmd("login", m.Option("username"), m.Option("password")) + sessid = msg.Result(0) + http.SetCookie(w, &http.Cookie{Name: "sessid", Value: sessid}) + } + + if sessid != "" { + http.Redirect(w, r, "/upload", http.StatusFound) + } else { + render := m.Spawn(m.Target()).Put("option", "request", r).Put("option", "response", w) + render.Cmd("/render", "login.html") + } + m.Back(m) }}, "temp": &ctx.Command{Name: "temp", Help: "应用示例", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { msg := m.Spawn(m.Target()) @@ -938,9 +950,6 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", tmpl.ExecuteTemplate(w, "head", m) tmpl.ExecuteTemplate(w, "tail", m.Meta) }}, - "hi": &ctx.Command{Name: "hi", Help: "应用示例", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { - m.Echo("hello") - }}, }, } diff --git a/usr/template/common/login.html b/usr/template/common/login.html index ca1cf1a2..b79e9738 100644 --- a/usr/template/common/login.html +++ b/usr/template/common/login.html @@ -1,25 +1,11 @@ {{define "login"}}
- {{end}} {{define "userinfo"}} @@ -93,3 +79,13 @@ } {{end}} + +{{$msg := .}} +{{$meta := .Meta}} + +{{template "head" $meta}} +{{if meta $meta.message}} + {{template "message" $meta}} +{{end}} +{{template "login" $meta}} +{{template "tail" $meta}} diff --git a/usr/template/travel.html b/usr/template/travel.html index 2299defc..0c450893 100644 --- a/usr/template/travel.html +++ b/usr/template/travel.html @@ -11,6 +11,9 @@ cursor:pointer; background-color:lightgray; } + code { + font-size:14px; + } .name { padding-left:10px; text-align:left; @@ -27,9 +30,6 @@ padding-left:10px; text-align:left; } - code { - font-size:14px; - } .result { background-color:black; color:white; @@ -245,3 +245,22 @@