From 6e1ae805e96f98218b2c08d2c89425e6b33c51b6 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 13 Feb 2015 10:50:14 +0800 Subject: [PATCH] #212 --- main.go | 1 + playground/build.go | 4 ++-- playground/file.go | 42 +++++++++++++-------------------------- playground/playgrounds.go | 15 ++++++++++++++ session/sessions.go | 2 +- static/js/playground.js | 8 ++++++-- 6 files changed, 39 insertions(+), 33 deletions(-) diff --git a/main.go b/main.go index f60227f..e6ea542 100644 --- a/main.go +++ b/main.go @@ -166,6 +166,7 @@ func main() { // playground http.HandleFunc(conf.Wide.Context+"/playground", handlerWrapper(playground.IndexHandler)) + http.HandleFunc(conf.Wide.Context+"/playground/", handlerWrapper(playground.IndexHandler)) http.HandleFunc(conf.Wide.Context+"/playground/ws", handlerWrapper(playground.WSHandler)) http.HandleFunc(conf.Wide.Context+"/playground/save", handlerWrapper(playground.SaveHandler)) http.HandleFunc(conf.Wide.Context+"/playground/build", handlerWrapper(playground.BuildHandler)) diff --git a/playground/build.go b/playground/build.go index fdbc317..d4bb74f 100644 --- a/playground/build.go +++ b/playground/build.go @@ -47,14 +47,14 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) { return } - filePath := args["filePath"].(string) + fileName := args["fileName"].(string) + filePath := filepath.Clean(conf.Wide.Playground + "/" + fileName) suffix := "" if util.OS.IsWindows() { suffix = ".exe" } - fileName := filepath.Base(filePath) executable := filepath.Clean(conf.Wide.Playground + "/" + strings.Replace(fileName, ".go", suffix, -1)) cmd := exec.Command("go", "build", "-o", executable, filePath) diff --git a/playground/file.go b/playground/file.go index eb8bc6f..7478805 100644 --- a/playground/file.go +++ b/playground/file.go @@ -18,14 +18,15 @@ import ( "crypto/md5" "encoding/hex" "encoding/json" + "io" "net/http" "os" "os/exec" "path/filepath" + "github.com/b3log/wide/conf" "github.com/b3log/wide/session" "github.com/b3log/wide/util" - "github.com/b3log/wide/conf" ) // SaveHandler handles request of Playground code save. @@ -50,14 +51,10 @@ func SaveHandler(w http.ResponseWriter, r *http.Request) { code := args["code"].(string) - // generate file name - hasher := md5.New() - hasher.Write([]byte(code)) - fileName := hex.EncodeToString(hasher.Sum(nil)) - fileName += ".go" - filePath := filepath.Clean(conf.Wide.Playground + "/" + fileName) + // Step1. format code + cmd := exec.Command("gofmt") - fout, err := os.Create(filePath) + stdin, err := cmd.StdinPipe() if nil != err { logger.Error(err) data["succ"] = false @@ -65,19 +62,8 @@ func SaveHandler(w http.ResponseWriter, r *http.Request) { return } - fout.WriteString(code) - if err := fout.Close(); nil != err { - logger.Error(err) - data["succ"] = false - - return - } - - data["filePath"] = filePath - data["url"] = filepath.ToSlash(filePath) - - argv := []string{filePath} - cmd := exec.Command("gofmt", argv...) + io.WriteString(stdin, code) + stdin.Close() bytes, _ := cmd.Output() output := string(bytes) @@ -92,16 +78,16 @@ func SaveHandler(w http.ResponseWriter, r *http.Request) { code = string(output) data["code"] = code - // generate file name - hasher = md5.New() + // Step2. generate file name + hasher := md5.New() hasher.Write([]byte(code)) - fileName = hex.EncodeToString(hasher.Sum(nil)) + fileName := hex.EncodeToString(hasher.Sum(nil)) fileName += ".go" - filePath = filepath.Clean(conf.Wide.Playground + "/" + fileName) - data["filePath"] = filePath - data["url"] = filepath.ToSlash(filePath) + data["fileName"] = fileName - fout, err = os.Create(filePath) + // Step3. write file + filePath := filepath.Clean(conf.Wide.Playground + "/" + fileName) + fout, err := os.Create(filePath) fout.WriteString(code) if err := fout.Close(); nil != err { logger.Error(err) diff --git a/playground/playgrounds.go b/playground/playgrounds.go index c086983..16368d7 100644 --- a/playground/playgrounds.go +++ b/playground/playgrounds.go @@ -17,10 +17,13 @@ package playground import ( "html/template" + "io/ioutil" "math/rand" "net/http" "os" + "path/filepath" "strconv" + "strings" "time" "github.com/b3log/wide/conf" @@ -58,7 +61,19 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) { locale := conf.Wide.Locale + // try to load file code := conf.HelloWorld + if strings.HasSuffix(r.RequestURI, ".go") { + fileName := r.RequestURI[len("/playground/"):] + filePath := filepath.Clean(conf.Wide.Playground + "/" + fileName) + + bytes, err := ioutil.ReadFile(filePath) + if nil != err { + logger.Warn(err) + } + + code = string(bytes) + } model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(locale), "locale": locale, "session": wideSession, "pathSeparator": conf.PathSeparator, "codeMirrorVer": conf.CodeMirrorVer, diff --git a/session/sessions.go b/session/sessions.go index c7399ed..5445c8d 100644 --- a/session/sessions.go +++ b/session/sessions.go @@ -386,7 +386,7 @@ func (sessions *wSessions) Remove(sid string) { } } - logger.Debugf("Removed a session [%s] of user [%s], it has [%d] sessions currently", sid, s.Username, cnt) + logger.Tracef("Removed a session [%s] of user [%s], it has [%d] sessions currently", sid, s.Username, cnt) return } diff --git a/static/js/playground.js b/static/js/playground.js index f8f76b3..0d4c960 100644 --- a/static/js/playground.js +++ b/static/js/playground.js @@ -66,6 +66,10 @@ var playground = { console.log('[playground onmessage]' + e.data); var data = JSON.parse(e.data); + + if ("init-playground" === data.cmd) { + return; + } playground.pid = data.pid; @@ -104,7 +108,7 @@ var playground = { return; } - var url = window.location.protocol + "//" + window.location.host + '/' + data.url; + var url = window.location.protocol + "//" + window.location.host + '/playground/' + data.fileName; var html = '' + url + ""; $("#url").html(html); @@ -154,7 +158,7 @@ var playground = { // Step 2. compile code var request = newWideRequest(); - request.filePath = data.filePath; + request.fileName = data.fileName; $.ajax({ type: 'POST',