The Definitive Guide to Convention-Driven, AI-Era Web Applications in Rust
Introduction: A Framework Made of Decisions
- The Paralysis That Rails Solved
- When the Coder Is an Agent, Not a Person
- Rust off Rails in One Paragraph
- What This Book Will Teach You
- What This Book Will Not Do
- A Note on Versions and Accuracy
- How to Read This Book
Part I: Foundations
Chapter 1: A Framework Made of Decisions
- What Rust off Rails Actually Is
- The Problem with Endless Deliberation
- Standing on Giants: Rails, Phoenix and Rust
- Convention Over Deliberation, Not Just Configuration
- The Prime Directives: Rules That Shape Every App
- Why This Paradigm Is New - and Why It Matters
Chapter 2: The Rust Web Landscape
- Why Rust for Web Applications
- The Ecosystem: Axum, Actix, Rocket and Warp
- What “Type Safety” Means for Web Code
- The Cost of Compilation and How We Manage It
- When Rust Makes Sense and When It Does Not
Chapter 3: Environment and Toolchain Setup
- Installing Rust and Essential Toolchain Components
- The One-Line Installer: What It Actually Does
- Manual Setup for Those Who Prefer Control
- Verifying Your Installation
- Platform Notes: Linux, macOS and Windows Considerations
- Starting Your First Project
Part II: The Stack
Chapter 4: The Approved Stack - One Crate per Job
- Selection Criteria: Training Data, Compile-Time Safety and Stability
- The Core Stack: Tokio, Axum, SeaORM, Askama
- Supporting Crates: Serialization, Time, Cryptography and CLI
- The Frontend Stack: Tailwind, HTMX and the Alternatives
- Forbidden Crates and Why They’re Banned
- The Complete Cargo.toml
Chapter 5: Project Scaffolding and Directory Structure
- Root Files: Cargo.toml, Dockerfile and Configuration
- The src Tree: lib, main and Module Boundaries
- Controllers, Models, Views and Their One-Way Dependencies
- Templates, Assets and Static Files
- Tests, Migrations and the Product Directory
- How It All Fits Together
Chapter 6: Configuration, State and Environment
- The Config Struct: Parsing Environment Exactly Once
- Environment Modes: Development, Test, Production
- Building AppState: What Middleware and Handlers Share
- Secrets Hygiene: Where Secrets Live and How They Die
- Fail Fast: Boot Validation and Common Pitfalls
Chapter 7: Routing: Mapping URLs to Actions
- The routes.rs Architecture and Feature-Specific Routers
- Resourceful Routes: RESTful Patterns and the Standard Actions
- Path Parameters, Query Strings and Body Extractors
- Nested Routes and Shallow Nesting
- Frontend Mode Differences: HTMX, Inertia, SPA, API
- Route Security and the Route Table
Chapter 8: Controllers: Thin Handlers, Clean Flow
- Handler Signatures and the Argument Order Rule
- State, Context and Authentication Extractors
- Read Actions: Index, Show and Query Delegation
- Write Actions: Create, Update, Destroy and Redirect Rules
- Authorization and Security in Handlers
- Common Pitfalls and How to Avoid Them
Chapter 9: Views and Templates with Askama
- Page Structs and the Rendering Contract
- Layouts: The Global Shell and Block System
- Partials and Fragments: Reusable HTML with Type Safety
- The Component Kit: Macros for Accessible UI
- HTMX Integration: Boosted Navigation and Event Delegation
- Forms: Reusable Components with Validation
- Flash and Alerts: One-Time Messages
- Dark Mode and Theming
Part III: Building Applications
Chapter 10: Models and the Data Layer
- Entity Definitions: Mirroring the Migration Exactly
- Parameters and Validation: Input Structs That Fail Loudly
- CRUD Functions: create, update, destroy and Error Handling
- Associations: belongs_to, has_many and Eager Loading
- What Models Are Not Allowed to Do
Chapter 11: Migrations: Schema as Immutable History
- Creating and Registering Migrations
- The Migration Trait and Schema DSL
- Zero-Downtime Migrations: The Expand-Contract Pattern
- SQLite Limitations and Table Rebuilds
- Testing Rollbacks and the Immutable History Rule
Chapter 12: Database Portability: SQLite and Postgres
- Runtime Backend Selection via DATABASE_URL
- Connection Pooling: Different Defaults for Different Engines
- Transactions: BEGIN IMMEDIATE and the kernel::db::begin Rule
- Raw SQL Escape Hatches and Backend-Specific Logic
- Data Migration Between Backends
Part V: User Experience and Frontend
Chapter 13: Authentication and Session Management
- The ACCOUNTS Setting: Composing Authentication Flows
- Sessions: Tokens, Digests and Cookie Configuration
- Password Reset: Rate Limits, Tokens and Session Revocation
- Email Verification and the Anti-Hijack Rule
- Invite-Only Mode and Signup Control
- Rate Limiting and Login Protection
Chapter 14: HTMX-Driven Interfaces
- Setting Up HTMX and Tailwind Without Node
- Boosted Navigation: Links That Don’t Reload the Page
- Handling Validation Errors and Redirect Rules
- Partial Updates and Out-of-Band Swaps
- JavaScript That Survives Swaps: Event Delegation Patterns
Chapter 15: Inertia Mode: React or Svelte with Rust Routing
- The Inertia Protocol: JSON for Visits, HTML for Initial Load
- Page Props, ts-rs Type Generation and the Large Int Rule
- Frontend Setup: Vite, Pages and Routing
- Forms with useForm and Server-Side Validation Errors
- Asset Versioning and the 409 Reload
Chapter 16: SPA Mode: A React Frontend on a Rust API
- Why SPA Mode: When You Actually Need a Public API
- The API Boundary: /api/v1 and the No-Leakage Rule
- Frontend Architecture: React Router 8 and Data Loading
- State Management: TanStack Query and Cache Invalidation
- Authentication Without JWT: Cookie Sessions in a SPA
Part VI: Production Readiness
Chapter 17: Background Jobs and the Persistent Queue
- The Philosophy: The Database Is the Only Infrastructure
- Defining Workers: Traits, Constants and the perform Method
- Enqueueing: Jobs Inside Transactions and Lightweight Args
- Retries, Backoff, Snoozing and Dead Letter Queues
- Scheduling: Delayed Execution and Cron-Based Recurring Jobs
- The Jobs Dashboard and CLI: Monitoring from Browser or Terminal
Chapter 18: Mailers and Email Delivery
- The Email Struct: To, Subject, Parts and Attachments
- Delivery Modes: SMTP, Local, Log and Memory
- Templates: HTML and Text with Absolute URLs Only
- Deliver Later: Asynchronous Sending and Token Minting at Send Time
- The Local Mailbox: Rendering, Searching and Agent Verification
Chapter 19: Real-Time Updates and WebSockets
- The Signal Model: Broadcast Signals, Not HTML
- SSE Streams: Connections, Heartbeats and Resume Cursors
- Pub/Sub: In-Process Notification and Postgres NOTIFY
- Presence Tracking: People, Not Tabs
- WebSocket Channels: Rooms, Protocols and Slow Consumer Handling
Chapter 20: Security: Defenses Against Common Attacks
- CSRF: Fetch Metadata and Origin Validation
- XSS: Auto-Escaping, Ammonia and Strict CSP
- Injection: SeaORM Builders and No String Formatting for SQL
- Authentication Security: Rate Limits, Session Revocation, Secrets Hygiene
- Security Headers: CSP, HSTS and Cache-Control
- Rate Limiting: Fixed Windows and Lockout Prevention
- Development Security: localhost Binding and Host Header Checks
Chapter 21: Testing Strategies and the Definition of Done
- Integration Tests: The TestApp and Simulating Browser Behavior
- Model and Policy Tests: Truth Tables and Boundary Conditions
- Factories and Fixtures: Reusable Test Data Without Mocking
- Database Handling: In-Memory SQLite and Postgres Slot-Based Testing
- Frontend Testing: Vitest, Playwright and Never Mocking Fetch
- The Definition of Done: cargo offrails verify and What It Checks
Chapter 22: Error Handling, Logging and Observability
- AppError: Variants, HTTP Mapping and the IntoResponse Implementation
- Error Shaping: Different Responses for HTML, HTMX and JSON
- Panic Handling: The Catch Layer and Generic 500 Pages
- Logging with tracing-subscriber: Structured Events and Levels
- Spans, Instrumentation and What to Skip
- Health Checks: The /up Endpoint and Database Verification
Chapter 23: Caching: When, Where and How
- The Philosophy: Do Not Cache Until Measured
- Architecture: Moka in Memory, cache_entries in the Database
- Key Design: Versioning with updated_at and Prefix Deletion
- Invalidation: What You Can and Cannot Do in Transactions
- What Never to Cache: HTML, Flash, Forms, User-Specific Data
Chapter 24: File Storage and Uploads
- Architecture: object_store and the blobs Metadata Table
- Backends: Local Disk, Memory and S3-Compatible Services
- Upload Handling: Multipart Extractors and Body Size Limits
- Serving and Security: Authorization Through the App, Not Direct URLs
- Async Deletion: Jobs That Clean Up After Transactions
Chapter 25: API Documentation and GraphQL
- OpenAPI with utoipa: Annotations, Schemas and Security Schemes
- API Versioning: /api/v1, /api/v2 and the No-Breaking-Changes Rule
- Schema Management: Committing the Document and Drift Tests
- GraphQL: Schema, Resolvers and DataLoaders
- Complexity Limits, Depth Guards and Security
Part VII: Operations and Scaling
Chapter 26: Deployment: Docker, Platforms and Zero-Downtime Releases
- The Docker Image: One Container, Web and Worker
- Multi-Stage Builds: Rust Toolchain, Node and Runtime
- Platform Configurations: Fly.io, Kamal, Render, Railway
- Database Migrations in Production: Release Commands and Advisory Locks
- Health Checks, Rolling Deploys and Rollback Strategies
- Open-Source and Secret Management
Chapter 27: CI/CD, Code Quality and Long-Term Maintenance
- The CI Pipeline: Parallel Verification and Database Testing
- Code Quality: fmt, clippy, cargo-deny and gitleaks
- Coverage and Mutation Testing: Thresholds and Enforced Gates
- Browser and Accessibility Checks in CI
- Upgrading: Framework Releases and the CHANGELOG as Guide
Chapter 28: Conclusion - The Shape of Things to Come
- What We Have Built: The Complete Railboard Application
- The Trade-Offs: Opinionation as Feature and as Constraint
- When to Use Rust off Rails and When Not To
- The Future: Where This Paradigm Is Heading
- Go Build Something Real