1
0
forked from x/ContextOS

mac add web.login

This commit is contained in:
shaoying 2018-05-28 22:34:14 +08:00
parent ec0b608aa4
commit 885b787b3d
5 changed files with 296 additions and 89 deletions

View File

@ -2311,7 +2311,7 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
"right": &Command{
Name: "right [share|add|del group [cache|config|command item]]",
Help: "用户组管理,查看、添加、删除用户组或是接口",
Formats: map[string]int{"add": 0, "del": 0, "cache": 0, "config": 0, "command": 0},
Formats: map[string]int{"check": 0, "add": 0, "del": 0, "cache": 0, "config": 0, "command": 0},
Hand: func(m *Message, c *Context, key string, arg ...string) {
index := m.Target().Index // {{{
if index == nil {
@ -2356,6 +2356,33 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
}
switch {
case m.Has("check"):
if group != nil {
switch {
case m.Has("cache"):
if _, ok := group.Caches[item]; ok {
m.Echo("ok")
}
case m.Has("config"):
if _, ok := group.Configs[item]; ok {
m.Echo("ok")
}
case m.Has("command"):
if x, ok := group.Commands[item]; ok {
if len(arg) > 2 {
if len(x.Options) > 0 {
for i := 2; i < len(arg)-1; i += 2 {
if x, ok := x.Options[arg[i]]; !ok || x != arg[i+1] {
return
}
}
}
}
m.Echo("ok")
}
}
}
return
case m.Has("add"):
if group == nil {
if _, ok := index[arg[0]]; ok {
@ -2380,11 +2407,18 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
group.Configs[item] = x
}
case m.Has("command"):
if x, ok := current.Commands[item]; ok {
if _, ok := current.Commands[item]; ok {
if group.Commands == nil {
group.Commands = map[string]*Command{}
}
group.Commands[item] = x
// group.Commands[item] = x
options := map[string]string{}
for i := 2; i < len(arg)-1; i += 2 {
options[arg[i]] = arg[i+1]
}
group.Commands[item] = &Command{
Options: options,
}
}
}

View File

@ -362,16 +362,17 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
"count": &ctx.Cache{Name: "count", Value: "0", Help: "主机协议"},
},
Configs: map[string]*ctx.Config{
"protocol": &ctx.Config{Name: "protocol", Value: "", Help: "主机协议"},
"hostname": &ctx.Config{Name: "hostname", Value: "", Help: "主机地址"},
"port": &ctx.Config{Name: "port", Value: "", Help: "主机端口"},
"dir": &ctx.Config{Name: "dir", Value: "/", Help: "主机路由"},
"file": &ctx.Config{Name: "file", Value: "", Help: "主机文件"},
"query": &ctx.Config{Name: "query", Value: "", Help: "主机参数"},
"output": &ctx.Config{Name: "output", Value: "stdout", Help: "响应输出"},
"editor": &ctx.Config{Name: "editor", Value: "vim", Help: "响应编辑器"},
"upload_tpl": &ctx.Config{Name: "upload_tpl", Value: "usr/template/upload.html", Help: "上传文件路径"},
"travel_tpl": &ctx.Config{Name: "travel_tpl", Value: "usr/template/travel.html", Help: "上传文件路径"},
"protocol": &ctx.Config{Name: "protocol", Value: "", Help: "主机协议"},
"hostname": &ctx.Config{Name: "hostname", Value: "", Help: "主机地址"},
"port": &ctx.Config{Name: "port", Value: "", Help: "主机端口"},
"dir": &ctx.Config{Name: "dir", Value: "/", Help: "主机路由"},
"file": &ctx.Config{Name: "file", Value: "", Help: "主机文件"},
"query": &ctx.Config{Name: "query", Value: "", Help: "主机参数"},
"output": &ctx.Config{Name: "output", Value: "stdout", Help: "响应输出"},
"editor": &ctx.Config{Name: "editor", Value: "vim", Help: "响应编辑器"},
"upload_tpl": &ctx.Config{Name: "upload_tpl", Value: "upload.html", Help: "上传文件路径"},
"travel_tpl": &ctx.Config{Name: "travel_tpl", Value: "travel.html", Help: "上传文件路径"},
"template_dir": &ctx.Config{Name: "template_dir", Value: "usr/template/", Help: "上传文件路径"},
},
Commands: map[string]*ctx.Command{
"serve": &ctx.Command{Name: "serve [directory [address [protocol]]]", Help: "开启应用服务", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
@ -606,8 +607,38 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
}
dir := m.Option("file")
if !m.Options("username") {
m.Option("username", "")
}
m.Option("right", "")
if !m.Options("sessid") {
m.Option("sessid", "")
m.Option("message", "please login")
} else {
msg := m.Find("aaa").Cmd("login", m.Option("sessid"))
if msg.Result(0) == "error: " {
m.Option("sessid", "")
m.Option("message", "login failure")
} else {
m.Option("username", msg.Result(0))
msg = m.Spawn(m.Target())
msg.Cmd("right", "check", msg.Cap("group"), "command", "/upload", "file", dir)
if msg.Result(0) == "ok" {
m.Option("right", "ok")
} else {
m.Option("message", "your do not have the right of", dir)
}
}
}
if m.Option("method") == "POST" {
if m.Options("content") {
if m.Options("shareto") {
msg := m.Spawn(m.Target())
msg.Cmd("right", "add", m.Option("shareto"), "command", "/upload", "file", m.Option("sharefile"))
m.Append("link", "hello")
return
} else if m.Options("content") {
name := path.Join(dir, m.Option("filename"))
if _, e := os.Stat(name); e != nil {
f, e := os.Create(name)
@ -639,14 +670,6 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
}
}
switch m.Option("cmd") {
case "git":
branch := m.Find("nfs").Cmd("git", "-C", dir, "branch")
m.Option("branch", branch.Result(0))
status := m.Find("nfs").Cmd("git", "-C", dir, "status")
m.Option("status", status.Result(0))
}
file := m.Option("file")
s, e := os.Stat(file)
if m.Assert(e); !s.IsDir() {
@ -707,7 +730,54 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
}
w.Header().Add("Content-Type", "text/html")
m.Assert(template.Must(template.ParseGlob(m.Conf("upload_tpl"))).Execute(w, m.Meta))
tmpl := template.Must(template.Must(template.ParseGlob(m.Conf("template_dir") + "/common/*.html")).ParseGlob(m.Conf("template_dir") + "upload.html"))
m.Assert(tmpl)
tmpl.ExecuteTemplate(w, "head", m.Meta)
if m.Options("right") {
tmpl.ExecuteTemplate(w, "userinfo", m.Meta)
msg := m.Spawn(m.Target())
for k, v := range msg.Target().Index {
for i, j := range v.Commands {
for v, n := range j.Options {
if n == dir {
msg.Add("append", "group", k)
msg.Add("append", "command", i)
msg.Add("append", "argument", v)
msg.Add("append", "value", n)
m.Log("fuck", nil, "why %v", msg.Meta)
}
}
}
}
tmpl.ExecuteTemplate(w, "share", msg.Meta)
tmpl.ExecuteTemplate(w, "upload", m.Meta)
} else {
tmpl.ExecuteTemplate(w, "login", m.Meta)
}
switch m.Option("cmd") {
case "git":
branch := m.Find("nfs").Cmd("git", "-C", dir, "branch")
m.Option("branch", branch.Result(0))
status := m.Find("nfs").Cmd("git", "-C", dir, "status")
m.Option("status", status.Result(0))
tmpl.ExecuteTemplate(w, "git", m.Meta)
}
if m.Options("message") {
tmpl.ExecuteTemplate(w, "message", m.Meta)
}
tmpl.ExecuteTemplate(w, "list", m.Meta)
if m.Options("right") {
tmpl.ExecuteTemplate(w, "create", m.Meta)
}
tmpl.ExecuteTemplate(w, "tail", m.Meta)
delete(m.Meta, "result")
delete(m.Meta, "append")
// }}}
@ -770,7 +840,10 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
}
w.Header().Add("Content-Type", "text/html")
m.Assert(template.Must(template.ParseGlob(m.Conf("travel_tpl"))).ExecuteTemplate(w, "head", m.Meta))
tmpl := template.Must(template.Must(template.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"} {
@ -789,17 +862,21 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
if len(msg.Meta["append"]) > 0 {
msg.Option("current_module", module)
msg.Option("current_domain", m.Option("domain"))
m.Log("fuck", nil, "fuck %v", msg.Meta)
m.Assert(template.Must(template.ParseGlob(m.Conf("travel_tpl"))).ExecuteTemplate(w, v, msg.Meta))
m.Assert(tmpl.ExecuteTemplate(w, v, msg.Meta))
}
}
}
m.Assert(template.Must(template.ParseGlob(m.Conf("travel_tpl"))).ExecuteTemplate(w, "tail", m.Meta))
m.Assert(tmpl.ExecuteTemplate(w, "tail", m.Meta))
delete(m.Meta, "result")
delete(m.Meta, "append")
// }}}
}},
"/login": &ctx.Command{Name: "/login", Help: "文件上传", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
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)})
}},
"temp": &ctx.Command{Name: "temp", Help: "应用示例", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
msg := m.Spawn(m.Target())
question := []string{}

View File

@ -30,7 +30,7 @@ ctx = {
}
}
} else if (value == undefined) {
return args[key];
return args[key] || "";
} else {
args[key] = value;
}
@ -77,6 +77,9 @@ ctx = {
console.log("POST: "+url+"?"+arg);
xhr.send(arg);
},//}}}
Refresh: function() {//{{{
location.assign(location.href);
},//}}}
Cap: function(cap, cb) {//{{{
if (typeof cap == "function") {

View File

@ -0,0 +1,85 @@
{{define "login"}}
<fieldset><legend>login</legend>
<form method="POST" action="/login" onsubmit="return login(event)">
<label>username:</label><input type="text" name="username" value="{{index .username 0}}" required autofocus>
<label>password:</label><input type="password" name="password" required>
<input type="submit">
</form>
</fieldset>
<script>
function login(event) {
var fields = event.target.elements;
ctx.POST("/login", {
username: fields["username"].value,
password: fields["password"].value
}, function(msg) {
if (ctx.Cookie("sessid")) {
ctx.Refresh()
}
})
return false;
}
</script>
{{end}}
{{define "userinfo"}}
<fieldset><legend>userinfo</legend>
welcome {{index .username 0}}
<button onclick="return logout(event)">logout</button>
</fieldset>
<script>
function logout(event) {
ctx.Cookie("sessid", "");
ctx.Refresh();
return false;
}
</script>
{{end}}
{{define "share"}}
<fieldset><legend>share</legend>
<form method="POST" onsubmit="return share(event)">
<label>share to:</label><input type="text" name="shareto" required>
<input type="submit">
</form>
<table>
<colgroup>
{{range .append}}
<col class="{{.}}">
{{end}}
</colgroup>
<tr>
{{range .append}}
<th class="{{.}}">{{.}}</th>
{{end}}
</tr>
{{$meta := .}}
{{if .append}}
{{$first := index .append 0}}
{{range $i, $k := index . $first}}
<tr>
{{range $key := index $meta "append"}}
<td class="{{$key}}">
<code>{{index $meta $key $i}}</code>
</td>
{{end}}
</tr>
{{end}}
{{end}}
</table>
</fieldset>
<script>
function share(event) {
var fields = event.target.elements;
var shareto = fields["shareto"].value
ctx.POST("/upload", {
shareto: shareto,
sharefile: file,
}, function(msg) {
ctx.Refresh();
})
return false;
}
</script>
{{end}}

View File

@ -1,3 +1,4 @@
{{define "head"}}
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
@ -35,79 +36,83 @@
}
</style>
</head>
<body>
{{if index . "cmd"}}
{{$cmd := index . "cmd" 0}}
{{if eq $cmd "git"}}
<fieldset><legend>branch</legend>
<pre>{{index . "branch"}}</pre>
</fieldset>
<fieldset><legend>status</legend>
<pre>{{index . "status"}}</pre>
</fieldset>
{{end}}
{{end}}
<fieldset><legend>upload</legend>
<form method="POST" action="/upload" enctype="multipart/form-data">
<input type="file" name="file"><input type="submit">
</form>
</fieldset>
{{define "git"}}
<fieldset><legend>branch</legend>
<pre>{{index . "branch"}}</pre>
</fieldset>
<fieldset><legend>status</legend>
<pre>{{index . "status"}}</pre>
</fieldset>
{{end}}
{{if index . "message" 0}}
{{define "upload"}}
<fieldset><legend>upload</legend>
<form method="POST" action="/upload" enctype="multipart/form-data">
<input type="file" name="file"><input type="submit">
</form>
</fieldset>
{{end}}
{{define "message"}}
<fieldset class="message"><legend>message</legend>
<pre>{{index . "message"}}</pre>
</fieldset>
{{end}}
<fieldset><legend><input type="text" id="dir" value="{{index .file 0}}" onkeydown="return Change(event)"></legend>
<table>
<colgroup>
{{range .append}}
<col class="{{.}}">
{{end}}
</colgroup>
<tr>
{{$sort := index . "sort" 0}}
{{$reverse := index . "reverse" 0}}
{{range .append}}
{{if eq $sort .}}
<th class="{{.}} sort" onclick="list('{{.}}')">{{.}}</th>
{{else if eq $reverse .}}
<th class="{{.}} reverse" onclick="list('{{.}}')">{{.}}</th>
{{else if eq . "path"}}
{{else}}
<th class="{{.}}" onclick="list('{{.}}')">{{.}}</th>
{{define "list"}}
<fieldset><legend><input type="text" id="dir" value="{{index .file 0}}" onkeydown="return Change(event)"></legend>
<table>
<colgroup>
{{range .append}}
<col class="{{.}}">
{{end}}
{{end}}
</tr>
{{$meta := .}}
{{if .append}}
{{$first := index .append 0}}
{{range $i, $k := index . $first}}
<tr>
{{range $key := index $meta "append"}}
{{if eq $key "name"}}
<td class="{{$key}}">
<a href="/upload?file={{index $meta "path" $i}}"><code>{{index $meta $key $i}}</code></a>
</td>
{{else if eq $key "path"}}
{{else}}
<td class="{{$key}}">
<code>{{index $meta $key $i}}</code>
</td>
</colgroup>
<tr>
{{$sort := index . "sort" 0}}
{{$reverse := index . "reverse" 0}}
{{range .append}}
{{if eq $sort .}}
<th class="{{.}} sort" onclick="list('{{.}}')">{{.}}</th>
{{else if eq $reverse .}}
<th class="{{.}} reverse" onclick="list('{{.}}')">{{.}}</th>
{{else if eq . "path"}}
{{else}}
<th class="{{.}}" onclick="list('{{.}}')">{{.}}</th>
{{end}}
{{end}}
{{end}}
</tr>
{{end}}
{{end}}
</table>
</fieldset>
</tr>
<script>
var file = '{{index .file 0}}';
</script>
{{$meta := .}}
{{if .append}}
{{$first := index .append 0}}
{{range $i, $k := index . $first}}
<tr>
{{range $key := index $meta "append"}}
{{if eq $key "name"}}
<td class="{{$key}}">
<a href="/upload?file={{index $meta "path" $i}}"><code>{{index $meta $key $i}}</code></a>
</td>
{{else if eq $key "path"}}
{{else}}
<td class="{{$key}}">
<code>{{index $meta $key $i}}</code>
</td>
{{end}}
{{end}}
</tr>
{{end}}
{{end}}
</table>
</fieldset>
{{end}}
{{define "create"}}
<fieldset><legend>create</legend>
<form method="POST" action="/upload">
<table>
@ -116,7 +121,9 @@
</table>
</form>
</fieldset>
{{end}}
{{define "tail"}}
<script src="library/context.js"></script>
<script>
function Change(event) {
@ -132,3 +139,4 @@
}
</script>
</body>
{{end}}