Learn the basics of constructing a HTTP API with focus on logging and error handling from the very start. We'll start from hello world and evolve into a more advanced API.
Each step is represented as a diff, so you can follow along
with the changes. But the contents of the entire file is
always available for each change. I recommend you to type in
those changes manually, copy-paste never taught nobody
nothin. I've provided tests for all changes but you'll have to
explore those on your own. Also each change comes with a
components diagram showing the overall design.
If the
comments are somewhat sparse it's because this material was
used in a course where I explained each concept in more
detail. Please let me know
which part might benefit from more discussion/explanation.
This tutorial was prepared with Go 1.12 as a module with the following commands
mkdir -p $HOME/tutorial/cmd/app touch $HOME/tutorial/cmd/app/main.go cd $HOME/tutorial go mod init tutorialWhen you follow along run the tests and compile using
cd $HOME/tutorial go test ./...
Table of contents
- Initial revision
- Use own router
- Expose log.Fatal
- Use http.Handle
- Working with books
- Decouple routing rules from main func
- Respond with JSON
- Lowercase JSON attributes
- Use own logger for errors
- Log startup progress
- Handle unknown HTTP method
- Use more powerful router
- Log requests
- Log All requests
- Prepare for storing books
- POST adds a new book