forked from reactjs/react-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
96 lines (81 loc) · 2.69 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"sync"
)
type comment struct {
Author string `json:"author"`
Text string `json:"text"`
}
const dataFile = "./comments.json"
var commentMutex = new(sync.Mutex)
// Handle comments
func handleComments(w http.ResponseWriter, r *http.Request) {
// Since multiple requests could come in at once, ensure we have a lock
// around all file operations
commentMutex.Lock()
defer commentMutex.Unlock()
// 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 the data file (%s): %s", dataFile, err), http.StatusInternalServerError)
return
}
// Read the comments from the file.
commentData, err := ioutil.ReadFile(dataFile)
if err != nil {
http.Error(w, fmt.Sprintf("Unable to read the data file (%s): %s", dataFile, err), http.StatusInternalServerError)
return
}
switch r.Method {
case "POST":
// Decode the JSON data
comments := make([]comment, 0)
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")})
// 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
}
// 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
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-cache")
io.Copy(w, bytes.NewReader(commentData))
case "GET":
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-cache")
// stream the contents of the file to the response
io.Copy(w, bytes.NewReader(commentData))
default:
// Don't know the method, so error
http.Error(w, fmt.Sprintf("Unsupported method: %s", r.Method), http.StatusMethodNotAllowed)
}
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
http.HandleFunc("/comments.json", handleComments)
http.Handle("/", http.FileServer(http.Dir("./public")))
log.Println("Server started: http://localhost:" + port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}