This commit is contained in:
Liang Ding 2015-02-13 10:50:14 +08:00
parent 36a8bb4d50
commit 6e1ae805e9
6 changed files with 39 additions and 33 deletions

View File

@ -166,6 +166,7 @@ func main() {
// playground // 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/", handlerWrapper(playground.IndexHandler))
http.HandleFunc(conf.Wide.Context+"/playground/ws", handlerWrapper(playground.WSHandler)) 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/save", handlerWrapper(playground.SaveHandler))
http.HandleFunc(conf.Wide.Context+"/playground/build", handlerWrapper(playground.BuildHandler)) http.HandleFunc(conf.Wide.Context+"/playground/build", handlerWrapper(playground.BuildHandler))

View File

@ -47,14 +47,14 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
filePath := args["filePath"].(string) fileName := args["fileName"].(string)
filePath := filepath.Clean(conf.Wide.Playground + "/" + fileName)
suffix := "" suffix := ""
if util.OS.IsWindows() { if util.OS.IsWindows() {
suffix = ".exe" suffix = ".exe"
} }
fileName := filepath.Base(filePath)
executable := filepath.Clean(conf.Wide.Playground + "/" + strings.Replace(fileName, ".go", suffix, -1)) executable := filepath.Clean(conf.Wide.Playground + "/" + strings.Replace(fileName, ".go", suffix, -1))
cmd := exec.Command("go", "build", "-o", executable, filePath) cmd := exec.Command("go", "build", "-o", executable, filePath)

View File

@ -18,14 +18,15 @@ import (
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"io"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"github.com/b3log/wide/conf"
"github.com/b3log/wide/session" "github.com/b3log/wide/session"
"github.com/b3log/wide/util" "github.com/b3log/wide/util"
"github.com/b3log/wide/conf"
) )
// SaveHandler handles request of Playground code save. // SaveHandler handles request of Playground code save.
@ -50,14 +51,10 @@ func SaveHandler(w http.ResponseWriter, r *http.Request) {
code := args["code"].(string) code := args["code"].(string)
// generate file name // Step1. format code
hasher := md5.New() cmd := exec.Command("gofmt")
hasher.Write([]byte(code))
fileName := hex.EncodeToString(hasher.Sum(nil))
fileName += ".go"
filePath := filepath.Clean(conf.Wide.Playground + "/" + fileName)
fout, err := os.Create(filePath) stdin, err := cmd.StdinPipe()
if nil != err { if nil != err {
logger.Error(err) logger.Error(err)
data["succ"] = false data["succ"] = false
@ -65,19 +62,8 @@ func SaveHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
fout.WriteString(code) io.WriteString(stdin, code)
if err := fout.Close(); nil != err { stdin.Close()
logger.Error(err)
data["succ"] = false
return
}
data["filePath"] = filePath
data["url"] = filepath.ToSlash(filePath)
argv := []string{filePath}
cmd := exec.Command("gofmt", argv...)
bytes, _ := cmd.Output() bytes, _ := cmd.Output()
output := string(bytes) output := string(bytes)
@ -92,16 +78,16 @@ func SaveHandler(w http.ResponseWriter, r *http.Request) {
code = string(output) code = string(output)
data["code"] = code data["code"] = code
// generate file name // Step2. generate file name
hasher = md5.New() hasher := md5.New()
hasher.Write([]byte(code)) hasher.Write([]byte(code))
fileName = hex.EncodeToString(hasher.Sum(nil)) fileName := hex.EncodeToString(hasher.Sum(nil))
fileName += ".go" fileName += ".go"
filePath = filepath.Clean(conf.Wide.Playground + "/" + fileName) data["fileName"] = fileName
data["filePath"] = filePath
data["url"] = filepath.ToSlash(filePath)
fout, err = os.Create(filePath) // Step3. write file
filePath := filepath.Clean(conf.Wide.Playground + "/" + fileName)
fout, err := os.Create(filePath)
fout.WriteString(code) fout.WriteString(code)
if err := fout.Close(); nil != err { if err := fout.Close(); nil != err {
logger.Error(err) logger.Error(err)

View File

@ -17,10 +17,13 @@ package playground
import ( import (
"html/template" "html/template"
"io/ioutil"
"math/rand" "math/rand"
"net/http" "net/http"
"os" "os"
"path/filepath"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/b3log/wide/conf" "github.com/b3log/wide/conf"
@ -58,7 +61,19 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) {
locale := conf.Wide.Locale locale := conf.Wide.Locale
// try to load file
code := conf.HelloWorld 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, model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(locale), "locale": locale,
"session": wideSession, "pathSeparator": conf.PathSeparator, "codeMirrorVer": conf.CodeMirrorVer, "session": wideSession, "pathSeparator": conf.PathSeparator, "codeMirrorVer": conf.CodeMirrorVer,

View File

@ -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 return
} }

View File

@ -66,6 +66,10 @@ var playground = {
console.log('[playground onmessage]' + e.data); console.log('[playground onmessage]' + e.data);
var data = JSON.parse(e.data); var data = JSON.parse(e.data);
if ("init-playground" === data.cmd) {
return;
}
playground.pid = data.pid; playground.pid = data.pid;
@ -104,7 +108,7 @@ var playground = {
return; return;
} }
var url = window.location.protocol + "//" + window.location.host + '/' + data.url; var url = window.location.protocol + "//" + window.location.host + '/playground/' + data.fileName;
var html = '<a href="' + url + '" target="_blank">' var html = '<a href="' + url + '" target="_blank">'
+ url + "</a>"; + url + "</a>";
$("#url").html(html); $("#url").html(html);
@ -154,7 +158,7 @@ var playground = {
// Step 2. compile code // Step 2. compile code
var request = newWideRequest(); var request = newWideRequest();
request.filePath = data.filePath; request.fileName = data.fileName;
$.ajax({ $.ajax({
type: 'POST', type: 'POST',