build-an-http-server / lesson-23.md
Lesson 23 · Building responses

Route by path

A real server answers different paths differently. Today you build a router that maps a method and path to a handler, so "/" and "/about" return different pages.

The goal

Register handlers keyed by method and path, and dispatch a request to the matching one.

Start here - the target
TO DO
Scenario: Dispatching to a handler by path
Givena router with GET "/" returning body "home" and GET "/about" returning body "about"
Whena request for GET "/about" is dispatched
Thenthe response body is "about"
Background

A server that returns the same body for every path is not much of a server. A router (often called a mux) fixes that: it holds a table of registered routes and picks the Handler whose method and path match the request. Handlers take a Request and return a Response — the two types the chapters built.

Key the table on both method and path from the very start, even though you only register GET routes today. It costs nothing now and means the next lessons — a 404 for unknown paths, a 405 for the wrong method — slot in without reworking the table’s shape.

Make it work
type Handler func(Request) Response
type Mux struct{ routes map[string]Handler }
func key(method, path string) string { return method + " " + path }
func (m *Mux) Handle(method, path string, h Handler) { m.routes[key(method, path)] = h }
func (m *Mux) dispatch(r Request) Response { return m.routes[key(r.Method, r.Path)](r) }
CheckpointDONE
Requests dispatch to a handler by method and path. Commit.