All topics / Web Services With Only net/http

Web Services With Only net/http

Build real Go web services using nothing but the standard library: the Handler/ServeMux/Server mental model, routing by hand, reading requests and writing JSON, middleware as plain wrappers, a full JSON REST API with no framework, project structure with context and graceful shutdown, and exactly what Gin/Echo/chi add on top. The foundation every Go framework is built on.

  1. The net/http Mental Model The whole net/http architecture in three types — Handler, ServeMux, Server (plus the HandlerFunc adapter) — and the one mental model that ties them together: the mux routes a request to a handler, the handler writes the response.
  2. Handlers & Routing by Hand Map URL patterns to handlers with HandlerFunc and the Go 1.22 method+path router — GET/POST/{id} patterns, r.PathValue, the old switch-on-method way, subtrees, and conflict panics.
  3. Reading Requests, Writing JSON Pull data out of a request with PathValue, query, headers, and JSON body decoding, then write JSON back in the right header-status-body order — including the superfluous-WriteHeader trap and validation by hand.
  4. Middleware Is Just a Wrapper Middleware in net/http is a function that takes an http.Handler and returns a new one. Build logging and auth middleware, chain them by wrapping, and pass data down with context.
  5. A JSON REST API With No Framework Assemble routing, JSON I/O, and middleware into a full CRUD messages API on the Go 1.22 mux — five handlers over one in-memory store, mutex-guarded, no framework anywhere.
  6. Structure, Context & Graceful Shutdown Wire dependencies onto a struct with handler methods and a routes() seam, carry cancellation and request-scoped values through context, harden http.Server with timeouts, and shut down cleanly by draining in-flight requests.
  7. What the Frameworks Add Map Gin, Echo, and chi back onto the net/http you just built — router, context, middleware, binding — and decide honestly whether you even need a framework now that the stdlib mux does method+path routing.