From 45c8e6640bdc384b3496c3c82b001ef58e4e9773 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20O=E2=80=99Shannessy?= Date: Fri, 27 Feb 2015 13:40:47 -0800 Subject: [PATCH] Improve Go server - log to stdout when starting the server - send updated data on POST - set content type headers --- server.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/server.go b/server.go index 86a53e5f..74dd06a1 100644 --- a/server.go +++ b/server.go @@ -68,7 +68,11 @@ func handleComments(w http.ResponseWriter, r *http.Request) { return } + w.Header().Set("Content-Type", "application/json") + io.Copy(w, bytes.NewReader(commentData)) + case "GET": + w.Header().Set("Content-Type", "application/json") // stream the contents of the file to the response io.Copy(w, bytes.NewReader(commentData)) @@ -81,5 +85,6 @@ func handleComments(w http.ResponseWriter, r *http.Request) { func main() { http.HandleFunc("/comments.json", handleComments) http.Handle("/", http.FileServer(http.Dir("./public"))) + log.Println("Server started: http://localhost:3000") log.Fatal(http.ListenAndServe(":3000", nil)) }