7 Designing an HTTP Server
This chapter covers
- Structuring, writing, running, and testing an idiomatic HTTP server.
- Using middleware and handler chaining patterns to minimize repetitive code and add extra functionality to HTTP handlers without changing their code.
- Receiving and responding with the JSON format.
Imagine you're about to embark on an exciting journey at Bite, a start-up with ambitions to transform the world of link management. Your first project at Bite is a URL shortener HTTP server that shortens long URLs based on a user-specified short key. When a user requests the shortened URL, the server redirects the user to the long URL. For example, users might use the short key "go" to go to "https://go.dev".
This chapter explores how to write an idiomatic, maintainable, and testable JSON API from scratch using the Go Standard Library. And it guides you through in building the URL shortener JSON API using Go's net/http package, following your introduction to handlers, requests, and responses in the last chapter.
Note
You can find the source code for this chapter at https://github.com/inancgumus/effective-go/tree/main/ch07.