This repository was archived by the owner on Jul 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Update Go server #30
Closed
Closed
Update Go server #30
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,12 +64,17 @@ func handleComments(w http.ResponseWriter, r *http.Request) { | |
return | ||
} | ||
|
||
// Write out the json response | ||
commentsEncoder := json.NewEncoder(cFile) | ||
commentsEncoder.Encode(comments) | ||
// Pretty print the JSON, write to file | ||
var encodedComments, _ = json.MarshalIndent(comments, "", " ") | ||
cFile.WriteAt(encodedComments, 0) | ||
|
||
// send the encoded comments to the response | ||
w.Header().Set("Content-Type", "application/json") | ||
w.Write(encodedComments) | ||
|
||
case "GET": | ||
// stream the contents of the file to the response | ||
w.Header().Set("Content-Type", "application/json") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, sorry I missed that. |
||
io.Copy(w, cFile) | ||
|
||
default: | ||
|
@@ -81,5 +86,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)) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3 problems here:
var encodedComment...
vsencodedComments :=
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MarshalIndent
withEncode
. It also felt a bit silly to write the file and then immediately read it (my first pass was to justio.Copy(w, cFile)
like in the GET handler).Like I said, I haven't written Go. If we could get this pedantically idiomatic and keep the JSON formatting that would be awesome! Either way, nobody should be taking these servers and putting them into production ;) , so it's not the end of the world if we don't get it perfect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW: The issue with using {Un,}Marhsal is that they read in the entire source and return byte buffers. So, pretend the json file is 50MB in size. So for each post request that would use the Marshal/Unmarshal pair (vs the encoders) the file would need to be read into a []byte, which would be ~50MB. Then you need to UnMarshal that into the []comment, which would be another data structure of ~50 MB modulo JSON, and then another 50MB+ []byte get's created when you MarshalIndent the updated comment back to JSON. So memory usage is ~150MB doing it this way. I haven't run any specific tests, but the pure Encoder/Decoder version of this will use ~50MB total, most of it because we realize the fully constructed []comment.
However this is an example, and ... AFAICT the other examples are doing it by just reading everything into ram / constructing arbitrary sized objects in ram and then the reverse to write data back out. So maybe I'm being too picky about the example?
I'd be happy to submit a pull request that switches everything to using UnMarshal/MarshalIndent instead if you think it's best.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you? That would be awesome. I got my fill of writing Go for the time being :D
The servers implementations are by far the least important part of the whole thing here. The variety is intended to make sure anybody can run something without having to muck around too much with installing different thing (eg, shipping just a Go server would be bad). Keeping the behavior consistent across these servers is important to me though because it makes it easier to help people. No saying "oh you're using the Go server and it does things slightly differently... normally you wouldn't have to parse a single long line of JSON in your head".
So if you wouldn't mind doing that, I'd appreciate it. If you wanted to add comments saying that "normally you wouldn't do this, you would just do X, Y, Z" instead, that's totally fine.