This commit is contained in:
parent
36e55281c7
commit
4abad1ec71
|
@ -1,3 +1,4 @@
|
|||
// Wide 配置相关,所有配置(包括用户配置)都是保存在 wide.json 中.
|
||||
package conf
|
||||
|
||||
import (
|
||||
|
|
1
main.go
1
main.go
|
@ -93,6 +93,7 @@ func main() {
|
|||
|
||||
// Shell
|
||||
http.HandleFunc("/shell/ws", shell.WSHandler)
|
||||
http.HandleFunc("/shell", shell.IndexHandler)
|
||||
|
||||
// 用户
|
||||
http.HandleFunc("/user/new", user.AddUser)
|
||||
|
|
|
@ -2,19 +2,58 @@ package shell
|
|||
|
||||
import (
|
||||
"github.com/b3log/wide/conf"
|
||||
"github.com/b3log/wide/i18n"
|
||||
"github.com/b3log/wide/user"
|
||||
"github.com/golang/glog"
|
||||
"github.com/gorilla/websocket"
|
||||
"html/template"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var shellWS = map[string]*websocket.Conn{}
|
||||
|
||||
func IndexHandler(w http.ResponseWriter, r *http.Request) {
|
||||
i18n.Load()
|
||||
|
||||
model := map[string]interface{}{"Wide": conf.Wide, "i18n": i18n.GetLangs(r), "locale": i18n.GetLocale(r)}
|
||||
|
||||
session, _ := user.Session.Get(r, "wide-session")
|
||||
|
||||
if session.IsNew {
|
||||
// TODO: 写死以 admin 作为用户登录
|
||||
name := conf.Wide.Users[0].Name
|
||||
|
||||
session.Values["username"] = name
|
||||
session.Values["id"] = strconv.Itoa(rand.Int())
|
||||
// 一天过期
|
||||
session.Options.MaxAge = 60 * 60 * 24
|
||||
|
||||
glog.Infof("Created a session [%s] for user [%s]", session.Values["id"].(string), name)
|
||||
}
|
||||
|
||||
session.Save(r, w)
|
||||
|
||||
t, err := template.ParseFiles("view/shell.html")
|
||||
|
||||
if nil != err {
|
||||
glog.Error(err)
|
||||
http.Error(w, err.Error(), 500)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
t.Execute(w, model)
|
||||
}
|
||||
|
||||
func WSHandler(w http.ResponseWriter, r *http.Request) {
|
||||
session, _ := user.Session.Get(r, "wide-session")
|
||||
username := session.Values["username"].(string)
|
||||
sid := session.Values["id"].(string)
|
||||
|
||||
shellWS[sid], _ = websocket.Upgrade(w, r, nil, 1024, 1024)
|
||||
|
@ -58,7 +97,7 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
output := ""
|
||||
if !strings.Contains(inputCmd, "clear") {
|
||||
output = pipeCommands(commands...)
|
||||
output = pipeCommands(username, commands...)
|
||||
}
|
||||
|
||||
ret = map[string]interface{}{"output": output, "cmd": "shell-output"}
|
||||
|
@ -70,9 +109,9 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
func pipeCommands(commands ...*exec.Cmd) string {
|
||||
func pipeCommands(username string, commands ...*exec.Cmd) string {
|
||||
for i, command := range commands[:len(commands)-1] {
|
||||
setCmdEnv(command)
|
||||
setCmdEnv(command, username)
|
||||
|
||||
out, err := command.StdoutPipe()
|
||||
|
||||
|
@ -85,9 +124,10 @@ func pipeCommands(commands ...*exec.Cmd) string {
|
|||
}
|
||||
|
||||
last := commands[len(commands)-1]
|
||||
setCmdEnv(last)
|
||||
setCmdEnv(last, username)
|
||||
|
||||
out, err := last.Output()
|
||||
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
|
@ -95,10 +135,16 @@ func pipeCommands(commands ...*exec.Cmd) string {
|
|||
return string(out)
|
||||
}
|
||||
|
||||
func setCmdEnv(cmd *exec.Cmd) {
|
||||
// TODO: 使用用户自己的仓库路径设置 GOPATH
|
||||
cmd.Env = append(cmd.Env, "TERM=xterm", "GOPATH="+conf.Wide.Workspace,
|
||||
"GOROOT="+os.Getenv("GOROOT"))
|
||||
func setCmdEnv(cmd *exec.Cmd, username string) {
|
||||
userWorkspace := conf.Wide.GetUserWorkspace(username)
|
||||
|
||||
cmd.Dir = conf.Wide.Workspace
|
||||
cmd.Env = append(cmd.Env,
|
||||
"TERM=xterm",
|
||||
"GOPATH="+userWorkspace,
|
||||
"GOOS="+runtime.GOOS,
|
||||
"GOARCH="+runtime.GOARCH,
|
||||
"GOROOT="+runtime.GOROOT(),
|
||||
"PATH="+os.Getenv("PATH"))
|
||||
|
||||
cmd.Dir = userWorkspace
|
||||
}
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
#shellOutput, #shellInput {
|
||||
width: 100%;
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
var shellWS = new WebSocket(config.channel.shell + '/shell/ws');
|
||||
shellWS.onopen = function() {
|
||||
console.log('[shell onopen] connected');
|
||||
};
|
||||
shellWS.onmessage = function(e) {
|
||||
console.log('[shell onmessage]' + e.data);
|
||||
var data = JSON.parse(e.data);
|
||||
if ('init-shell' !== data.cmd) {
|
||||
$('#shellOutput').val(data.output);
|
||||
}
|
||||
};
|
||||
shellWS.onclose = function(e) {
|
||||
console.log('[shell onclose] disconnected (' + e.code + ')');
|
||||
delete shellWS;
|
||||
};
|
||||
shellWS.onerror = function(e) {
|
||||
console.log('[shell onerror] ' + e);
|
||||
};
|
||||
|
||||
var shell = {
|
||||
init: function() {
|
||||
$('#shellInput').keydown(function(event) {
|
||||
if (13 === event.which) {
|
||||
var input = {
|
||||
cmd: $('#shellInput').val()
|
||||
};
|
||||
shellWS.send(JSON.stringify(input));
|
||||
$('#shellInput').val('');
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
$(document).ready(function() {
|
||||
shell.init();
|
||||
});
|
|
@ -48,25 +48,6 @@ outputWS.onerror = function(e) {
|
|||
console.log('[output onerror] ' + e);
|
||||
};
|
||||
|
||||
var shellWS = new WebSocket(config.channel.shell + '/shell/ws');
|
||||
shellWS.onopen = function() {
|
||||
console.log('[shell onopen] connected');
|
||||
};
|
||||
shellWS.onmessage = function(e) {
|
||||
console.log('[shell onmessage]' + e.data);
|
||||
var data = JSON.parse(e.data);
|
||||
if ('init-shell' !== data.cmd) {
|
||||
$('#shellOutput').val(data.output);
|
||||
}
|
||||
};
|
||||
shellWS.onclose = function(e) {
|
||||
console.log('[shell onclose] disconnected (' + e.code + ')');
|
||||
delete shellWS;
|
||||
};
|
||||
shellWS.onerror = function(e) {
|
||||
console.log('[shell onerror] ' + e);
|
||||
};
|
||||
|
||||
var wide = {
|
||||
curNode: undefined,
|
||||
curEditor: undefined,
|
||||
|
@ -78,15 +59,6 @@ var wide = {
|
|||
},
|
||||
init: function() {
|
||||
this._initLayout();
|
||||
$('#shellInput').keydown(function(event) {
|
||||
if (13 === event.which) {
|
||||
var input = {
|
||||
cmd: $('#shellInput').val()
|
||||
};
|
||||
shellWS.send(JSON.stringify(input));
|
||||
$('#shellInput').val('');
|
||||
}
|
||||
});
|
||||
|
||||
$("body").bind("mousedown", function(event) {
|
||||
if (!(event.target.id === "dirRMenu" || $(event.target).closest("#dirRMenu").length > 0)) {
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>{{.i18n.wide}}</title>
|
||||
<link rel="stylesheet" href="{{.Wide.StaticServer}}/static/css/shell.css?{{.Wide.StaticResourceVersion}}">
|
||||
</head>
|
||||
<body>
|
||||
<input id="shellInput" />
|
||||
|
||||
<div>
|
||||
<textarea id="shellOutput" rows="10" ></textarea>
|
||||
</div>
|
||||
|
||||
<script type="text/javascript">
|
||||
var config = {
|
||||
channel: {
|
||||
shell: '{{.Wide.ShellChannel}}'
|
||||
}
|
||||
};
|
||||
</script>
|
||||
<script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/jquery-2.1.1.min.js"></script>
|
||||
<script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/reconnecting-websocket.js"></script>
|
||||
|
||||
|
||||
<script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/shell.js?{{.Wide.StaticResourceVersion}}"></script>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue