short url
This commit is contained in:
Liang Ding 2015-02-16 17:10:21 +08:00
parent 5ecaff8c8f
commit ec0ca1e118
3 changed files with 64 additions and 5 deletions

View File

@ -169,6 +169,7 @@ func main() {
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/short-url", handlerWrapper(playground.ShortURLHandler))
http.HandleFunc(conf.Wide.Context+"/playground/build", handlerWrapper(playground.BuildHandler)) 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/run", handlerWrapper(playground.RunHandler))
http.HandleFunc(conf.Wide.Context+"/playground/stop", handlerWrapper(playground.StopHandler)) http.HandleFunc(conf.Wide.Context+"/playground/stop", handlerWrapper(playground.StopHandler))

View File

@ -23,6 +23,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings"
"github.com/b3log/wide/conf" "github.com/b3log/wide/conf"
"github.com/b3log/wide/session" "github.com/b3log/wide/session"
@ -90,5 +91,45 @@ func SaveHandler(w http.ResponseWriter, r *http.Request) {
return 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
} }

View File

@ -286,15 +286,32 @@ var playground = {
playground.editor.setValue(data.code); playground.editor.setValue(data.code);
if (!data.succ) { if (!data.succ) {
console.log(data);
return; return;
} }
var url = window.location.protocol + "//" + window.location.host + '/playground/' + data.fileName; var url = window.location.protocol + "//" + window.location.host + '/playground/' + data.fileName;
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: <a href="' + url + '" target="_blank">' + url + "</a><br/>"; var html = 'URL: <a href="' + url + '" target="_blank">' + url + "</a><br/>";
html += 'Short URL: <a href="' + data.shortURL + '" target="_blank">' + data.shortURL + '</a><br/>';
html += 'Embeded: <br/><textarea rows="5" cols="80"><p><iframe src="' + url + '?embed=true" width="100%" height="600"></iframe></p></textarea>'; html += 'Embeded: <br/><textarea rows="5" cols="80"><p><iframe src="' + url + '?embed=true" width="100%" height="600"></iframe></p></textarea>';
$("#dialogShare").html(html); $("#dialogShare").html(html);
$("#dialogShare").dialog("open"); $("#dialogShare").dialog("open");
}});
} }
}); });
}, },