Golang web services: a net/http API, no framework
Build an HTTP API with no framework, add middleware, and shut it down cleanly.
This section builds an HTTP JSON API with nothing but the standard library. Since Go 1.22, net/http routes on method and path (GET /tasks/{id}) and reads path parameters with r.PathValue. That routing was the main reason projects used to add a framework such as gin, echo or chi. Learning the plain version first means any framework you meet later reads as a thin layer over code you already understand.
The two lessons take you from a first handler to a server you could deploy: timeouts on http.Server (a zero-value server has none, so a slow client can hold a connection open forever), files embedded in the binary with //go:embed, middleware for logging and panic recovery, and a graceful shutdown that lets requests already in flight finish when the process is told to stop.
- 01An HTTP server with no frameworkGo 1.22 routes by method and path in the standard library. You do not need gin, echo, or chi.Graded challenge
- 02Middleware and graceful shutdownWrap every request with logging, panic recovery, and auth. Then stop the server without dropping anyone.Graded challenge
What you will be able to do
- Build a JSON API with method-and-path routing, where every handler sets headers, then the status, then the body, and returns straight after an error.
- Configure the server timeouts that a bare
http.ListenAndServeleaves out. - Wrap every route in middleware: a function that takes an
http.Handlerand returns one. - Stop the server on SIGINT or SIGTERM without dropping anyone, inside the grace period your platform allows (30 seconds by default on Kubernetes).
The order to study it in
- Before you start, be comfortable with structs, methods, interfaces and JSON from Core language, and with
contextfrom Concurrency: every request carries a context, and the shutdown is built on one. - Read An HTTP server with no framework first and get it answering curl requests. Then read Middleware and graceful shutdown, which wraps that same server.
- After this, Modules and packages and Databases fill in the rest, and the Practice projects rebuild the same API in three folder layouts so you can compare them.
What each lesson covers
By the end of this section you will be able to do everything below. Each lesson has runnable Go examples you can edit and run in the browser.
1. An HTTP server with no framework
- Build a JSON API with Go 1.22 method-and-path routing and
r.PathValue. - Write handlers that set headers, then the status, then the body, and return after an error.
- Explain why a bare
http.ListenAndServeis unsafe and configurehttp.Servertimeouts. - Embed a file or a static folder into the binary with
//go:embedand serve it withhttp.FileServerFS.
Remember: A handler reads the request and writes the response. Everything a framework adds is convenience on top of that one function shape.
- Build a JSON API with Go 1.22 method-and-path routing and
2. Middleware and graceful shutdown
- Write a middleware that wraps an
http.Handlerfor logging or panic recovery. - Explain when to panic and when to return an error.
- Build a server that shuts down gracefully on SIGINT without dropping in-flight requests.
Remember: A handler that wraps another handler is how you extend Go’s HTTP stack, and for an API it covers logging, auth, limits and recovery.
- Write a middleware that wraps an