This commit is contained in:
parent
36a8bb4d50
commit
6e1ae805e9
1
main.go
1
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))
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -67,6 +67,10 @@ var playground = {
|
|||
|
||||
var data = JSON.parse(e.data);
|
||||
|
||||
if ("init-playground" === data.cmd) {
|
||||
return;
|
||||
}
|
||||
|
||||
playground.pid = data.pid;
|
||||
|
||||
var val = $("#output").val();
|
||||
|
@ -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 = '<a href="' + url + '" target="_blank">'
|
||||
+ url + "</a>";
|
||||
$("#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',
|
||||
|
|
Loading…
Reference in New Issue