Overview
Labrinth is Modrinth’s backend API service, written in Rust using the Actix-Web framework. It powers all Modrinth clients (web, desktop, mobile) with a RESTful API. Location:apps/labrinth/Language: Rust (Edition 2024, v1.90.0+)
Framework: Actix-Web 4.x
Architecture
Labrinth follows a layered architecture:Directory Structure
Key Technologies
Web Framework: Actix-Web
Actix-Web is a high-performance, actor-based web framework.src/main.rs
- Actor-based concurrency model
- Async/await with Tokio runtime
- Middleware support (CORS, logging, rate limiting)
- WebSocket support
- OpenAPI documentation generation
Database: PostgreSQL + SQLx
PostgreSQL 15 is the primary database, accessed via SQLx. SQLx Features:- Compile-time query verification
- Async/await support
- Connection pooling
- Migrations support
Example Query
Analytics: ClickHouse
ClickHouse stores analytics events (downloads, views, searches). Location:src/clickhouse/
Example: Track Download
Cache: Redis
Redis is used for:- Session storage
- Rate limiting
- Temporary data caching
- Real-time counters
Example: Rate Limiting
Search: Meilisearch
Meilisearch provides fast, typo-tolerant search. Location:src/search/
File Storage: S3
Files (mod JARs, images, etc.) are stored in S3-compatible object storage. Location:src/file_hosting/
API Routing
Routes are organized by version and resource:src/routes/v3/mod.rs
Route Example
src/routes/v3/projects.rs
Authentication & Authorization
Session-Based Auth
Users authenticate via GitHub OAuth, with sessions stored in Redis.src/auth/session.rs
API Token Auth
API tokens (mrp_...) for programmatic access.
Permission Checks
src/auth/checks.rs
Background Jobs
Background tasks run in queues for async processing. Location:src/queue/
Testing
Running Tests
Test Structure
Location:src/test/
Local Development
See Local Setup for complete instructions.Quick Start
http://localhost:8000
Accessing Services
Pre-PR Checks
Before opening a pull request:1
Run Clippy
Zero warnings required - CI will fail otherwise.
2
Format Code
3
Prepare SQLx Cache
.sqlx/ with query metadata for offline builds.4
Run Tests (optional)
Tests take a long time, so only run if you’ve changed core logic:
API Documentation
Labrinth usesutoipa for OpenAPI documentation.
Swagger UI: http://localhost:8000/docs (when running locally)
Deployment
Labrinth is deployed as a Docker container.Building the Docker Image
Release Profile
Production builds use therelease-labrinth profile:
Cargo.toml
Environment Variables
Key environment variables (see.env.docker-compose for complete list):
Common Tasks
Adding a New Endpoint
1
Create Route Handler
src/routes/v3/my_resource.rs
2
Register in Module
src/routes/v3/mod.rs
3
Add Tests
Adding a Database Migration
Next Steps
Local Setup
Complete guide to running Labrinth locally
Testing
Learn about testing strategies
API Documentation
Full API reference
Deployment
Production deployment guide
