2.7 KiB
quotesdb — Local Development Guide
Prerequisites
- Nix with Flakes enabled
direnv(optional, for auto-loading the dev shell)
Enter the dev shell from the repo root:
nix develop
This gives you: cargo, rustc, rust-analyzer, trunk, wasm-bindgen-cli, tofu, wrangler, jq.
Running the API server
# From quotesdb/
cargo run
The server listens on http://localhost:3000. The SQLite database is created at ./quotesdb.sqlite on first run.
Override the database path:
DATABASE_URL=/tmp/test.sqlite cargo run
Running the UI dev server
# From quotesdb/
trunk serve
Opens http://localhost:8080. The Trunk proxy forwards /api/* to the native API server on port 3000.
Run both together:
# Terminal 1
cargo run
# Terminal 2
trunk serve
Environment variables
Copy .env.example to .env and adjust as needed:
cp .env.example .env
.env is gitignored — never commit it.
Running tests
# From quotesdb/
cargo test
Integration tests in tests/ spin up a real API server against a temporary SQLite database.
D1 schema migrations
Migrations are SQL files applied via wrangler d1 execute. The schema lives at infra/schema.sql.
Apply to local D1 (development)
wrangler d1 execute quotesdb --file infra/schema.sql --local
Apply to remote D1 (production)
wrangler d1 execute quotesdb --file infra/schema.sql --remote
Requires CLOUDFLARE_API_TOKEN to be set (or wrangler login).
First-time D1 setup
D1 must be created before the Worker can bind to it:
# Create D1 via OpenTofu (run once)
cd infra/
tofu init
tofu apply -target=cloudflare_d1_database.quotesdb
# Apply schema
wrangler d1 execute quotesdb --file schema.sql --remote
After D1 exists, run tofu apply without the -target flag to apply the full plan.
Building the WASM API binary
cargo build --release --bin api --target wasm32-unknown-unknown
Output: target/wasm32-unknown-unknown/release/api.wasm
Building the WASM UI
trunk build --release
Output: dist/
Cloudflare Turnstile CAPTCHA
The submit form includes a Cloudflare Turnstile CAPTCHA widget. In production,
the API verifies tokens using the TURNSTILE_SECRET_KEY environment variable.
Local development: The API server skips CAPTCHA verification when
TURNSTILE_SECRET_KEY is not set. You can submit quotes without completing
the CAPTCHA challenge.
To test CAPTCHA locally, set the secret key:
export TURNSTILE_SECRET_KEY="your-secret-key"
cargo run
Obtain the secret key from:
cd infra && tofu output -raw turnstile_secret_key