From 4cf10f5c4a7f83c8e2e3b02ddbc87da019390ed8 Mon Sep 17 00:00:00 2001 From: Edward Muller Date: Wed, 25 Feb 2015 17:10:27 -0800 Subject: [PATCH] Updates based on #30 Allocate []bytes for reading data in and write out indented JSON. --- server.go | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/server.go b/server.go index c8797f6b..86a53e5f 100644 --- a/server.go +++ b/server.go @@ -1,9 +1,11 @@ package main import ( + "bytes" "encoding/json" "fmt" "io" + "io/ioutil" "log" "net/http" "os" @@ -29,48 +31,46 @@ func handleComments(w http.ResponseWriter, r *http.Request) { // Stat the file, so we can find it's current permissions fi, err := os.Stat(dataFile) if err != nil { - http.Error(w, fmt.Sprintf("Unable to stat data file (%s): %s", dataFile, err), http.StatusInternalServerError) + http.Error(w, fmt.Sprintf("Unable to stat the data file (%s): %s", dataFile, err), http.StatusInternalServerError) return } - // Open the file Read/Write - cFile, err := os.OpenFile(dataFile, os.O_RDWR, fi.Mode()) + // Read the comments from the file. + commentData, err := ioutil.ReadFile(dataFile) if err != nil { - http.Error(w, fmt.Sprintf("Unable to open data file (%s): %s", dataFile, err), http.StatusInternalServerError) + http.Error(w, fmt.Sprintf("Unable to read the data file (%s): %s", dataFile, err), http.StatusInternalServerError) return } - defer cFile.Close() //Ensure the file is closed when we are done. switch r.Method { case "POST": // Decode the JSON data comments := make([]comment, 0) - commentsDecoder := json.NewDecoder(cFile) - if err := commentsDecoder.Decode(&comments); err != nil { - http.Error(w, fmt.Sprintf("Unable to read comments from data file (%s): %s", dataFile, err), http.StatusInternalServerError) + if err := json.Unmarshal(commentData, &comments); err != nil { + http.Error(w, fmt.Sprintf("Unable to Unmarshal comments from data file (%s): %s", dataFile, err), http.StatusInternalServerError) return } // Add a new comment to the in memory slice of comments comments = append(comments, comment{Author: r.FormValue("author"), Text: r.FormValue("text")}) - // Truncate the file and Seek to the beginning of it - if err := cFile.Truncate(0); err != nil { - http.Error(w, fmt.Sprintf("Unable to truncate data file (%s): %s", dataFile, err), http.StatusInternalServerError) + // Marshal the comments to indented json. + commentData, err = json.MarshalIndent(comments, "", " ") + if err != nil { + http.Error(w, fmt.Sprintf("Unable to marshal comments to json: %s", err), http.StatusInternalServerError) return } - if r, err := cFile.Seek(0, 0); r != 0 && err != nil { - http.Error(w, fmt.Sprintf("Unable to seek to beginning of data file (%s): %s", dataFile, err), http.StatusInternalServerError) + + // Write out the comments to the file, preserving permissions + err := ioutil.WriteFile(dataFile, commentData, fi.Mode()) + if err != nil { + http.Error(w, fmt.Sprintf("Unable to write comments to data file (%s): %s", dataFile, err), http.StatusInternalServerError) return } - // Write out the json response - commentsEncoder := json.NewEncoder(cFile) - commentsEncoder.Encode(comments) - case "GET": // stream the contents of the file to the response - io.Copy(w, cFile) + io.Copy(w, bytes.NewReader(commentData)) default: // Don't know the method, so error