Skip to content

2.07 REST API Implementation (Axum): Exit Evidence

Evidence · Step: 2.07 REST API Implementation (Axum)

Step 2.07 REST API Implementation (Axum)
Date 2026-11-15
Database PostgreSQL 18.6 (postgres:18-alpine) in Docker Desktop on Windows 11; postgres:18-alpine on GitHub’s ubuntu-latest in CI
Tools Axum 0.8, Postman CLI 1.75.0 (collection run and lint, not logged in)
Result Passed
Deliverable Where
The service crates/api (mezgeb-api): routes.rs (every operation), problem.rs (Problem Details), wire.rs (strict bodies), lib.rs (middleware, shutdown), auth.rs (interim tokens), config.rs
Database support Migration 0008: who opened an account and with which key; adjustment_requests (MZ009). Engine: accounts.rs, adjustments.rs, entries.rs, general_ledger_page, get_hold
Migrations for deployment mezgeb-cli migrate
Container Dockerfile (135 MB, non-root), the api profile in compose.yaml, deploy/tokens.dev.toml
Contract tests crates/api/tests/contract.rs, crates/api/tests/common
HTTP tests crates/api/tests/http.rs
Postman run in CI postman/collections/mezgeb-api-smoke; the api-container job in .github/workflows/ci.yml
Chaos through the API scripts/chaos-api.sh and its client, crates/api/examples/api_chaos.rs
Documentation docs/api/README.md, The service; db-schema.md, Adjustment requests

