1
0
forked from x/ContextOS

mac add aaa.userinfo web.upload context.Cookie context.Search context.Cache

This commit is contained in:
shaoying 2018-10-24 09:34:13 +08:00
parent 1bd5acc897
commit 4fe19b1818
6 changed files with 243 additions and 84 deletions

View File

@ -216,6 +216,19 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
}
}
}},
"userinfo": &ctx.Command{Name: "userinfo sessid", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Travel(func(m *ctx.Message, n int) bool {
if m.Cap("sessid") == arg[0] {
m.Append("method", m.Cap("method"))
m.Append("stream", m.Cap("stream"))
m.Append("sessid", m.Cap("sessid"))
m.Append("login_time", m.Cap("login_time"))
m.Append("expire_time", m.Cap("expire_time"))
}
return true
})
m.Table()
}},
"right": &ctx.Command{Name: "right [user [check|owner|share group [order] [add|del]]]", Form: map[string]int{"from": 1}, Help: "权限管理", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Travel(func(m *ctx.Message, n int) bool {
if n == 0 {

View File

@ -1332,6 +1332,15 @@ func (m *Message) Appendv(key string, arg ...interface{}) interface{} {
return nil
}
func (m *Message) Parse(arg string) string {
if len(arg) > 1 && arg[0] == '$' {
return m.Cap(arg[1:])
}
if len(arg) > 1 && arg[0] == '@' {
return m.Confx(arg[1:])
}
return arg
}
func (m *Message) Wait() bool {
if m.target.exit != nil {
return <-m.target.exit

View File

@ -84,6 +84,7 @@ func (web *WEB) HandleCmd(m *ctx.Message, key string, cmd *ctx.Command) {
msg.Option("referer", r.Header.Get("Referer"))
msg.Option("accept", r.Header.Get("Accept"))
r.ParseMultipartForm(int64(m.Confi("multipart_bsize")))
if r.ParseForm(); len(r.PostForm) > 0 {
for k, v := range r.PostForm {
m.Log("info", "%s: %v", k, v)
@ -147,11 +148,11 @@ func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
if r.URL.Path == "/" && m.Confs("root_index") {
http.Redirect(w, r, m.Conf("root_index"), http.StatusFound)
} else {
web.ServeMux.ServeHTTP(w, r)
r.URL.Path = m.Conf("root_index")
}
web.ServeMux.ServeHTTP(w, r)
if m.Confs("logheaders") {
for k, v := range w.Header() {
m.Log("info", "%s: %v", k, v)
@ -169,7 +170,7 @@ func (web *WEB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
return s
}
func (web *WEB) Begin(m *ctx.Message, arg ...string) ctx.Server {
web.Configs["root_index"] = &ctx.Config{Name: "root_index", Value: "", Help: "默认路由"}
web.Configs["root_index"] = &ctx.Config{Name: "root_index", Value: "/render", Help: "默认路由"}
web.Configs["logheaders"] = &ctx.Config{Name: "logheaders(yes/no)", Value: "no", Help: "日志输出报文头"}
web.Caches["directory"] = &ctx.Cache{Name: "directory", Value: m.Confx("directory", arg, 0), Help: "服务目录"}
web.Caches["route"] = &ctx.Cache{Name: "route", Value: "/" + web.Context.Name + "/", Help: "模块路由"}
@ -237,6 +238,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
"nroute": &ctx.Cache{Name: "nroute", Value: "0", Help: "路由数量"},
},
Configs: map[string]*ctx.Config{
"multipart_bsize": &ctx.Config{Name: "multipart_bsize", Value: "102400", Help: "缓存大小"},
"body_response": &ctx.Config{Name: "body_response", Value: "response", Help: "响应缓存"},
"method": &ctx.Config{Name: "method", Value: "GET", Help: "请求方法"},
"brow_home": &ctx.Config{Name: "brow_home", Value: "http://localhost:9094", Help: "服务"},
@ -258,11 +260,14 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
"componet": &ctx.Config{Name: "componet", Value: map[string]interface{}{
"login": []interface{}{
map[string]interface{}{
"template": "head", "name": "head", "help": "head",
"context": "", "command": "", "arguments": []interface{}{},
"name": "head", "help": "head", "template": "head",
},
map[string]interface{}{
"template": "componet", "name": "login", "help": "login",
"name": "userinfo", "help": "userinfo",
"context": "aaa", "command": "userinfo", "arguments": []interface{}{"@sessid"},
},
map[string]interface{}{
"name": "login", "help": "login", "template": "componet",
"context": "aaa", "command": "login", "arguments": []interface{}{"@username", "@password"},
"inputs": []interface{}{
map[string]interface{}{
@ -283,21 +288,20 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
"result_reload": "10",
},
map[string]interface{}{
"template": "tail", "name": "tail", "help": "tail",
"context": "", "command": "", "arguments": []interface{}{},
"name": "tail", "help": "tail", "template": "tail",
},
},
"index": []interface{}{
map[string]interface{}{
"template": "head", "name": "head", "help": "head",
"name": "head", "help": "head", "template": "head",
"context": "", "command": "", "arguments": []interface{}{},
},
map[string]interface{}{
"template": "clipboard", "name": "clipbaord", "help": "clipbaord",
"name": "clipbaord", "help": "clipbaord", "template": "clipboard",
"context": "", "command": "", "arguments": []interface{}{},
},
map[string]interface{}{
"template": "componet", "name": "message", "help": "message",
"name": "message", "help": "message", "template": "componet",
"context": "cli", "command": "buffer", "arguments": []interface{}{},
"inputs": []interface{}{
map[string]interface{}{
@ -311,9 +315,8 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
},
},
map[string]interface{}{
"template": "componet", "name": "time", "help": "time",
"context": "cli", "command": "time",
"arguments": []interface{}{"@string"},
"name": "time", "help": "time", "template": "componet",
"context": "cli", "command": "time", "arguments": []interface{}{"@string"},
"inputs": []interface{}{
map[string]interface{}{
"type": "text", "name": "time_format",
@ -330,7 +333,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
},
},
map[string]interface{}{
"template": "componet", "name": "json", "help": "json",
"name": "json", "help": "json", "template": "componet",
"context": "nfs", "command": "json",
"arguments": []interface{}{"@string"},
"inputs": []interface{}{
@ -345,7 +348,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
},
},
map[string]interface{}{
"template": "componet", "name": "dir", "help": "dir",
"name": "dir", "help": "dir", "template": "componet",
"context": "nfs", "command": "dir",
"arguments": []interface{}{"@dir",
"dir_deep", "no",
@ -353,6 +356,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
"dir_link", "<a class='download' data-type='%s'>%s<a>",
"dir_info", "",
},
"form_type": "upload",
"inputs": []interface{}{
map[string]interface{}{
"type": "text", "name": "dir",
@ -380,6 +384,9 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
map[string]interface{}{
"name": "filename", "value": "filename",
},
map[string]interface{}{
"name": "is_dir", "value": "is_dir",
},
map[string]interface{}{
"name": "line", "value": "line",
},
@ -415,10 +422,18 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
},
},
},
map[string]interface{}{
"type": "file", "name": "upload",
"label": "upload", "value": "upload",
},
map[string]interface{}{
"type": "submit", "name": "submit",
"label": "submit", "value": "submit",
},
},
},
map[string]interface{}{
"template": "componet", "name": "web_site", "help": "web_site",
"name": "web_site", "help": "web_site", "template": "componet",
"context": "web", "command": "config",
"arguments": []interface{}{
"web_site",
@ -427,7 +442,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
"display_result": "",
},
map[string]interface{}{
"template": "tail", "name": "tail", "help": "tail",
"name": "tail", "help": "tail", "template": "tail",
"context": "", "command": "", "arguments": []interface{}{},
},
},
@ -787,6 +802,22 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
m.Appendv("login", login)
m.Echo(sessid)
}},
"/upload": &ctx.Command{Name: "/upload", Help: "上传文件", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
r := m.Optionv("request").(*http.Request)
f, h, e := r.FormFile("upload")
m.Assert(e)
defer f.Close()
p := path.Join(m.Conf("directory"), m.Option("dir"), h.Filename)
o, e := os.Create(p)
m.Assert(e)
defer o.Close()
io.Copy(o, f)
m.Log("upload", "file(%d): %s", h.Size, p)
m.Append("redirect", m.Option("referer"))
}},
"/render": &ctx.Command{Name: "/render template", Help: "渲染模板, template: 模板名称", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if _, ok := m.Target().Server.(*WEB); m.Assert(ok) {
accept_json := strings.HasPrefix(m.Option("accept"), "application/json")
@ -796,9 +827,8 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
// m.Assert(e)
// tmpl.Funcs(ctx.CGI)
//
tmpl := template.New("render").Funcs(ctx.CGI)
tmpl.ParseGlob("/home/shaoying/context/usr/template/common/base.tmpl")
tmpl.ParseGlob(fmt.Sprintf("%s/context/usr/template/common/base.tmpl", os.Getenv("HOME")))
w := m.Optionv("response").(http.ResponseWriter)
if accept_json {
@ -810,22 +840,13 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
if m.Option("componet_group") == "" {
m.Option("componet_group", m.Conf("componet_group"))
}
group := m.Option("componet_group")
right := false
if group == "login" {
right = true
}
order := m.Option("componet_order")
right := group == "login"
login := m
login = nil
for count := 0; count == 0; {
order := -1
if m.Option("componet_order") != "" {
order = m.Optioni("componet_order")
}
if !right {
login = m.Spawn().Cmd("session").Appendv("login").(*ctx.Message)
}
@ -840,13 +861,13 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
}
}
for i, v := range m.Confv("componet", group).([]interface{}) {
if order != -1 && i != order {
for count := 0; count == 0; group, order, right = "login", "", true {
for _, v := range m.Confv("componet", group).([]interface{}) {
val := v.(map[string]interface{})
if order != "" && val["name"].(string) != order {
continue
}
val := v.(map[string]interface{})
order_right := right
if !order_right && login != nil {
if role := login.Confv("right", []interface{}{group, val["name"], "right", "role"}); role != nil && role.(string) == "share" {
@ -857,16 +878,21 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
continue
}
msg := m.Find(val["context"].(string))
context := m.Cap("module")
if val["context"] != nil {
context = val["context"].(string)
}
msg := m.Find(context)
if msg == nil {
if !accept_json {
if !accept_json && val["template"] != nil {
m.Assert(tmpl.ExecuteTemplate(w, val["template"].(string), m))
}
continue
}
count++
msg.Option("componet_order", i)
msg.Option("componet_order", val["name"].(string))
for k, v := range val {
if msg.Option(k) != "" {
@ -887,13 +913,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
for _, v := range val["arguments"].([]interface{}) {
switch value := v.(type) {
case string:
if len(value) > 1 && value[0] == '$' {
args = append(args, msg.Cap(value[1:]))
} else if len(value) > 1 && value[0] == '@' {
args = append(args, msg.Confx(value[1:]))
} else {
args = append(args, value)
}
args = append(args, m.Parse(value))
}
}
}
@ -906,30 +926,22 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
}
}
if msg.Cmd(val["command"], args); accept_json {
if val["command"] != nil {
msg.Cmd(val["command"], args)
}
if accept_json {
list = append(list, msg.Meta)
} else {
} else if val["template"] != nil {
m.Assert(tmpl.ExecuteTemplate(w, val["template"].(string), msg))
}
if msg.Appends("sessid") {
if msg.Detail(0) == "login" && msg.Appends("sessid") {
http.SetCookie(w, &http.Cookie{Name: "sessid", Value: msg.Append("sessid")})
m.Append("page_redirect", fmt.Sprintf("/render?componet_group=%s&componet_order=%s",
m.Option("componet_group", m.Option("last_componet_group")),
m.Option("componet_order", m.Option("last_componet_order"))))
m.Append("page_refresh", "10")
return
}
}
if count == 0 {
m.Option("last_componet_group", m.Option("componet_group"))
m.Option("last_componet_order", m.Option("componet_order"))
m.Option("componet_group", "login")
m.Option("componet_order", "-1")
group = m.Option("componet_group")
order = -1
right = true
}
}
if accept_json {

View File

@ -65,6 +65,20 @@ function send_command(form, cb) {
}
function onaction(event, action) {
switch (action) {
case "click":
if (event.target.nodeName == "INPUT") {
if (event.altKey) {
event.target.focus()
event.target.select()
console.log("fuck")
console.log(document.execCommand("paste"))
// var clipboard = document.querySelector("#clipboard")
// clipboard.value = text
// clipboard.select()
// document.execCommand("copy")
}
}
break
case "command":
send_command(event.target.form)
break
@ -148,10 +162,15 @@ function init_download(event) {
if (!option) {
return
}
document.querySelector("form.option.dir input[name=dir]").value = context.Search("download_dir")
option["dir"].value && send_command(option)
var append = document.querySelector("table.append.dir")
append.onchange =
append.onclick = function(event) {
console.log(event)
if (event.target.tagName == "A") {
if (event.target.dataset.type != "true") {
location.href = option["dir"].value+"/"+event.target.innerText
@ -170,6 +189,7 @@ function init_download(event) {
var sort_order = option["sort_order"]
switch (event.target.innerText) {
case "filename":
case "is_dir":
sort_order.value = (sort_order.value == "str")? "str_r": "str"
break
case "line":

View File

@ -1,4 +1,83 @@
context = {
Search: function(key, value) {
var args = {};
var search = location.search.split("?");
if (search.length > 1) {
var searchs = search[1].split("&");
for (var i = 0; i < searchs.length; i++) {
var keys = searchs[i].split("=");
args[keys[0]] = decodeURIComponent(keys[1]);
}
}
if (key == undefined) {
return args
} else if (typeof key == "object") {
for (var k in key) {
if (key[k] != undefined) {
args[k] = key[k];
}
}
} else if (value == undefined) {
return args[key] || this.Cookie(key);
} else {
args[key] = value;
}
var arg = [];
for (var k in args) {
arg.push(k+"="+encodeURIComponent(args[k]));
}
location.search = arg.join("&");
},
Cookie: function(key, value) {
if (key == undefined) {
cs = {}
cookies = document.cookie.split("; ")
for (var i = 0; i < cookies.length; i++) {
cookie = cookies[i].split("=")
cs[cookie[0]] = cookie[1]
}
return cs
}
if (typeof key == "object") {
for (var k in key) {
document.cookie = k+"="+key[k];
}
return this.Cookie()
}
if (value == undefined) {
var pattern = new RegExp(key+"=([^;]*);?");
var result = pattern.exec(document.cookie);
if (result && result.length > 0) {
return result[1];
}
return "";
}
document.cookie = key+"="+value;
return this.Cookie(key);
},
Cache: function(key, cb, sync) {
if (key == undefined) {
return this.cache
}
if (this.cache && !sync) {
typeof cb == "function" && cb(this.cache[key])
return this.cache[key]
}
var that = this
this.GET("", {"componet_group": "login", "componet_order": "userinfo"}, function(msg) {
msg = msg[0]
that.cache = {}
for (var i = 0; i < msg.append.length; i++) {
that.cache[msg.append[i]] = msg[msg.append[i]].join("")
}
typeof cb == "function" && cb(that.cache[key])
})
},
GET: function(url, form, cb) {
form = form || {}
@ -39,6 +118,8 @@ context = {
msg.result && console.log(msg.result.join(""));
if (msg.page_redirect) {
location.href = msg.page_redirect.join("")
} else if (msg.page_refresh) {
location.reload()
}
typeof cb == "function" && cb(msg)
}

View File

@ -61,6 +61,8 @@
<body onkeyup="return onaction(event, 'keymap')">
{{end}}
{{define "void"}}{{end}}
{{define "detail"}}{{detail .}}{{end}}
{{define "option"}}{{option .}}{{end}}
{{define "append"}}{{append .}}{{end}}
@ -73,8 +75,18 @@
</fieldset>
{{end}}
{{define "upload"}}
<fieldset><legend>clipboard</legend>
<form
>
<file></file>
</form>
</fieldset>
{{end}}
{{define "componet"}}
<fieldset><legend title="{{option .Meta "help"}}">{{option .Meta "help"}}({{option .Meta "command"}}.{{option .Meta "context"}})</legend>
{{$form_type := option . "form_type"|meta}}
<form class="option {{option .Meta "name"}}"
data-last_componet_group="{{option . "last_componet_group"|meta}}"
data-last_componet_order="{{option . "last_componet_order"|meta}}"
@ -82,6 +94,9 @@
data-componet_order="{{option . "componet_order"|meta}}"
data-componet_name="{{option . "name"|meta}}"
data-componet_help="{{option . "help"|meta}}"
{{if eq $form_type "upload"}}
method="POST" action="/upload" enctype="multipart/form-data"
{{end}}
>
<input style="display:none"></input>
{{range $index, $input := option . "inputs"}}
@ -89,6 +104,10 @@
{{$type := index $input "type"}}
{{if eq $type "button"}}
<input type="button" name="{{index $input "name"}}" value="{{index $input "value"}}" onclick="return onaction(event, 'command')">
{{else if eq $type "submit"}}
<input type="submit" value="{{index $input "value"}}">
{{else if eq $type "file"}}
<input type="file" name="{{index $input "name"}}">
{{else if eq $type "choice"}}
{{$default_value := index $input "value"}}
<label>{{index $input "label"}} : </label>
@ -104,7 +123,12 @@
</select>
{{else}}
<label>{{index $input "label"}} : </label>
<input name="{{index $input "name"}}" value="{{index $input "value"}}" onkeyup="return onaction(event, 'input')" list="clipstack">
<input
name="{{index $input "name"}}"
value="{{index $input "value"}}"
list="clipstack"
onclick="return onaction(event, 'click')"
onkeyup="return onaction(event, 'input')">
{{end}}
</div>
{{end}}