Skip to Content
API Service (Rust)Overview & Module Map

API Service — Overview & Module Map

Responsibilities

The Rust API service is the sole public-facing entry point. It handles:

  • HTTP request routing and middleware (Axum + Tower)
  • Authentication and session validation
  • Authorization (role enforcement)
  • Contributor submission queue orchestration (/resources/submissions*)
  • Request proxying to the Intelligence service via gRPC
  • All PostgreSQL writes for identity, sessions, and conversations
  • Reading chat_messages written by the Intelligence service
  • Static file serving via tower-http::services::ServeDir
  • Rate limiting on auth-sensitive endpoints
  • Observability via tracing + TraceLayer

AppState

pub struct AppState { pub db: PgPool, // SQLx PostgreSQL connection pool pub config: Config, // Loaded from environment pub intelligence_client: IntelligenceClient, // tonic gRPC client wrapper pub start_time: std::time::Instant, // Used for uptime calculation }

PgPool implements FromRef<AppState>, allowing extractors to access it directly from state without destructuring.

Module Structure

Cargo Dependencies

CategoryCrateVersionNotes
Web Frameworkaxum0.8.8Tower-compatible, type-safe extractors
Async Runtimetokio (features: full)1.49.0Multi-threaded work-stealing scheduler
gRPC clienttonic0.14.2HTTP/2, mTLS capable
gRPC codegenprost + tonic-prost0.14.xProtobuf v3 codegen
Serializationserde + serde_json1.0.xDerive macros
Databasesqlx (postgres, uuid, chrono, macros)0.8.3Compile-time checked queries
IDsuuid (v4, serde)1.19.0UUIDv4 generation
Timechrono (serde)0.4Timestamps, duration
Password hashingbcrypt0.15Argon2-equivalent, blocking
OAuthoauth24.4Google + GitHub flows
HTTP clientreqwest (json)0.12OAuth token exchange
Emaillettre (tokio1-rustls-tls)0.11Async SMTP
Rate limitinggovernor + tower_governor0.10.4 / 0.8.0Token bucket, per-IP
Middlewaretower + tower-http0.5.2 / 0.6CORS, TraceLayer, ServeDir
Observabilitytracing + tracing-subscriber0.1.41Structured logging
Streamingfutures + async-stream0.3SSE bridge macros
Validationregex + once_cell1.10.4Email/password validation
Cryptosha20.10Token hashing
IP networksipnetwork0.21.1Client IP storage
Envdotenvy0.15.7.env loading
Buildtonic-prost-build0.14.2build.rs proto compilation

Environment Configuration

VariableDefaultPurpose
DATABASE_URLPostgreSQL connection string
SERVER_HOST0.0.0.0Bind address
SERVER_PORT8080HTTP port
INTELLIGENCE_SERVICE_URLhttp://[::1]:50051gRPC endpoint
GOOGLE_CLIENT_ID / _SECRET / _REDIRECT_URLGoogle OAuth
GITHUB_CLIENT_ID / _SECRET / _REDIRECT_URLGitHub OAuth
SMTP_HOST / _PORT / _USERNAME / _PASSWORDEmail delivery
FROM_EMAILSender address
CONTACT_EMAIL(falls back to FROM_EMAIL)Recipient for contact form submissions
FRONTEND_URLUsed in email links
CORS_ALLOWED_ORIGINS*Comma-separated or *
RUST_LOGapi=debugtracing log filter
Last updated on