This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
http-api-rs is a REST API template built in Rust using Actix Web. It demonstrates best practices for structuring a production-ready API with database integration, OpenAPI documentation, and health checking.
make dev- Run the dev server with an in-memory SQLite database (listens on port 9090)cargo watch -x 'run'- Watch for file changes and auto-restart the server (requirescargo install cargo-watch)make test- Run all testsmake test -- --test-threads=1- Run tests sequentially (useful for debugging)
make lint- Run cargo clippy to check code qualitymake fix- Auto-fix clippy warnings
make migrate- Create SQLite database and apply all migrationsmake cleandb- Delete the SQLite database filemake entities- Regenerate SeaORM entity files from the database schema
cargo build --release- Build optimized release binarymake start- Run the release binarymake health- Health check the running server
PORT- Server port (default: 9090)DATABASE_URL- Database connection string (default:sqlite::memory:)RUST_LOG- Log level (see env_logger docs)
Main Application (src/main.rs)
- Sets up Actix Web server with Logger middleware
- Mounts API routes under
/apiscope - Configures OpenAPI/Swagger UI at
/swagger-ui/ - Root route
/returns "Hello World!"
Modules
src/env.rs- Environment variable parsing and defaultssrc/health.rs- Health check endpoint that validates API and database connectivitysrc/entities/- SeaORM entity models (auto-generated from migrations)
ORM: SeaORM (sea_orm v1.1.19)
- Supports SQLite, MySQL, PostgreSQL via feature flags
- Entities are auto-generated from migrations
- Version 1.x: Major upgrade from 0.12.x with enhanced query capabilities, refined insertion workflows, and improved partial model functionality
Migrations: Located in migration/ workspace member
- Uses SeaORM migration system
- Currently has one migration:
m20220101_000001_create_audit_table - To add migrations:
- Run
cargo run -- migrate generate MIGRATION_NAMEin themigration/directory - Edit the generated file to define schema changes
- Run
make migrateto apply
- Run
Health Check Implementation (src/health.rs)
- Verifies API connectivity (always Operational)
- Checks database connection and queries (can be Critical or Deminished)
- The health endpoint performs an actual database write to the audit table to validate full connectivity
Swagger/OpenAPI
- Generated via Utoipa decorators on handler functions
- Accessible at
http://localhost:9090/swagger-ui/ - Update by adding/modifying
#[utoipa::path(...)]attributes on route handlers - Add new schemas to the
ApiDocV1struct'scomponents(schemas(...))
- Create a handler function in a new module (or existing module)
- Add
#[utoipa::path(...)]decorator for OpenAPI documentation - Register the handler in route configuration (e.g., in a
config()function) - Mount the config in
main.rsunder the/apiscope
- Create a migration in
migration/src/with schema definition - Run
make migrateto apply the migration - Run
make entitiesto auto-generate Rust entity files - Import entities from
crate::entities::{prelude::*, *}and use them in handlers
- Database errors use
sea_orm::DbErr - HTTP responses use Actix Web's
HttpResponsebuilder - Logging uses the standard
logcrate (initialized viaenv_logger::init())
- Tests are co-located with source code using
#[cfg(test)]modules - Use
#[actix_web::test]attribute for async test functions - Import test utilities from
actix_web::test - Run specific tests:
cargo test --lib healthorcargo test test_name
Key Crates
actix-web(4.4.0) - Web frameworksea-orm(1.1.19) - ORM for database operationsutoipa(5.4.0) - OpenAPI/Swagger code generation with OpenAPI 3.1 supportutoipa-swagger-ui(9.0.2) - Swagger UI integrationserde(1.0.228) - Serialization/deserialization for JSONlog/env_logger(0.11.0) - Logging infrastructure
See Cargo.toml for complete dependency list and versions.
make migrate # First time setup
make dev # Runs server with DATABASE_URL=sqlite:./sqlite.dbSQLite file is stored at ./sqlite.db in the project root when using make dev.
RUST_LOG=debug cargo test test_name -- --nocapture --test-threads=1make cleandb
make migrate
make dev