Cerebro is a Rust backend for a newsletter service, built while working through Zero To Production In Rust. The project is intentionally developed in public: the codebase is still evolving, and each stage adds another production-oriented backend capability.
The application currently exposes:
GET /health_checkfor service health checksPOST /subscriptionsfor accepting newsletter subscription form data
Subscriptions are persisted in PostgreSQL via SQLx. The service uses Actix Web, Tokio, structured tracing, database migrations and environment-aware configuration.
- Rust 2024 edition
- Actix Web
- Tokio
- PostgreSQL
- SQLx
- Docker for local Postgres
- tracing, tracing-subscriber and tracing-actix-web
Install:
- Rust and Cargo
- Docker
psqlsqlx-cli
Install SQLx CLI with Postgres support:
cargo install sqlx-cli --no-default-features --features postgres,rustlsRuntime configuration is stored in configuration.yaml.
For SQLx compile-time query checking, set DATABASE_URL. You can start from the
example file:
cp .env.example .envThe default local database URL is:
postgres://postgres:password000@localhost:5432/newsletterDo not commit .env; it is intentionally ignored.
Start Postgres and run migrations:
./scripts/init_db.shIf Postgres is already running and you only want to create/migrate the database:
SKIP_DOCKER=true ./scripts/init_db.shYou can also start the database manually with Docker:
docker run \
--name cerebro-postgres \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=password000 \
-e POSTGRES_DB=newsletter \
-p 5432:5432 \
-v cerebro-postgres-data:/var/lib/postgresql/data \
-d postgres \
postgres -N 1000If you use the manual Docker command, run migrations afterwards:
SKIP_DOCKER=true ./scripts/init_db.shLater you can stop and restart the same database container:
docker stop cerebro-postgres
docker start cerebro-postgresRun the application:
cargo runBy default the server listens on:
127.0.0.1:8000Check the health endpoint:
curl http://127.0.0.1:8000/health_checkCreate a subscription:
curl -i -X POST http://127.0.0.1:8000/subscriptions \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "name=app%20tester&email=app_tester%40mail.com"Run the test suite with a live local Postgres instance available:
DATABASE_URL=postgres://postgres:password000@localhost:5432/newsletter cargo testOr export DATABASE_URL once for the current shell session:
export DATABASE_URL=postgres://postgres:password000@localhost:5432/newsletter
cargo testThe integration tests create isolated test databases and run migrations against them.
Migrations live in migrations/ and are part of the application source.
Planned work:
- Deployment and production readiness
- Subscriber input validation
- Error handling
- Newsletter delivery
- API security
- Final polish
The deployment section will cover containerization, Docker image builds, SQLx offline mode, hierarchical configuration, database connectivity, image optimization and deployment to a managed platform.
This project is licensed under the MIT License. See LICENSE.
