- Use
ApiErroras the error type for API routes - Always use spaces instead of tabs, and run
cargo fmtbefore finishing a turn - Always use
cargo clippyinstead ofcargo check - The return type of an HTTP route should not be
HttpResponseif possible; always prefer more specific types- Use
web::Json<T>for JSON-encoded response - Use
()for no content
- Use
- Prefer
ApiErrorvariants:ApiError::Requestinstead ofApiError::InvalidInputApiError::Authinstead ofApiError::CustomAuthenticationApiError::Internalfor database errors, 3rd party service errors, anything else internal- Use
anyhow!to construct a value forInternal,Request, andAuthvariants
- Error messages (both for errors and exceptions) must be formatted as per the Rust API guidelines:
- lowercase message
- no trailing punctuation
- wrap code items e.g. type names in backticks
- Prefer
internal_context,request_contextwhen attaching context to an existing error (like Anyhowcontext) - Prefer importing
anyhow::Resultand usingResult<T>instead ofanyhow::Result<T> - Prefer
anyhow::Ok(value)instead ofOk::<_, anyhow::Error>(value)when an explicit Anyhow result type is needed - All operations should ideally have some context attached
- Database operations can have a message like
.internal_context("fetching XYZ")
- Database operations can have a message like
- You can perform real-time queries against the databases in the Docker Compose
docker exec labrinth-postgres psql -c "select 1"docker exec labrinth-redis redis-cli flushalldocker exec labrinth-clickhouse clickhouse-client "select 1"- On some machines, you may have to use
podmaninstead ofdocker- check which one is available first
- Hardcoded credentials for admin:
Authorization: Bearer mra_adminfor default admin userAuthorization: Bearer mra_userfor a regular userModrinth-Admin: feedbeefas admin key
- When using
sqlx::queryetc. always use the macro form likesqlx::query!orsqlx::query_scalar!- never the plain function form. Avoid usingquery_as!. - Do not use
()as an error type for operations, unless you have a very good reason. Either make a new error type, or useanyhow::Error. - Do not run
cargo test, even for a single specific test, unless explicitly prompted to by the user, since it takes a long time to run. - You can force a search reindex by:
- Running
cd apps/labrinth && cargo run -p labrinth -- --run-background-task index-search(prefer this if backend is running locally) - Hitting the force reindex admin endpoint
- Running
- To seed the database locally:
psql postgresql://labrinth:labrinth@localhost/labrinth -f apps/labrinth/fixtures/labrinth-seed-data-202508052143.sql - When writing
sqlxqueries, preferr#raw strings over escaping quotes - When interacting with the Postgres database, prefer using a
ro_pool: ReadOnlyPgPoolwhen performing read-only operations - After writing a SQL query, run
EXPLAIN ANALYZEto see how long the query would take to run. Report to the user the estimated performance impact of the query. - When explicitly asked to check query timings, suggest populating the local database with representative dummy data and benchmarking against the populated database. Do not draw performance conclusions from an empty database; ask for confirmation before resetting or replacing existing data.