This example demonstrates a user management API using Sword's web controllers. It includes CRUD operations for users, request validation, and PostgreSQL integration.
- Ensure Docker is installed.
- Navigate to the example's directory:
cd examples/web - Start PostgreSQL:
docker compose up -d - Run the application:
cargo run
The API will be available at http://localhost:8081/api/users.
A simple vanilla JS client is included in public/ and served by the app itself:
- Open http://localhost:8081/static/ to load
index.htmlandapp.js.
It consumes the API from the browser (same origin, no CORS):
GET /api/users- renders the user listPOST /api/users- creates a user via the formDELETE /api/users/{id}- deletes a userGET /api/sse/countdown- streams the countdown events viaEventSource
Note: the user CRUD operations require PostgreSQL (
docker compose up -d). The SSE countdown works without a database.
The example also exposes an SSE stream at GET /api/sse/countdown:
curl -N http://localhost:8081/api/sse/countdownThis returns a text/event-stream response with a countdown event every 250ms followed by a final done event.
Note: SSE connections are long-lived. If
request-timeoutis enabled in your config, the global timeout layer will terminate the stream, so leave it disabled or set a generous timeout for routes that stream events.