Networking and services
A service is a handler, a client, and the limits around both. These lessons build an HTTP server and its routes, middleware, a client with a timeout, a JSON API, HTML from templates and a shutdown that waits for the requests in flight. They then add the storage side: database/sql, transactions, migrations, CORS and the timeouts that protect the process.
8.1
An HTTP server with net/http
http.ListenAndServe, a handler function, the request and the response, and a server that runs
Open
8.2
Handlers, ServeMux and patterns
ServeMux, method and path patterns, r.PathValue and a 404 written by hand
Open
8.3
Middleware
a handler that wraps a handler, logging, a header and the chain in order
Open
8.4
An HTTP client
http.Get, a timeout, a custom Transport, the response body closed and an error checked
Open
8.5
A JSON API by hand
decode a request body, encode a response, the status codes and a handler per route
Open
8.6
HTML from templates
html/template in a handler, a layout, escaping and a page that renders data
Open
8.7
Graceful shutdown
signal.NotifyContext, Server.Shutdown, a context deadline and an in-flight request that finishes
Open
8.8
database/sql
sql.Open, a driver named in words, Query and QueryRow, Scan and the pool settings
Open
8.9
Transactions and prepared statements
Begin, Commit and Rollback, a prepared statement and a transaction that fails halfway
Open
8.10
Schema migrations
a version table, an up step applied once and a migration that runs at start-up
Open
8.11
CORS and a dev proxy
the CORS headers, a preflight request, a proxy in development and the same origin in production
Open
8.12
Timeouts, body limits and slow clients
server timeouts, http.MaxBytesReader, a slow client and the limit that protects the process
Open