This commit is contained in:
Liang Ding 2015-11-24 17:39:35 +08:00
parent 0ee3704a5b
commit f3dadc4795
16 changed files with 127 additions and 111 deletions

View File

@ -78,6 +78,11 @@ func GoFmtHandler(w http.ResponseWriter, r *http.Request) {
return
}
data := map[string]interface{}{}
data["code"] = code
result.Data = data
fmt := conf.GetGoFmt(username)
argv := []string{filePath}
@ -88,13 +93,14 @@ func GoFmtHandler(w http.ResponseWriter, r *http.Request) {
if "" == output {
// format error, returns the original content
result.Succ = true
result.Code = code
return
}
code = string(output)
result.Code = code
data["code"] = code
result.Data = data
fout, err = os.Create(filePath)
fout.WriteString(code)

View File

@ -32,8 +32,8 @@ import (
// GoTestHandler handles request of go test.
func GoTestHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
result := util.NewResult()
defer util.RetResult(w, r, result)
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
if httpSession.IsNew {
@ -48,7 +48,7 @@ func GoTestHandler(w http.ResponseWriter, r *http.Request) {
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
logger.Error(err)
data["succ"] = false
result.Succ = false
return
}
@ -66,7 +66,7 @@ func GoTestHandler(w http.ResponseWriter, r *http.Request) {
stdout, err := cmd.StdoutPipe()
if nil != err {
logger.Error(err)
data["succ"] = false
result.Succ = false
return
}
@ -74,12 +74,12 @@ func GoTestHandler(w http.ResponseWriter, r *http.Request) {
stderr, err := cmd.StderrPipe()
if nil != err {
logger.Error(err)
data["succ"] = false
result.Succ = false
return
}
if !data["succ"].(bool) {
if !result.Succ {
return
}
@ -106,7 +106,7 @@ func GoTestHandler(w http.ResponseWriter, r *http.Request) {
if err := cmd.Start(); nil != err {
logger.Error(err)
data["succ"] = false
result.Succ = false
return
}

View File

@ -32,8 +32,8 @@ import (
// GoVetHandler handles request of go vet.
func GoVetHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
result := util.NewResult()
defer util.RetResult(w, r, result)
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
if httpSession.IsNew {
@ -48,7 +48,7 @@ func GoVetHandler(w http.ResponseWriter, r *http.Request) {
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
logger.Error(err)
data["succ"] = false
result.Succ = false
return
}
@ -66,7 +66,7 @@ func GoVetHandler(w http.ResponseWriter, r *http.Request) {
stdout, err := cmd.StdoutPipe()
if nil != err {
logger.Error(err)
data["succ"] = false
result.Succ = false
return
}
@ -74,12 +74,12 @@ func GoVetHandler(w http.ResponseWriter, r *http.Request) {
stderr, err := cmd.StderrPipe()
if nil != err {
logger.Error(err)
data["succ"] = false
result.Succ = false
return
}
if !data["succ"].(bool) {
if !result.Succ {
return
}
@ -106,7 +106,7 @@ func GoVetHandler(w http.ResponseWriter, r *http.Request) {
if err := cmd.Start(); nil != err {
logger.Error(err)
data["succ"] = false
result.Succ = false
return
}

View File

@ -29,8 +29,8 @@ import (
// BuildHandler handles request of Playground building.
func BuildHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
result := util.NewResult()
defer util.RetResult(w, r, result)
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
if httpSession.IsNew {
@ -42,7 +42,7 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
var args map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
logger.Error(err)
data["succ"] = false
result.Succ = false
return
}
@ -55,18 +55,22 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
suffix = ".exe"
}
data := map[string]interface{}{}
executable := filepath.Clean(conf.Wide.Playground + "/" + strings.Replace(fileName, ".go", suffix, -1))
cmd := exec.Command("go", "build", "-o", executable, filePath)
out, err := cmd.CombinedOutput()
data["output"] = template.HTML(string(out))
if nil != err {
data["succ"] = false
result.Succ = false
return
}
data["executable"] = executable
result.Data = data
}

View File

@ -32,8 +32,8 @@ import (
// SaveHandler handles request of Playground code save.
func SaveHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
result := util.NewResult()
defer util.RetResult(w, r, result)
session, _ := session.HTTPSession.Get(r, "wide-session")
if session.IsNew {
@ -45,7 +45,7 @@ func SaveHandler(w http.ResponseWriter, r *http.Request) {
var args map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
logger.Error(err)
data["succ"] = false
result.Succ = false
return
}
@ -58,7 +58,7 @@ func SaveHandler(w http.ResponseWriter, r *http.Request) {
stdin, err := cmd.StdinPipe()
if nil != err {
logger.Error(err)
data["succ"] = false
result.Succ = false
return
}
@ -72,6 +72,8 @@ func SaveHandler(w http.ResponseWriter, r *http.Request) {
code = string(output)
}
data := map[string]interface{}{}
data["code"] = code
// Step2. generate file name
@ -87,16 +89,18 @@ func SaveHandler(w http.ResponseWriter, r *http.Request) {
fout.WriteString(code)
if err := fout.Close(); nil != err {
logger.Error(err)
data["succ"] = false
result.Succ = false
return
}
result.Data = data
}
// ShortURLHandler handles request of short URL.
func ShortURLHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
result := util.NewResult()
defer util.RetResult(w, r, result)
session, _ := session.HTTPSession.Get(r, "wide-session")
if session.IsNew {
@ -108,7 +112,7 @@ func ShortURLHandler(w http.ResponseWriter, r *http.Request) {
var args map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
logger.Error(err)
data["succ"] = false
result.Succ = false
return
}
@ -121,7 +125,7 @@ func ShortURLHandler(w http.ResponseWriter, r *http.Request) {
var response map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
logger.Error(err)
data["succ"] = false
result.Succ = false
return
}
@ -131,5 +135,5 @@ func ShortURLHandler(w http.ResponseWriter, r *http.Request) {
shortURL = response["tinyurl"].(string)
}
data["shortURL"] = shortURL
result.Data = shortURL
}

View File

@ -40,20 +40,20 @@ type outputBuf struct {
// RunHandler handles request of executing a binary file.
func RunHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
result := util.NewResult()
defer util.RetResult(w, r, result)
var args map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
logger.Error(err)
data["succ"] = false
result.Succ = false
}
sid := args["sid"].(string)
wSession := session.WideSessions.Get(sid)
if nil == wSession {
data["succ"] = false
result.Succ = false
}
filePath := args["executable"].(string)
@ -67,13 +67,13 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
stdout, err := cmd.StdoutPipe()
if nil != err {
logger.Error(err)
data["succ"] = false
result.Succ = false
}
stderr, err := cmd.StderrPipe()
if nil != err {
logger.Error(err)
data["succ"] = false
result.Succ = false
}
outReader := bufio.NewReader(stdout)
@ -81,14 +81,14 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
if err := cmd.Start(); nil != err {
logger.Error(err)
data["succ"] = false
result.Succ = false
}
wsChannel := session.PlaygroundWS[sid]
channelRet := map[string]interface{}{}
if !data["succ"].(bool) {
if !result.Succ {
if nil != wsChannel {
channelRet["cmd"] = "run-done"
channelRet["output"] = ""
@ -227,13 +227,13 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
// StopHandler handles request of stoping a running process.
func StopHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
result := util.NewResult()
defer util.RetResult(w, r, result)
var args map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
logger.Error(err)
data["succ"] = false
result.Succ = false
return
}
@ -243,7 +243,7 @@ func StopHandler(w http.ResponseWriter, r *http.Request) {
wSession := session.WideSessions.Get(sid)
if nil == wSession {
data["succ"] = false
result.Succ = false
return
}

View File

@ -36,8 +36,8 @@ var logger = log.NewLogger(os.Stdout)
// Clone handles request of git clone.
func CloneHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
result := util.NewResult()
defer util.RetResult(w, r, result)
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
if httpSession.IsNew {
@ -51,7 +51,7 @@ func CloneHandler(w http.ResponseWriter, r *http.Request) {
var args map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
logger.Error(err)
data["succ"] = false
result.Succ = false
return
}
@ -66,7 +66,7 @@ func CloneHandler(w http.ResponseWriter, r *http.Request) {
stdout, err := cmd.StdoutPipe()
if nil != err {
logger.Error(err)
data["succ"] = false
result.Succ = false
return
}
@ -74,12 +74,12 @@ func CloneHandler(w http.ResponseWriter, r *http.Request) {
stderr, err := cmd.StderrPipe()
if nil != err {
logger.Error(err)
data["succ"] = false
result.Succ = false
return
}
if !data["succ"].(bool) {
if !result.Succ {
return
}
@ -106,7 +106,7 @@ func CloneHandler(w http.ResponseWriter, r *http.Request) {
if err := cmd.Start(); nil != err {
logger.Error(err)
data["succ"] = false
result.Succ = false
return
}

View File

@ -281,8 +281,8 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
// SaveContentHandler handles request of session content string.
func SaveContentHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
result := util.NewResult()
defer util.RetResult(w, r, result)
args := struct {
Sid string
@ -291,14 +291,14 @@ func SaveContentHandler(w http.ResponseWriter, r *http.Request) {
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
logger.Error(err)
data["succ"] = false
result.Succ = false
return
}
wSession := WideSessions.Get(args.Sid)
if nil == wSession {
data["succ"] = false
result.Succ = false
return
}

View File

@ -88,9 +88,8 @@ func PreferenceHandler(w http.ResponseWriter, r *http.Request) {
// non-GET request as save request
succ := true
data := map[string]interface{}{"succ": &succ}
defer util.RetJSON(w, r, data)
result := util.NewResult()
defer util.RetResult(w, r, result)
args := struct {
FontFamily string
@ -112,7 +111,7 @@ func PreferenceHandler(w http.ResponseWriter, r *http.Request) {
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
logger.Error(err)
succ = false
result.Succ = false
return
}
@ -146,7 +145,7 @@ func PreferenceHandler(w http.ResponseWriter, r *http.Request) {
user.Lived = now
user.Updated = now
succ = user.Save()
result.Succ = user.Save()
}
// LoginHandler handles request of user login.
@ -211,8 +210,8 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
// LogoutHandler handles request of user logout (exit).
func LogoutHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
result := util.NewResult()
defer util.RetResult(w, r, result)
httpSession, _ := HTTPSession.Get(r, "wide-session")
@ -248,15 +247,14 @@ func SignUpUserHandler(w http.ResponseWriter, r *http.Request) {
// non-GET request as add user request
succ := true
data := map[string]interface{}{"succ": &succ}
defer util.RetJSON(w, r, data)
result := util.NewResult()
defer util.RetResult(w, r, result)
var args map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
logger.Error(err)
succ = false
result.Succ = false
return
}
@ -267,8 +265,8 @@ func SignUpUserHandler(w http.ResponseWriter, r *http.Request) {
msg := addUser(username, password, email)
if userCreated != msg {
succ = false
data["msg"] = msg
result.Succ = false
result.Msg = msg
return
}

View File

@ -156,8 +156,8 @@ var menu = {
url: config.context + '/logout',
data: JSON.stringify(request),
dataType: "json",
success: function (data) {
if (data.succ) {
success: function (result) {
if (result.succ) {
window.location.href = "/login";
}
}
@ -454,8 +454,8 @@ var menu = {
type: 'POST',
url: config.context + '/preference',
data: JSON.stringify(request),
success: function (data, textStatus, jqXHR) {
if (!data.succ) {
success: function (result, textStatus, jqXHR) {
if (!result.succ) {
return false;
}

View File

@ -334,10 +334,12 @@ var playground = {
url: config.context + '/playground/save',
data: JSON.stringify(request),
dataType: "json",
success: function (data) {
success: function (result) {
var data = result.data;
playground.editor.setValue(data.code);
if (!data.succ) {
if (!result.succ) {
console.log(data);
return;
}
@ -351,9 +353,9 @@ var playground = {
url: config.context + '/playground/short-url',
data: JSON.stringify(request),
dataType: "json",
success: function (data) {
if (!data.succ) {
console.log(data);
success: function (result) {
if (!result.succ) {
console.log(result);
return;
}
@ -361,8 +363,8 @@ var playground = {
+ config.label.colon + '</label><a href="'
+ url + '" target="_blank">' + url + "</a><br/>";
html += '<label>' + config.label.short_url + config.label.colon
+ '</label><a href="' + data.shortURL + '" target="_blank">'
+ data.shortURL + '</a><br/>';
+ '</label><a href="' + result.data + '" target="_blank">'
+ result.data + '</a><br/>';
html += '<label>' + config.label.embeded + config.label.colon
+ '</label><br/><textarea rows="5" style="width:100%" readonly><iframe style="border:1px solid" src="'
+ url + '?embed=true" width="99%" height="600"></iframe></textarea>';
@ -434,12 +436,13 @@ var playground = {
url: config.context + '/playground/save',
data: JSON.stringify(request),
dataType: "json",
success: function (data) {
// console.log(data);
success: function (result) {
var data = result.data;
playground.editor.setValue(data.code);
playground.editor.setCursor(cursor);
if (!data.succ) {
if (!result.succ) {
return;
}
@ -452,12 +455,14 @@ var playground = {
url: config.context + '/playground/build',
data: JSON.stringify(request),
dataType: "json",
success: function (data) {
success: function (result) {
// console.log(data);
var data = result.data;
$("#output").val(data.output);
if (!data.succ) {
if (!result.succ) {
return;
}
@ -470,7 +475,7 @@ var playground = {
url: config.context + '/playground/run',
data: JSON.stringify(request),
dataType: "json",
success: function (data) {
success: function (result) {
// console.log(data);
}
});
@ -497,8 +502,8 @@ var playground = {
url: config.context + '/playground/save',
data: JSON.stringify(request),
dataType: "json",
success: function (data) {
playground.editor.setValue(data.code);
success: function (result) {
playground.editor.setValue(result.data.code);
playground.editor.setCursor(cursor);
}
});

View File

@ -44,7 +44,7 @@ var session = {
url: config.context + '/session/save',
data: JSON.stringify(request),
dataType: "json",
success: function (data) {
success: function (result) {
}
});
}, 30000);

View File

@ -201,9 +201,9 @@ var tree = {
url: config.context + '/file/decompress',
data: JSON.stringify(request),
dataType: "json",
success: function (data) {
if (!data.succ) {
$("#dialogAlert").dialog("open", data.msg);
success: function (result) {
if (!result.succ) {
$("#dialogAlert").dialog("open", result.msg);
return false;
}
@ -534,8 +534,8 @@ var tree = {
url: config.context + '/file/rename',
data: JSON.stringify(request),
dataType: "json",
success: function (data) {
if (!data.succ) {
success: function (result) {
if (!result.succ) {
$("#dialogRenamePrompt").dialog("close");
bottomGroup.tabs.setCurrent("notification");
windows.flowBottom();

View File

@ -117,8 +117,8 @@ var wide = {
url: config.context + '/file/remove',
data: JSON.stringify(request),
dataType: "json",
success: function (data) {
if (!data.succ) {
success: function (result) {
if (!result.succ) {
$("#dialogRemoveConfirm").dialog("close");
bottomGroup.tabs.setCurrent("notification");
windows.flowBottom();
@ -155,8 +155,8 @@ var wide = {
url: config.context + '/file/new',
data: JSON.stringify(request),
dataType: "json",
success: function (data) {
if (!data.succ) {
success: function (result) {
if (!result.succ) {
$("#dialogNewFilePrompt").dialog("close");
bottomGroup.tabs.setCurrent("notification");
windows.flowBottom();
@ -199,8 +199,8 @@ var wide = {
url: config.context + '/file/new',
data: JSON.stringify(request),
dataType: "json",
success: function (data) {
if (!data.succ) {
success: function (result) {
if (!result.succ) {
$("#dialogNewDirPrompt").dialog("close");
bottomGroup.tabs.setCurrent("notification");
windows.flowBottom();
@ -356,8 +356,7 @@ var wide = {
url: config.context + '/git/clone',
data: JSON.stringify(request),
dataType: "json",
success: function (data) {
success: function (result) {
}
});
}
@ -672,9 +671,9 @@ var wide = {
url: config.context + '/go/fmt',
data: JSON.stringify(request),
dataType: "json",
success: function (data) {
if (data.succ) {
editor.setValue(data.code);
success: function (result) {
if (result.succ) {
editor.setValue(result.data.code);
editor.setCursor(cursor);
editor.scrollTo(null, scrollInfo.top);
@ -705,9 +704,9 @@ var wide = {
url: config.context + '/go/fmt',
data: JSON.stringify(request),
dataType: "json",
success: function (data) {
if (data.succ) {
formatted = data.code;
success: function (result) {
if (result.succ) {
formatted = result.data.code;
}
}
});

View File

@ -81,8 +81,8 @@
return false;
}
},
success: function (data) {
if (!data.succ) {
success: function (result) {
if (!result.succ) {
$("#msg").text('{{.i18n.login_error}}').show();
return;
}

View File

@ -96,9 +96,9 @@
url: '/signup',
data: JSON.stringify(request),
dataType: "json",
success: function (data) {
if (!data.succ) {
$("#msg").text(data.msg).show();
success: function (result) {
if (!result.succ) {
$("#msg").text(result.msg).show();
return;
}