A URL Shortener
This project builds a working URL shortener in the same way as the rest of this guide. It uses no framework, no driver, and no dependency that hides how the code works. The project writes its own Postgres client, with the wire protocol from the cookbook recipe. The slug logic and the HTTP routes are pure functions over bytes, so most of these chapters run in your browser. The last chapter assembles the parts into one file that you run against a real database on your machine.
This project is a build for instruction, for a demonstration on your own machine. Do not deploy it as a public service. It quotes values into SQL, and does not use protocol parameters. It sends cleartext auth on loopback, serves one request at a time, and issues slugs that are easy to guess. Each chapter names its shortcut at the point where it takes it.
- A database driver is a client library that you can write. The driver connects, authenticates, sends SQL and reads rows, all as messages over one socket.
- When a query fails, you do not lose the connection. Postgres reports the error and then returns to ready. A client that reads the two messages can continue.
- A short link is base62 arithmetic on the row id, not a random string that you must check for collisions.
- Handlers that take request bytes and return response bytes need no server for a test. The same store seam that made the ORM testable also works for a web service.
5 chapters.
- What You Will BuildA URL shortener with real storage, and the two decisions that control all the chapters after this one.
- A Postgres ClientStartup, password auth, a row iterator and error recovery, with tests against scripted bytes.
- Slugs Are ArithmeticBase62 over the row id, and the validation that a redirect service needs.
- Routes Over a SeamAll five CRUD routes as one pure function, with tests against an in-memory store.
- The ServerThe client, the slugs and the routes put together into one file, and run against a real Postgres.