The work arrived in eight pull requests:

  1. the engine and migration 0008 (#73);
  2. mezgeb-cli migrate (#74);
  3. the service (#75);
  4. the contract and HTTP tests (#76);
  5. the container and the CI Postman run (#77);
  6. the chaos run through the API (#78);
  7. the documentation (#79);
  8. this evidence (#80).
Task Done
mezgeb-api crate: handlers call the templates and the engine All 42 operations of the v1-draft contract. Every route checks its scope first. Operations go through TemplateRegistry, one route per released template; a handler never builds lines. Accounts are named by ID or by chart reference. The offline endpoints answer 501 not_implemented, as the contract says
Middleware: request ID A UUIDv7 per request, or the caller’s X-Request-Id if it is a UUID (anything else is replaced, so it cannot forge log lines). Returned in X-Request-Id, and the instance of every problem
Middleware: body limit MEZGEB_BODY_LIMIT_BYTES, 64 KiB by default; a larger body is 400 invalid_request
Middleware: timeout After MEZGEB_REQUEST_TIMEOUT_MS (10 s) the client gets 503 busy with Retry-After. Writes run in their own tracked tasks, so the timeout never cancels a transaction: it commits or fails on its own
Middleware: access log without sensitive fields One JSON line per request: method, route pattern (/v1/accounts/{account_id}, never the path with its IDs), status, latency, request ID, client. No bodies, tokens, amounts, references or idempotency keys
Problem Details error mapping problem.rs: the 68 codes of the catalogue. Every PostingError, TemplateError and LedgerError maps to its code, with rule, line and role where the contract has them. An unexpected error is logged with its cause and answered with a bare 500 internal_error
Contract tests generated from the OpenAPI spec tests/contract.rs loads openapi/mezgeb.v1.yaml and builds a JSON Schema 2020-12 validator for every response of every operation. Each actual response is checked against the one the contract gives for its status

Interim authentication. The contract names OAuth 2.0, which step 2.08 builds. Until then the service reads static bearer tokens from a file of their SHA-256 hashes, each with a client, an actor and scopes, and every route checks its scope (default deny). Ownership checks (one client cannot see another’s accounts) are 2.08’s. The service already records who opened each account, ready for those checks.

Adjustments in two calls. The contract’s maker-checker adjustment needed somewhere for a request to wait between the two calls: the new adjustment_requests table. Approval posts with the key adjustment-{id}, so an approval cut off part-way is finished by the next one and posts once. Writing this evidence found that an approval the ledger refused (an account that would go negative) left the request stuck in approving. It now goes back to pending, and adjustments_need_a_second_person_and_post_once tests that.

Requirement Test Result
Postman collection runs in CI against a containerised service CI job api-container: docker compose --profile api up -d --build --wait (PostgreSQL, then mezgeb-cli migrate, then the service), then postman collection run postman/collections/mezgeb-api-smoke --bail failure Passes on #77: 15 requests, 21 assertions, 0 failed. Locally, two runs against the same database: 22 assertions each (the second also gets 409 for the existing 1010), 0 failed
A client timeout followed by a retry gives a single entry a_client_timeout_then_a_retry_gives_one_entry: another transaction holds the payer’s row, so the posting waits past a one-second timeout and the client gets 503 busy with Retry-After. The row is released; the posting commits; the retry with the same key gets 200, Idempotent-Replayed: true, and the same entry Passes; one entry for the key
Malformed, negative and float amounts and a huge payload are rejected with the right code bad_amounts_and_huge_payloads_are_refused. 10.255, ten, 1e3, 10,00 and "": 400 invalid_request, pointer /amounts/amount/amount. -5.00 and 0.00: 422 non_positive_amount, rule 2006. JSON number 10.5: 400. Unknown currency: 400. Unknown field: 400. 200 KB of metadata: 400 Passes; nothing posted
Graceful shutdown during load loses no acknowledged request graceful_shutdown_under_load_loses_no_acknowledged_request: 8 workers post for 3 seconds, then the server shuts down. Every 201 the clients received has exactly its entry in the journal, and the integrity report is clean Passes

The Postman collection generated in step 2.06 (mezgeb-api-v1) documents the contract, but it is not a test: its sample IDs are random. So CI runs a scripted collection instead, which does the following:

  • opens wallets, funds one and transfers;
  • replays the open and the transfer;
  • reuses a key with a different body (409 idempotency_key_reused) and finds the entry by its key;
  • checks the balance;
  • checks each refusal by its code;
  • uses a collection-level script to check that every error is Problem Details: content type, status, type matching code, and an instance.

100% of the spec’s endpoints pass contract tests. every_operation_meets_the_contract (tests/contract.rs) is one scenario through all 42 operations. Each response, success or error, is checked against the contract: status, required headers (Location, Idempotent-Replayed, Retry-After) and body schema. At the end the test lists any operation it never called, and fails if there is one. None is left.

The test found three mismatches while it was being written. Each was fixed on the side that was wrong:

  • Contract. openAccount returned a 200 replay that the contract did not document. The contract now has it, with Idempotent-Replayed.
  • Service. A repeated adjustment request answered 200, but the contract says 202. It now answers 202 both times, with the replay header the second time.
  • Test. The test reversed a P2P transfer after the payee had spent the money, and the reversal was refused with insufficient_funds. That is correct (ADR-0013). The test now asserts that refusal and reverses an entry that can be reversed.

The chaos test from step 1.15 re-runs through the API layer and comes out clean. scripts/chaos-api.sh is the 1.15 run with every request sent over HTTP. Each 10-second cycle runs 8 workers. A kill (SIGKILL) lands at a random moment in the middle:

  • odd cycles kill PostgreSQL;
  • even cycles kill mezgeb-api itself, which cuts off requests and transactions in flight with no graceful shutdown.

After each cycle, the check compares the client’s log with the journal. It then resolves every unknown outcome by retrying it through the API with the same key. One that had committed must come back as a replay of its entry; one that had not is posted now. Finally, every request must have had its effect once, the wallets must hold what was deposited, and mezgeb-cli verify --full must be clean. The script fails a cycle whose kill missed the load.

Run on 2026-11-15 on an idle machine: scripts/chaos-api.sh, 20 cycles, 8 workers, 10 seconds each. Columns as in step 1.15. During load is whether the load was still running when the kill landed. Back in is the time until PostgreSQL accepted queries again, or until the restarted service answered.

Cycle Killed After During load Sent Acked Rejected Unknown: committed / absent Back in Check Verify
1 postgres 2 s yes 217 152 11 1 / 53 7.0 s pass clean
2 api 3 s yes 191 146 5 2 / 38 0.8 s pass clean
3 postgres 4 s yes 407 309 16 0 / 82 5.7 s pass clean
4 api 5 s yes 380 332 16 2 / 30 0.3 s pass clean
5 postgres 6 s yes 577 498 26 1 / 52 5.7 s pass clean
6 api 6 s yes 447 396 27 2 / 22 0.2 s pass clean
7 postgres 7 s yes 697 629 29 1 / 38 5.7 s pass clean
8 api 5 s yes 390 341 17 1 / 31 0.8 s pass clean
9 postgres 7 s yes 696 614 30 1 / 51 5.7 s pass clean
10 api 5 s yes 472 422 18 2 / 30 0.8 s pass clean
11 postgres 7 s yes 707 633 27 3 / 44 6.5 s pass clean
12 api 3 s yes 254 208 6 2 / 38 0.8 s pass clean
13 postgres 5 s yes 589 496 22 1 / 70 5.6 s pass clean
14 api 4 s yes 308 264 12 3 / 29 0.2 s pass clean
15 postgres 7 s yes 751 644 33 1 / 73 5.7 s pass clean
16 api 8 s yes 717 661 40 1 / 15 0.2 s pass clean
17 postgres 8 s yes 869 800 37 1 / 31 5.9 s pass clean
18 api 2 s yes 175 130 5 3 / 37 0.2 s pass clean
19 postgres 6 s yes 676 603 27 1 / 45 7.2 s pass clean
20 api 8 s yes 390 358 16 1 / 15 0.2 s pass clean
All 9,910 8,636 420 30 / 824

The 854 unknown outcomes were resolved by retrying them through the API:

  • 30 had committed. Each retry returned its original entry, marked replayed.
  • 824 had not. They were posted, or refused, on the retry.

The final check over all 9,910 requests found 9,453 posted and 457 refused, with no unknowns and 0 violations. The final mezgeb-cli verify --full over 9,627 entries (18,926 lines) took 415 ms and is clean. PostgreSQL was back within 5.6–7.2 s, and the service within 0.2–0.8 s.

Clean. Every cycle passed its check and its full verification, with the kill inside the load each time.

  • When PostgreSQL is killed, requests in flight get a 5xx answer, or no answer, until it is back. The service’s pool reconnects by itself; the service is not restarted.
  • When the service is killed, PostgreSQL rolls back every transaction whose connection died. A request that had committed just before the kill is an unknown outcome that did commit. Its retry returns the same entry, marked replayed, so it is never posted twice.

An earlier 20-cycle run also passed: 5,725 requests, 0 violations, and a clean final verification. But it ran while the machine was compiling other work. In one cycle the kill landed only after the load had ended, and PostgreSQL took up to 90 s to come back. So the script now fails such a cycle, and the run above was made on an idle machine.

  • OAuth 2.0, token expiry, the clients table and ownership checks (one client only sees its own objects): step 2.08. Until then, run the service only where every token holder is trusted.
  • The scope check runs inside each handler. A body is parsed before it, so a caller without the scope who sends a malformed body gets 400, not 403. Step 2.08 moves the check ahead of the body.
  • Rate limiting (429): it is in the contract, but no limit is enforced yet. Step 2.08, with the clients table.
  • TLS: the service speaks plain HTTP and expects TLS in front of it.
  • The offline endpoints: steps 2.10 to 2.12.