Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jul 19, 2019. It is now read-only.

Updates based on #30 #34

Merged
merged 1 commit into from
Feb 26, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions server.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
Expand All @@ -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
Expand Down