|
@@ -35,6 +35,36 @@ const dataFile = "./comments.json"
|
|
|
|
|
|
var commentMutex = new(sync.Mutex)
|
|
|
|
|
|
+func updateComments(w http.ResponseWriter, r *http.Request, commentData []byte, fi os.FileInfo) {
|
|
|
+
|
|
|
+ var comments []comment
|
|
|
+ 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
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ comments = append(comments, comment{ID: time.Now().UnixNano() / 1000000, Author: r.FormValue("author"), Text: r.FormValue("text")})
|
|
|
+
|
|
|
+
|
|
|
+ commentData, err := json.MarshalIndent(comments, "", " ")
|
|
|
+ if err != nil {
|
|
|
+ http.Error(w, fmt.Sprintf("Unable to marshal comments to json: %s", err), http.StatusInternalServerError)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ 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
|
|
|
+ }
|
|
|
+
|
|
|
+ w.Header().Set("Content-Type", "application/json")
|
|
|
+ w.Header().Set("Cache-Control", "no-cache")
|
|
|
+ io.Copy(w, bytes.NewReader(commentData))
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
func handleComments(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
@@ -58,33 +88,7 @@ func handleComments(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
switch r.Method {
|
|
|
case "POST":
|
|
|
-
|
|
|
- var comments []comment
|
|
|
- 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
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- comments = append(comments, comment{ID: time.Now().UnixNano() / 1000000, Author: r.FormValue("author"), Text: r.FormValue("text")})
|
|
|
-
|
|
|
-
|
|
|
- commentData, err = json.MarshalIndent(comments, "", " ")
|
|
|
- if err != nil {
|
|
|
- http.Error(w, fmt.Sprintf("Unable to marshal comments to json: %s", err), http.StatusInternalServerError)
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- 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
|
|
|
- }
|
|
|
-
|
|
|
- w.Header().Set("Content-Type", "application/json")
|
|
|
- w.Header().Set("Cache-Control", "no-cache")
|
|
|
- io.Copy(w, bytes.NewReader(commentData))
|
|
|
+ updateComments(w, r, commentData, fi);
|
|
|
|
|
|
case "GET":
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
@@ -93,7 +97,7 @@ func handleComments(w http.ResponseWriter, r *http.Request) {
|
|
|
io.Copy(w, bytes.NewReader(commentData))
|
|
|
|
|
|
default:
|
|
|
-
|
|
|
+
|
|
|
http.Error(w, fmt.Sprintf("Unsupported method: %s", r.Method), http.StatusMethodNotAllowed)
|
|
|
}
|
|
|
}
|