This commit is contained in:
parent
36e55281c7
commit
4abad1ec71
|
@ -1,3 +1,4 @@
|
||||||
|
// Wide 配置相关,所有配置(包括用户配置)都是保存在 wide.json 中.
|
||||||
package conf
|
package conf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
1
main.go
1
main.go
|
@ -93,6 +93,7 @@ func main() {
|
||||||
|
|
||||||
// Shell
|
// Shell
|
||||||
http.HandleFunc("/shell/ws", shell.WSHandler)
|
http.HandleFunc("/shell/ws", shell.WSHandler)
|
||||||
|
http.HandleFunc("/shell", shell.IndexHandler)
|
||||||
|
|
||||||
// 用户
|
// 用户
|
||||||
http.HandleFunc("/user/new", user.AddUser)
|
http.HandleFunc("/user/new", user.AddUser)
|
||||||
|
|
|
@ -2,19 +2,58 @@ package shell
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/b3log/wide/conf"
|
"github.com/b3log/wide/conf"
|
||||||
|
"github.com/b3log/wide/i18n"
|
||||||
"github.com/b3log/wide/user"
|
"github.com/b3log/wide/user"
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
"html/template"
|
||||||
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"runtime"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var shellWS = map[string]*websocket.Conn{}
|
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) {
|
func WSHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
session, _ := user.Session.Get(r, "wide-session")
|
session, _ := user.Session.Get(r, "wide-session")
|
||||||
|
username := session.Values["username"].(string)
|
||||||
sid := session.Values["id"].(string)
|
sid := session.Values["id"].(string)
|
||||||
|
|
||||||
shellWS[sid], _ = websocket.Upgrade(w, r, nil, 1024, 1024)
|
shellWS[sid], _ = websocket.Upgrade(w, r, nil, 1024, 1024)
|
||||||
|
@ -58,7 +97,7 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
output := ""
|
output := ""
|
||||||
if !strings.Contains(inputCmd, "clear") {
|
if !strings.Contains(inputCmd, "clear") {
|
||||||
output = pipeCommands(commands...)
|
output = pipeCommands(username, commands...)
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = map[string]interface{}{"output": output, "cmd": "shell-output"}
|
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] {
|
for i, command := range commands[:len(commands)-1] {
|
||||||
setCmdEnv(command)
|
setCmdEnv(command, username)
|
||||||
|
|
||||||
out, err := command.StdoutPipe()
|
out, err := command.StdoutPipe()
|
||||||
|
|
||||||
|
@ -85,9 +124,10 @@ func pipeCommands(commands ...*exec.Cmd) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
last := commands[len(commands)-1]
|
last := commands[len(commands)-1]
|
||||||
setCmdEnv(last)
|
setCmdEnv(last, username)
|
||||||
|
|
||||||
out, err := last.Output()
|
out, err := last.Output()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err.Error()
|
return err.Error()
|
||||||
}
|
}
|
||||||
|
@ -95,10 +135,16 @@ func pipeCommands(commands ...*exec.Cmd) string {
|
||||||
return string(out)
|
return string(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
func setCmdEnv(cmd *exec.Cmd) {
|
func setCmdEnv(cmd *exec.Cmd, username string) {
|
||||||
// TODO: 使用用户自己的仓库路径设置 GOPATH
|
userWorkspace := conf.Wide.GetUserWorkspace(username)
|
||||||
cmd.Env = append(cmd.Env, "TERM=xterm", "GOPATH="+conf.Wide.Workspace,
|
|
||||||
"GOROOT="+os.Getenv("GOROOT"))
|
|
||||||
|
|
||||||
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();
|
||||||
|
});
|
|
@ -16,8 +16,8 @@ outputWS.onmessage = function(e) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else if ('go get' === data.cmd) {
|
} else if ('go get' === data.cmd) {
|
||||||
$('#output').text($('#output').text() + data.output);
|
$('#output').text($('#output').text() + data.output);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ('build' === data.cmd) {
|
if ('build' === data.cmd) {
|
||||||
if ('run' === data.nextCmd) {
|
if ('run' === data.nextCmd) {
|
||||||
|
@ -48,29 +48,10 @@ outputWS.onerror = function(e) {
|
||||||
console.log('[output onerror] ' + 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 = {
|
var wide = {
|
||||||
curNode: undefined,
|
curNode: undefined,
|
||||||
curEditor: undefined,
|
curEditor: undefined,
|
||||||
_initLayout: function () {
|
_initLayout: function() {
|
||||||
var mainH = $(window).height() - $(".menu").height() - $(".footer").height() - 2;
|
var mainH = $(window).height() - $(".menu").height() - $(".footer").height() - 2;
|
||||||
$(".content, .ztree").height(mainH);
|
$(".content, .ztree").height(mainH);
|
||||||
|
|
||||||
|
@ -78,15 +59,6 @@ var wide = {
|
||||||
},
|
},
|
||||||
init: function() {
|
init: function() {
|
||||||
this._initLayout();
|
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) {
|
$("body").bind("mousedown", function(event) {
|
||||||
if (!(event.target.id === "dirRMenu" || $(event.target).closest("#dirRMenu").length > 0)) {
|
if (!(event.target.id === "dirRMenu" || $(event.target).closest("#dirRMenu").length > 0)) {
|
||||||
|
@ -131,7 +103,7 @@ var wide = {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
goget: function() {
|
goget: function() {
|
||||||
var request = {
|
var request = {
|
||||||
"file": wide.curNode.path
|
"file": wide.curNode.path
|
||||||
};
|
};
|
||||||
|
|
|
@ -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