some updates related to json responses
This commit is contained in:
1 parent
e33c79224f
commit
ddcaccda0c
8 files changed
+88
-76
No files matched your search
Vendored
+1
-1
@@ -1 +1 @@
|
||||
$(document).ready(function(){$("#logout").click(function(a){return a.preventDefault(),$.ajax({type:"GET",url:"/admin",async:!1,username:"username",password:"password",headers:{Authorization:"Basic xxx"}}).fail(function(){window.location="/"}),!1}),$(document).pjax("a[data-pjax]","#content")}),$(document).on("ready pjax:end",function(){function a(){this.style.height="5px",this.style.height=this.scrollHeight+"px"}return $("#content").off(),document.title=document.getElementById("site-title").innerHTML,$("textarea").each(a),$("textarea").keyup(a),$(window).resize(function(){$("textarea").each(a)}),$("main").hasClass("browse")&&$(document).trigger("page:browse"),$(".editor")[0]&&$(document).trigger("page:editor"),!1}),$(document).on("page:browse",function(){var a="#foreground",b=new Object;b.selector="form#delete",b.form=$(b.selector),b.row="",b.button="",b.url="",$("#content").on("click",".delete",function(c){return c.preventDefault(),b.button=$(this),b.row=$(this).parent().parent(),$(a).fadeIn(200),b.url=b.row.find(".filename").text(),b.form.find("span").text(b.url),b.form.fadeIn(200),!1}),$("#content").on("submit",b.selector,function(c){c.preventDefault();var d=new XMLHttpRequest;return d.open("DELETE",b.button.data("file")),d.send(),d.onreadystatechange=function(){if(4==d.readyState){var c=JSON.parse(d.responseText),e="success",f=5e3;$(a).fadeOut(200),b.form.fadeOut(200),b.row.fadeOut(200),200!=d.status&&(e="error",f=!1),notification({text:c.message,type:e,timeout:f})}},!1}),$("#content").on("change",'input[type="file"]',function(a){a.preventDefault(),files=a.target.files;var b=new FormData;return $.each(files,function(a,c){b.append(a,c)}),$.ajax({url:window.location.pathname,type:"POST",data:b,cache:!1,dataType:"json",headers:{"X-Upload":"true"},processData:!1,contentType:!1}).done(function(a){notification({text:"File(s) uploaded successfully.",type:"success",timeout:5e3}),$.pjax({url:window.location.pathname,container:"#content"})}).fail(function(a){notification({text:"Something went wrong.",type:"error"}),console.log(a)}),!1}),$("#content").on("click","#upload",function(a){return a.preventDefault(),$('.actions input[type="file"]').click(),!1});var c=new Object;c.selector="form#new",c.form=$(c.selector),c.input=c.selector+' input[type="text"]',c.button="",c.url="",$("#content").on("click",".new",function(b){return b.preventDefault(),c.button=$(this),$(a).fadeIn(200),c.form.fadeIn(200),!1}),$("#content").on("keypress",c.input,function(a){return 13==a.keyCode?(a.preventDefault(),$(c.form).submit(),!1):void 0}),$("#content").on("submit",c.selector,function(a){a.preventDefault();var b=c.form.find('input[type="text"]').val(),d=b.split(":"),e="",f="";if(""==b)return notification({text:"You have to write something. If you want to close the box, click the button again.",type:"warning",timeout:5e3}),!1;if(1==d.length)e=b;else{if(2!=d.length)return notification({text:"Hmm... I don't understand you. Try writing something like 'name[:archetype]'.",type:"error"}),!1;e=d[0],f=d[1]}var g={filename:e,archetype:f},h=new XMLHttpRequest;return h.open("POST",window.location.pathname),h.setRequestHeader("Content-Type","application/json;charset=UTF-8"),h.send(JSON.stringify(g)),h.onreadystatechange=function(){if(4==h.readyState)if(200==h.status){var a=JSON.parse(h.responseText);notification({text:"File created successfully.",type:"success",timeout:5e3}),$.pjax({url:a.Location,container:"#content"})}else notification({text:"Something went wrong.",type:"error"}),console.log(h.responseText)},!1});var d=new Object;d.selector="form#rename",d.form=$(d.selector),d.input=d.selector+' input[type="text"]',d.button="",d.url="",$("#content").on("click",".rename",function(b){return b.preventDefault(),d.button=$(this),$(a).fadeIn(200),d.url=$(this).parent().parent().find(".filename").text(),d.form.fadeIn(200),d.form.find("span").text(d.url),d.form.find('input[type="text"]').val(d.url),!1}),$("#content").on("keypress",d.input,function(a){return 13==a.keyCode?(a.preventDefault(),$(d.form).submit(),!1):void 0}),$("#content").on("submit",d.selector,function(a){a.preventDefault();var b=d.form.find('input[type="text"]').val();if(""===b)return!1;"/"!=b.substring(0,1)&&(b=window.location.pathname.replace("/admin/browse/","")+"/"+b);var c={filename:b},e=new XMLHttpRequest;return e.open("PUT",d.url),e.setRequestHeader("Content-Type","application/json;charset=UTF-8"),e.send(JSON.stringify(c)),e.onreadystatechange=function(){if(4==e.readyState){var a=JSON.parse(e.responseText),b="success",c=5e3;200!=e.status&&(b="error",c=!1),$.pjax({url:window.location.pathname,container:"#content"}),notification({text:a.message,type:b,timeout:c})}},!1});var e=new Object;e.selector="form#git",e.form=$(e.selector),e.input=e.selector+' input[type="text"]',$("#content").on("click","button.git",function(b){return b.preventDefault(),$(a).fadeIn(200),e.form.fadeIn(200),!1}),$("#content").on("keypress",e.input,function(a){return 13==a.keyCode?(a.preventDefault(),$(e.form).sLine truncated
|
||||
$(document).ready(function(){$("#logout").click(function(a){return a.preventDefault(),$.ajax({type:"GET",url:"/admin",async:!1,username:"username",password:"password",headers:{Authorization:"Basic xxx"}}).fail(function(){window.location="/"}),!1}),$(document).pjax("a[data-pjax]","#content")}),$(document).on("ready pjax:end",function(){function a(){this.style.height="5px",this.style.height=this.scrollHeight+"px"}return $("#content").off(),document.title=document.getElementById("site-title").innerHTML,$("textarea").each(a),$("textarea").keyup(a),$(window).resize(function(){$("textarea").each(a)}),$("main").hasClass("browse")&&$(document).trigger("page:browse"),$(".editor")[0]&&$(document).trigger("page:editor"),!1}),$(document).on("page:browse",function(){var a="#foreground",b=new Object;b.selector="form#delete",b.form=$(b.selector),b.row="",b.button="",b.url="",$("#content").on("click",".delete",function(c){return c.preventDefault(),b.button=$(this),b.row=$(this).parent().parent(),$(a).fadeIn(200),b.url=b.row.find(".filename").text(),b.form.find("span").text(b.url),b.form.fadeIn(200),!1}),$("#content").on("submit",b.selector,function(c){c.preventDefault();var d=new XMLHttpRequest;return d.open("DELETE",b.button.data("file")),d.send(),d.onreadystatechange=function(){if(4==d.readyState){var c=JSON.parse(d.responseText),e="success",f=5e3;$(a).fadeOut(200),b.form.fadeOut(200),b.row.fadeOut(200),200!=d.status&&(e="error",f=!1),notification({text:c.message,type:e,timeout:f})}},!1}),$("#content").on("change",'input[type="file"]',function(a){a.preventDefault(),files=a.target.files;var b=new FormData;return $.each(files,function(a,c){b.append(a,c)}),$.ajax({url:window.location.pathname,type:"POST",data:b,cache:!1,dataType:"json",headers:{"X-Upload":"true"},processData:!1,contentType:!1}).done(function(a){notification({text:"File(s) uploaded successfully.",type:"success",timeout:5e3}),$.pjax({url:window.location.pathname,container:"#content"})}).fail(function(a){notification({text:"Something went wrong.",type:"error"}),console.log(a)}),!1}),$("#content").on("click","#upload",function(a){return a.preventDefault(),$('.actions input[type="file"]').click(),!1});var c=new Object;c.selector="form#new",c.form=$(c.selector),c.input=c.selector+' input[type="text"]',c.button="",c.url="",$("#content").on("click",".new",function(b){return b.preventDefault(),c.button=$(this),$(a).fadeIn(200),c.form.fadeIn(200),!1}),$("#content").on("keypress",c.input,function(a){return 13==a.keyCode?(a.preventDefault(),$(c.form).submit(),!1):void 0}),$("#content").on("submit",c.selector,function(a){a.preventDefault();var b=c.form.find('input[type="text"]').val(),d=b.split(":"),e="",f="";if(""==b)return notification({text:"You have to write something. If you want to close the box, click the button again.",type:"warning",timeout:5e3}),!1;if(1==d.length)e=b;else{if(2!=d.length)return notification({text:"Hmm... I don't understand you. Try writing something like 'name[:archetype]'.",type:"error"}),!1;e=d[0],f=d[1]}var g={filename:e,archetype:f},h=new XMLHttpRequest;return h.open("POST",window.location.pathname),h.setRequestHeader("Content-Type","application/json;charset=UTF-8"),h.send(JSON.stringify(g)),h.onreadystatechange=function(){if(4==h.readyState){var a=JSON.parse(h.responseText),b="success",c=5e3;200!=h.status&&(b="error",c=!1),notification({text:a.message,type:b,timeout:c}),200==h.status&&$.pjax({url:data.Location,container:"#content"})}},!1});var d=new Object;d.selector="form#rename",d.form=$(d.selector),d.input=d.selector+' input[type="text"]',d.button="",d.url="",$("#content").on("click",".rename",function(b){return b.preventDefault(),d.button=$(this),$(a).fadeIn(200),d.url=$(this).parent().parent().find(".filename").text(),d.form.fadeIn(200),d.form.find("span").text(d.url),d.form.find('input[type="text"]').val(d.url),!1}),$("#content").on("keypress",d.input,function(a){return 13==a.keyCode?(a.preventDefault(),$(d.form).submit(),!1):void 0}),$("#content").on("submit",d.selector,function(a){a.preventDefault();var b=d.form.find('input[type="text"]').val();if(""===b)return!1;"/"!=b.substring(0,1)&&(b=window.location.pathname.replace("/admin/browse/","")+"/"+b);var c={filename:b},e=new XMLHttpRequest;return e.open("PUT",d.url),e.setRequestHeader("Content-Type","application/json;charset=UTF-8"),e.send(JSON.stringify(c)),e.onreadystatechange=function(){if(4==e.readyState){var a=JSON.parse(e.responseText),b="success",c=5e3;200!=e.status&&(b="error",c=!1),$.pjax({url:window.location.pathname,container:"#content"}),notification({text:a.message,type:b,timeout:c})}},!1});var e=new Object;e.selector="form#git",e.form=$(e.selector),e.input=e.selector+' input[type="text"]',$("#content").on("click","button.git",function(b){return b.preventDefault(),$(a).fadeIn(200),e.form.fadeIn(200),!1}),$("#content").on("keypress",e.input,function(a){return 13==a.keyCode?(a.preventDefault(),$(e.form).submit(),!1):void 0}),$("#content").on("submit",e.selector,function(aLine truncated
|
||||
+16
-14
@@ -169,24 +169,26 @@ $(document).on('page:browse', function() {
|
||||
request.send(JSON.stringify(content));
|
||||
request.onreadystatechange = function() {
|
||||
if (request.readyState == 4) {
|
||||
if (request.status == 200) {
|
||||
var data = JSON.parse(request.responseText);
|
||||
var response = JSON.parse(request.responseText);
|
||||
var type = "success";
|
||||
var timeout = 5000;
|
||||
|
||||
notification({
|
||||
text: "File created successfully.",
|
||||
type: 'success',
|
||||
timeout: 5000
|
||||
});
|
||||
if (request.status != 200) {
|
||||
type = "error";
|
||||
timeout = false;
|
||||
}
|
||||
|
||||
notification({
|
||||
text: response.message,
|
||||
type: type,
|
||||
timeout: timeout
|
||||
});
|
||||
|
||||
if (request.status == 200) {
|
||||
$.pjax({
|
||||
url: data.Location,
|
||||
container: '#content'
|
||||
})
|
||||
} else {
|
||||
notification({
|
||||
text: 'Something went wrong.',
|
||||
type: 'error'
|
||||
});
|
||||
console.log(request.responseText);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -353,4 +355,4 @@ $(document).on('page:browse', function() {
|
||||
git.form.fadeOut(200);
|
||||
return false;
|
||||
});
|
||||
});
|
||||
});
|
||||
+9
-3
@@ -32,11 +32,17 @@ func DELETE(w http.ResponseWriter, r *http.Request, c *config.Config) (int, erro
|
||||
|
||||
// Check for errors
|
||||
if err != nil {
|
||||
return utils.RespondJSON(w, "Something went wrong.", 500, nil)
|
||||
return utils.RespondJSON(w, map[string]string{
|
||||
"message": "Something went wrong.",
|
||||
}, 500, nil)
|
||||
}
|
||||
} else {
|
||||
return utils.RespondJSON(w, "File not found.", 404, nil)
|
||||
return utils.RespondJSON(w, map[string]string{
|
||||
"message": "File not found.",
|
||||
}, 404, nil)
|
||||
}
|
||||
|
||||
return utils.RespondJSON(w, message, 200, nil)
|
||||
return utils.RespondJSON(w, map[string]string{
|
||||
"message": message,
|
||||
}, 200, nil)
|
||||
}
|
||||
+29
-14
@@ -36,11 +36,15 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error)
|
||||
// Check if filename and archetype are specified in
|
||||
// the request
|
||||
if _, ok := info["filename"]; !ok {
|
||||
return utils.RespondJSON(w, "Filename not specified.", 500, nil)
|
||||
return utils.RespondJSON(w, map[string]string{
|
||||
"message": "Filename not specified.",
|
||||
}, 500, nil)
|
||||
}
|
||||
|
||||
if _, ok := info["archetype"]; !ok {
|
||||
return utils.RespondJSON(w, "Archtype not specified.", 500, nil)
|
||||
return utils.RespondJSON(w, map[string]string{
|
||||
"message": "Archtype not specified.",
|
||||
}, 500, nil)
|
||||
}
|
||||
|
||||
// Sanitize the file name path
|
||||
@@ -62,7 +66,9 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error)
|
||||
}
|
||||
|
||||
if err := utils.RunCommand(c.Hugo, args, c.Path); err != nil {
|
||||
return utils.RespondJSON(w, "Something went wrong.", 500, err)
|
||||
return utils.RespondJSON(w, map[string]string{
|
||||
"message": "Something went wrong.",
|
||||
}, 500, err)
|
||||
}
|
||||
} else {
|
||||
var err error
|
||||
@@ -77,22 +83,26 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return utils.RespondJSON(w, "Something went wrong.", 500, err)
|
||||
return utils.RespondJSON(w, map[string]string{
|
||||
"message": "Something went wrong.",
|
||||
}, 500, err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(200)
|
||||
w.Write([]byte("{\"Location\": \"" + url + "\"}"))
|
||||
return 0, nil
|
||||
return utils.RespondJSON(w, map[string]string{
|
||||
"location": url,
|
||||
"message": "File created.",
|
||||
}, 200, nil)
|
||||
}
|
||||
|
||||
func upload(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
|
||||
// Parse the multipart form in the request
|
||||
err := r.ParseMultipartForm(100000)
|
||||
if err != nil {
|
||||
return utils.RespondJSON(w, "Something went wrong.", 500, err)
|
||||
return utils.RespondJSON(w, map[string]string{
|
||||
"message": "Something went wrong.",
|
||||
}, 500, err)
|
||||
}
|
||||
|
||||
// For each file header in the multipart form
|
||||
@@ -102,24 +112,29 @@ func upload(w http.ResponseWriter, r *http.Request, c *config.Config) (int, erro
|
||||
// Open the first file
|
||||
var infile multipart.File
|
||||
if infile, err = hdr.Open(); nil != err {
|
||||
return utils.RespondJSON(w, "Something went wrong.", 500, err)
|
||||
return utils.RespondJSON(w, map[string]string{
|
||||
"message": "Something went wrong.",
|
||||
}, 500, err)
|
||||
}
|
||||
|
||||
// Create the file
|
||||
var outfile *os.File
|
||||
if outfile, err = os.Create(c.Path + r.URL.Path + hdr.Filename); nil != err {
|
||||
return utils.RespondJSON(w, "Something went wrong.", 500, err)
|
||||
return utils.RespondJSON(w, map[string]string{
|
||||
"message": "Something went wrong.",
|
||||
}, 500, err)
|
||||
}
|
||||
|
||||
// Copy the file content
|
||||
if _, err = io.Copy(outfile, infile); nil != err {
|
||||
return utils.RespondJSON(w, "Something went wrong.", 500, err)
|
||||
return utils.RespondJSON(w, map[string]string{
|
||||
"message": "Something went wrong.",
|
||||
}, 500, err)
|
||||
}
|
||||
|
||||
defer outfile.Close()
|
||||
}
|
||||
}
|
||||
|
||||
return utils.RespondJSON(w, "", 200, nil)
|
||||
return 0, nil
|
||||
return utils.RespondJSON(w, nil, 200, nil)
|
||||
}
|
||||
+9
-3
@@ -31,7 +31,9 @@ func PUT(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error)
|
||||
// Check if filename and archetype are specified in
|
||||
// the request
|
||||
if _, ok := info["filename"]; !ok {
|
||||
return utils.RespondJSON(w, "Filename not specified.", 400, nil)
|
||||
return utils.RespondJSON(w, map[string]string{
|
||||
"message": "Filename not specified.",
|
||||
}, 400, nil)
|
||||
}
|
||||
|
||||
// Sanitize the file name path
|
||||
@@ -42,8 +44,12 @@ func PUT(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error)
|
||||
|
||||
// Renames the file/folder
|
||||
if err := os.Rename(old, new); err != nil {
|
||||
return utils.RespondJSON(w, "Something went wrong.", 500, err)
|
||||
return utils.RespondJSON(w, map[string]string{
|
||||
"message": "Something went wrong.",
|
||||
}, 500, err)
|
||||
}
|
||||
|
||||
return utils.RespondJSON(w, "File renamed.", 200, nil)
|
||||
return utils.RespondJSON(w, map[string]string{
|
||||
"message": "File renamed.",
|
||||
}, 200, nil)
|
||||
}
|
||||
+16
-25
@@ -8,14 +8,16 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/hacdias/caddy-hugo/config"
|
||||
"github.com/hacdias/caddy-hugo/utils"
|
||||
)
|
||||
|
||||
// POST handles the POST method on GIT page which is only an API.
|
||||
func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
|
||||
// Check if git is installed on the computer
|
||||
if _, err := exec.LookPath("git"); err != nil {
|
||||
jsonMessage(w, "Git is not installed on your computer.", 500)
|
||||
return 0, nil
|
||||
return utils.RespondJSON(w, map[string]string{
|
||||
"message": "Git is not installed on your computer.",
|
||||
}, 400, nil)
|
||||
}
|
||||
|
||||
// Get the JSON information sent using a buffer
|
||||
@@ -28,8 +30,9 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error)
|
||||
|
||||
// Check if command was sent
|
||||
if _, ok := info["command"]; !ok {
|
||||
jsonMessage(w, "Command not specified.", 500)
|
||||
return 0, nil
|
||||
return utils.RespondJSON(w, map[string]string{
|
||||
"message": "Command not specified.",
|
||||
}, 400, nil)
|
||||
}
|
||||
|
||||
command := info["command"].(string)
|
||||
@@ -40,8 +43,9 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error)
|
||||
}
|
||||
|
||||
if len(args) == 0 {
|
||||
jsonMessage(w, "Command not specified.", 500)
|
||||
return 0, nil
|
||||
return utils.RespondJSON(w, map[string]string{
|
||||
"message": "Command not specified.",
|
||||
}, 400, nil)
|
||||
}
|
||||
|
||||
cmd := exec.Command("git", args...)
|
||||
@@ -49,25 +53,12 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error)
|
||||
output, err := cmd.CombinedOutput()
|
||||
|
||||
if err != nil {
|
||||
jsonMessage(w, err.Error(), 500)
|
||||
return 0, nil
|
||||
return utils.RespondJSON(w, map[string]string{
|
||||
"message": err.Error(),
|
||||
}, 500, err)
|
||||
}
|
||||
|
||||
jsonMessage(w, string(output), 200)
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
type jsonMSG struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func jsonMessage(w http.ResponseWriter, message string, code int) {
|
||||
msg := &jsonMSG{
|
||||
Message: message,
|
||||
}
|
||||
|
||||
m, _ := json.Marshal(msg)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(code)
|
||||
w.Write(m)
|
||||
return utils.RespondJSON(w, map[string]string{
|
||||
"message": string(output),
|
||||
}, 200, nil)
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
//go:generate go get github.com/jteeuwen/go-bindata
|
||||
//go:generate go install github.com/jteeuwen/go-bindata/go-bindata
|
||||
//go:generate go-bindata -debug -pkg assets -o assets/assets.go templates/ assets/css/ assets/js/ assets/fonts/
|
||||
//go:generate go-bindata -pkg assets -o assets/assets.go templates/ assets/css/ assets/js/ assets/fonts/
|
||||
|
||||
// Package hugo makes the bridge between the static website generator Hugo
|
||||
// and the webserver Caddy, also providing an administrative user interface.
|
||||
|
||||
+7
-15
@@ -238,27 +238,19 @@ func SplitCapitalize(name string) string {
|
||||
return name
|
||||
}
|
||||
|
||||
type jsonMSG struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
func RespondJSON(w http.ResponseWriter, message map[string]string, code int, err error) (int, error) {
|
||||
msg, msgErr := json.Marshal(message)
|
||||
|
||||
func RespondJSON(w http.ResponseWriter, message string, code int, err error) (int, error) {
|
||||
msg := &jsonMSG{
|
||||
Message: message,
|
||||
if msgErr != nil {
|
||||
return 500, msgErr
|
||||
}
|
||||
|
||||
m, err := json.Marshal(msg)
|
||||
|
||||
if err != nil {
|
||||
return 500, err
|
||||
}
|
||||
|
||||
if code == 500 && err == nil {
|
||||
err = errors.New(message)
|
||||
if code == 500 && err != nil {
|
||||
err = errors.New(message["message"])
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(code)
|
||||
w.Write(m)
|
||||
w.Write(msg)
|
||||
return 0, err
|
||||
}
|
||||
Reference in new issue
Block a user