some updates related to json responses

This commit is contained in:
Henrique Dias
2016-03-06 15:56:53 +00:00
parent e33c79224f
commit ddcaccda0c
8 changed files with 88 additions and 76 deletions

View File

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

View File

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

View File

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