diff --git a/main.go b/main.go
index 31be570..b21af76 100644
--- a/main.go
+++ b/main.go
@@ -169,6 +169,7 @@ func main() {
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/short-url", handlerWrapper(playground.ShortURLHandler))
http.HandleFunc(conf.Wide.Context+"/playground/build", handlerWrapper(playground.BuildHandler))
http.HandleFunc(conf.Wide.Context+"/playground/run", handlerWrapper(playground.RunHandler))
http.HandleFunc(conf.Wide.Context+"/playground/stop", handlerWrapper(playground.StopHandler))
diff --git a/playground/file.go b/playground/file.go
index 31e37a6..c55f107 100644
--- a/playground/file.go
+++ b/playground/file.go
@@ -23,6 +23,7 @@ import (
"os"
"os/exec"
"path/filepath"
+ "strings"
"github.com/b3log/wide/conf"
"github.com/b3log/wide/session"
@@ -90,5 +91,45 @@ func SaveHandler(w http.ResponseWriter, r *http.Request) {
return
}
-
+}
+
+// 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)
+
+ session, _ := session.HTTPSession.Get(r, "wide-session")
+ if session.IsNew {
+ http.Error(w, "Forbidden", http.StatusForbidden)
+
+ return
+ }
+
+ var args map[string]interface{}
+ if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
+ logger.Error(err)
+ data["succ"] = false
+
+ return
+ }
+
+ url := args["url"].(string)
+
+ resp, _ := http.Post("http://dwz.cn/create.php", "application/x-www-form-urlencoded",
+ strings.NewReader("url="+url))
+
+ var response map[string]interface{}
+ if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
+ logger.Error(err)
+ data["succ"] = false
+
+ return
+ }
+
+ shortURL := url
+ if 0 == response["status"].(float64) {
+ shortURL = response["tinyurl"].(string)
+ }
+
+ data["shortURL"] = shortURL
}
diff --git a/static/js/playground.js b/static/js/playground.js
index f451703..aa44a00 100644
--- a/static/js/playground.js
+++ b/static/js/playground.js
@@ -286,15 +286,32 @@ var playground = {
playground.editor.setValue(data.code);
if (!data.succ) {
+ console.log(data);
return;
}
var url = window.location.protocol + "//" + window.location.host + '/playground/' + data.fileName;
- var html = 'URL: ' + url + "
";
- html += 'Embeded:
';
- $("#dialogShare").html(html);
- $("#dialogShare").dialog("open");
+ var request = newWideRequest();
+ request.url = url;
+ $.ajax({
+ type: 'POST',
+ url: config.context + '/playground/short-url',
+ data: JSON.stringify(request),
+ dataType: "json",
+ success: function (data) {
+ if (!data.succ) {
+ console.log(data);
+ return;
+ }
+
+ var html = 'URL: ' + url + "
";
+ html += 'Short URL: ' + data.shortURL + '
';
+ html += 'Embeded:
';
+
+ $("#dialogShare").html(html);
+ $("#dialogShare").dialog("open");
+ }});
}
});
},