diff --git a/browse/get.go b/browse/get.go index 2e6a52ce..a018c471 100644 --- a/browse/get.go +++ b/browse/get.go @@ -20,8 +20,7 @@ func GET(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) tpl, err := utils.GetTemplate(r, functions, "browse") if err != nil { - w.Write([]byte(err.Error())) - return 500, err + return http.StatusInternalServerError, err } b := browse.Browse{ diff --git a/browse/post.go b/browse/post.go index 8651b911..bc690ad1 100644 --- a/browse/post.go +++ b/browse/post.go @@ -34,11 +34,11 @@ func POST(w http.ResponseWriter, r *http.Request) (int, error) { // Check if filename and archetype are specified in // the request if _, ok := info["filename"]; !ok { - return 400, errors.New("Filename not specified.") + return http.StatusBadRequest, errors.New("Filename not specified.") } if _, ok := info["archetype"]; !ok { - return 400, errors.New("Archtype not specified.") + return http.StatusBadRequest, errors.New("Archtype not specified.") } // Sanitize the file name path @@ -62,7 +62,7 @@ func POST(w http.ResponseWriter, r *http.Request) (int, error) { err = utils.CopyFile(archetype+".markdown", filename) if err != nil { w.Write([]byte(err.Error())) - return 500, err + return http.StatusInternalServerError, err } w.Header().Set("Location", "/admin/edit/"+filename) @@ -76,7 +76,7 @@ func POST(w http.ResponseWriter, r *http.Request) (int, error) { err = utils.CopyFile(archetype+".md", filename) if err != nil { w.Write([]byte(err.Error())) - return 500, err + return http.StatusInternalServerError, err } w.Header().Set("Location", "/admin/edit/"+filename) @@ -89,7 +89,7 @@ func POST(w http.ResponseWriter, r *http.Request) (int, error) { wf, err := os.Create(filename) if err != nil { w.Write([]byte(err.Error())) - return 500, err + return http.StatusInternalServerError, err } defer wf.Close() @@ -97,7 +97,7 @@ func POST(w http.ResponseWriter, r *http.Request) (int, error) { w.Header().Set("Location", "/admin/edit/"+filename) w.Header().Set("Content-Type", "application/json") w.Write([]byte("{}")) - return 200, nil + return http.StatusOK, nil } func upload(w http.ResponseWriter, r *http.Request) (int, error) { @@ -105,7 +105,7 @@ func upload(w http.ResponseWriter, r *http.Request) (int, error) { err := r.ParseMultipartForm(100000) if err != nil { w.Write([]byte(err.Error())) - return 500, err + return http.StatusInternalServerError, err } // For each file header in the multipart form @@ -116,25 +116,25 @@ func upload(w http.ResponseWriter, r *http.Request) (int, error) { var infile multipart.File if infile, err = hdr.Open(); nil != err { w.Write([]byte(err.Error())) - return 500, err + return http.StatusInternalServerError, err } // Create the file var outfile *os.File if outfile, err = os.Create(r.URL.Path + hdr.Filename); nil != err { w.Write([]byte(err.Error())) - return 500, err + return http.StatusInternalServerError, err } // Copy the file content if _, err = io.Copy(outfile, infile); nil != err { w.Write([]byte(err.Error())) - return 500, err + return http.StatusInternalServerError, err } } } w.Header().Set("Content-Type", "application/json") w.Write([]byte("{}")) - return 200, nil + return http.StatusOK, nil } diff --git a/editor/get.go b/editor/get.go index b4ae5673..193143cf 100644 --- a/editor/get.go +++ b/editor/get.go @@ -21,20 +21,18 @@ func GET(w http.ResponseWriter, r *http.Request, c *config.Config, filename stri // Check if the file format is supported. If not, send a "Not Acceptable" // header and an error if !utils.CanBeEdited(filename) { - return 406, errors.New("File format not supported.") + return http.StatusNotAcceptable, errors.New("File format not supported.") } // Check if the file exists. If it doesn't, send a "Not Found" message if _, err := os.Stat(filename); os.IsNotExist(err) { - w.Write([]byte(err.Error())) - return 404, nil + return http.StatusNotFound, nil } // Open the file and check if there was some error while opening file, err := ioutil.ReadFile(filename) if err != nil { - w.Write([]byte(err.Error())) - return 500, err + return http.StatusInternalServerError, err } // Create a new editor variable and set the extension @@ -55,8 +53,7 @@ func GET(w http.ResponseWriter, r *http.Request, c *config.Config, filename stri buffer := bytes.NewBuffer(file) file, err := parser.ReadFrom(buffer) if err != nil { - w.Write([]byte(err.Error())) - return 500, err + return http.StatusInternalServerError, err } if strings.Contains(string(file.FrontMatter()), "date") { @@ -86,8 +83,7 @@ func GET(w http.ResponseWriter, r *http.Request, c *config.Config, filename stri // Check if there were any errors if err != nil { - w.Write([]byte(err.Error())) - return 500, err + return http.StatusInternalServerError, err } default: // The editor will handle only content @@ -105,11 +101,10 @@ func GET(w http.ResponseWriter, r *http.Request, c *config.Config, filename stri tpl, err := utils.GetTemplate(r, functions, "editor", "frontmatter") if err != nil { - w.Write([]byte(err.Error())) - return 500, err + return http.StatusInternalServerError, err } - return 200, tpl.Execute(w, page) + return http.StatusOK, tpl.Execute(w, page) } func hasFrontMatterRune(file []byte) bool { diff --git a/editor/post.go b/editor/post.go index 007c22cc..6e974982 100644 --- a/editor/post.go +++ b/editor/post.go @@ -52,7 +52,7 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config, filename str file = f default: - return 400, nil + return http.StatusNotFound, nil } // Write the file @@ -60,12 +60,12 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config, filename str if err != nil { w.Write([]byte(err.Error())) - return 500, err + return http.StatusInternalServerError, err } w.Header().Set("Content-Type", "application/json") w.Write([]byte("{}")) - return 200, nil + return http.StatusOK, nil } func parseFrontMatterOnlyFile(rawFile map[string]interface{}, filename string) ([]byte, int, error) { @@ -80,7 +80,7 @@ func parseFrontMatterOnlyFile(rawFile map[string]interface{}, filename string) ( case "yaml": mark = rune('-') default: - return []byte{}, 400, nil + return []byte{}, http.StatusNotFound, nil } f, err := parser.InterfaceToFrontMatter(rawFile, mark) @@ -100,10 +100,10 @@ func parseFrontMatterOnlyFile(rawFile map[string]interface{}, filename string) ( f = []byte(fString) if err != nil { - return []byte{}, 500, err + return []byte{}, http.StatusInternalServerError, err } - return f, 200, nil + return f, http.StatusOK, nil } func parseCompleteFile(r *http.Request, c *config.Config, rawFile map[string]interface{}, filename string) ([]byte, int, error) { @@ -116,10 +116,10 @@ func parseCompleteFile(r *http.Request, c *config.Config, rawFile map[string]int // Schedule the post if r.Header.Get("X-Schedule") == "true" { - t, err := time.Parse("2006-01-02 15:04:05-07:00", rawFile["date"].(string)) + t, err := time.Parse("http.StatusOK6-01-02 15:04:05-07:00", rawFile["date"].(string)) if err != nil { - return []byte{}, 500, err + return []byte{}, http.StatusInternalServerError, err } scheduler := cron.New() @@ -160,7 +160,7 @@ func parseCompleteFile(r *http.Request, c *config.Config, rawFile map[string]int jsonFrontmatter, err := json.Marshal(rawFile) if err != nil { - return []byte{}, 500, err + return []byte{}, http.StatusInternalServerError, err } // Indents the json @@ -171,5 +171,5 @@ func parseCompleteFile(r *http.Request, c *config.Config, rawFile map[string]int f := new(bytes.Buffer) f.Write(frontMatterBuffer.Bytes()) f.Write([]byte(mainContent)) - return f.Bytes(), 200, nil + return f.Bytes(), http.StatusOK, nil }