Part 2: Upgrading the server
Tests, auth middleware, a service layer, and request timeouts
In part 1 we built a small payment links API.
In part 2 we are going to do the things everyone promises they will do “after launch” and then never do until something catches fire.
We will add:
- A service layer so handlers stop doing business decisions
- Auth middleware with a simple API key
- Request scoped timeouts so slow work does not hang forever
- Proper handler tests with
httptest
Same rule as before. No magic. Code that compiles.
Step 1: Restructure into a sane layout
Keep it small, but separate responsibilities.
Create this structure: