How inwerejosic/actix-blog-api works
Purpose and entry point
Rust REST API for a blog platform. It exposes session-authenticated user, post, tag, and post–tag relationship endpoints over HTTP. Application startup, dependency initialization, middleware, and route registration are centered in src/main.rs.
HTTP/API layer
Actix-web handlers implement login/logout/current-user flows plus CRUD-oriented user, post, and tag operations. Handlers receive request data, enforce endpoint-level behavior, invoke persistence operations, and translate failures into HTTP responses.
• src/handlers.rs
• src/main.rs
• src/error.rs
Authentication and session boundary
Authentication uses bcrypt-backed password verification and cookie sessions. Session state is persisted in Redis rather than process memory, making authenticated requests depend on both the signed session cookie/secret and Redis availability. Protected operations such as /me resolve the authenticated user through this boundary.
• src/auth.rs
• src/handlers.rs
• src/main.rs
Persistence and schema lifecycle
PostgreSQL is the system of record for users, blog posts, tags, the blog_tag junction, and schema-defined comments. SQLx manages database access; startup creates or prepares the schema automatically. The database module is the persistence boundary shared by HTTP handlers.
• src/db.rs
• src/models.rs
• src/main.rs
Domain model and relationships
Typed request/response and database-facing entities represent users, posts, tags, and authentication payloads. Posts belong to users; tags attach to posts through a many-to-many junction. Post retrieval includes its tags, while attachment and detachment mutate the relation.
• src/models.rs
• src/handlers.rs
• src/db.rs
Error propagation
Application-specific errors centralize conversion of authentication, validation, database, and missing-resource failures into API responses. This keeps handlers focused on request orchestration while preserving consistent failure semantics.
• src/error.rs
• src/handlers.rs
Runtime and deployment topology
The API runs as a Rust/Actix process on port 8080, requiring DATABASE_URL, REDIS_URL, and SECRET_KEY. Docker Compose provisions API, PostgreSQL 16, and Redis 7 with persistent volumes. The Docker image is built from Dockerfile; GitHub Actions publishes it to GHCR and Docker Hub.
• Dockerfile
• docker-compose.yaml
• .github/workflows/docker-publish.yaml
• src/main.rs
Main entry points: handlers.rs Endpoint handlers · main.rs Actix application · db.rs Database module · docker-compose.yaml Docker Compose
Components 12
HTTP API
- Actix application · Initializes dependencies, middleware, and routes on port 8080.
src/main.rs - Endpoint handlers · Implements session, user, post, tag, and relationship endpoints.
src/handlers.rs - API error mapping · Converts application failures into consistent HTTP responses.
src/error.rs
Application Core
- Authentication · Verifies passwords and resolves authenticated users from sessions.
src/auth.rs - Domain models · Defines users, posts, tags, auth payloads, and request/response types.
src/models.rs - Database module · Shared persistence boundary and schema preparation logic.
src/db.rs
Stateful Services
- PostgreSQL 16 · Stores users, posts, tags, blog_tag relations, and comments.
docker-compose.yaml - Redis 7 · Persists cookie-session state outside the API process.
docker-compose.yaml
Build & Deployment
- Docker Compose · Provisions the API, PostgreSQL, Redis, and persistent volumes.
docker-compose.yaml - API container image · Builds the Rust API runtime image.
Dockerfile - Image publishing · Publishes the container image to GHCR and Docker Hub.
.github/workflows/docker-publish.yaml
Other components
- API clients
Connections 15
- API clients → Actix application: HTTP requests
- Actix application → Endpoint handlers: registers routes
- Actix application → Authentication: configures sessions
- Actix application → Database module: initializes pool and schema
- Endpoint handlers → Authentication: login and protected identity
- Endpoint handlers → Database module: CRUD and tag relations
- Endpoint handlers → Domain models: uses payloads and entities
- Endpoint handlers → API error mapping: maps failures
- Authentication → Redis 7: reads and writes session state
- Database module → PostgreSQL 16: SQLx queries and schema
- Database module → Domain models: hydrates entities
- Docker Compose → API container image: runs API image
- Docker Compose → PostgreSQL 16: provisions
- Docker Compose → Redis 7: provisions
- Image publishing → API container image: builds and publishes