11package main
22
33import (
4+ "bytes"
45 "encoding/json"
56 "fmt"
67 "io"
8+ "io/ioutil"
79 "log"
810 "net/http"
911 "os"
@@ -29,48 +31,46 @@ func handleComments(w http.ResponseWriter, r *http.Request) {
2931 // Stat the file, so we can find it's current permissions
3032 fi , err := os .Stat (dataFile )
3133 if err != nil {
32- http .Error (w , fmt .Sprintf ("Unable to stat data file (%s): %s" , dataFile , err ), http .StatusInternalServerError )
34+ http .Error (w , fmt .Sprintf ("Unable to stat the data file (%s): %s" , dataFile , err ), http .StatusInternalServerError )
3335 return
3436 }
3537
36- // Open the file Read/Write
37- cFile , err := os . OpenFile (dataFile , os . O_RDWR , fi . Mode () )
38+ // Read the comments from the file.
39+ commentData , err := ioutil . ReadFile (dataFile )
3840 if err != nil {
39- http .Error (w , fmt .Sprintf ("Unable to open data file (%s): %s" , dataFile , err ), http .StatusInternalServerError )
41+ http .Error (w , fmt .Sprintf ("Unable to read the data file (%s): %s" , dataFile , err ), http .StatusInternalServerError )
4042 return
4143 }
42- defer cFile .Close () //Ensure the file is closed when we are done.
4344
4445 switch r .Method {
4546 case "POST" :
4647 // Decode the JSON data
4748 comments := make ([]comment , 0 )
48- commentsDecoder := json .NewDecoder (cFile )
49- if err := commentsDecoder .Decode (& comments ); err != nil {
50- http .Error (w , fmt .Sprintf ("Unable to read comments from data file (%s): %s" , dataFile , err ), http .StatusInternalServerError )
49+ if err := json .Unmarshal (commentData , & comments ); err != nil {
50+ http .Error (w , fmt .Sprintf ("Unable to Unmarshal comments from data file (%s): %s" , dataFile , err ), http .StatusInternalServerError )
5151 return
5252 }
5353
5454 // Add a new comment to the in memory slice of comments
5555 comments = append (comments , comment {Author : r .FormValue ("author" ), Text : r .FormValue ("text" )})
5656
57- // Truncate the file and Seek to the beginning of it
58- if err := cFile .Truncate (0 ); err != nil {
59- http .Error (w , fmt .Sprintf ("Unable to truncate data file (%s): %s" , dataFile , err ), http .StatusInternalServerError )
57+ // Marshal the comments to indented json.
58+ commentData , err = json .MarshalIndent (comments , "" , " " )
59+ if err != nil {
60+ http .Error (w , fmt .Sprintf ("Unable to marshal comments to json: %s" , err ), http .StatusInternalServerError )
6061 return
6162 }
62- if r , err := cFile .Seek (0 , 0 ); r != 0 && err != nil {
63- http .Error (w , fmt .Sprintf ("Unable to seek to beginning of data file (%s): %s" , dataFile , err ), http .StatusInternalServerError )
63+
64+ // Write out the comments to the file, preserving permissions
65+ err := ioutil .WriteFile (dataFile , commentData , fi .Mode ())
66+ if err != nil {
67+ http .Error (w , fmt .Sprintf ("Unable to write comments to data file (%s): %s" , dataFile , err ), http .StatusInternalServerError )
6468 return
6569 }
6670
67- // Write out the json response
68- commentsEncoder := json .NewEncoder (cFile )
69- commentsEncoder .Encode (comments )
70-
7171 case "GET" :
7272 // stream the contents of the file to the response
73- io .Copy (w , cFile )
73+ io .Copy (w , bytes . NewReader ( commentData ) )
7474
7575 default :
7676 // Don't know the method, so error
0 commit comments