🏗️ FastASGI Architecture Overview

📖 In this section: Understanding FastASGI's architecture is essential for framework contributors. This guide breaks down the core classes and their responsibilities, giving you a clear map of how the framework operates internally.

Core Classes Overview

FastASGI is built around a small set of core classes, each with specific responsibilities. Here's what each class does and how they work together:

🚀 FastASGI

Location: fastasgi/fastasgi.py
Purpose: The main application class that implements the ASGI protocol and coordinates all other components. It handles ASGI message routing (HTTP, Lifespan), manages the middleware chain, and provides the decorator interface for route registration.

🔄 APIRouter

Location: fastasgi/routing/apirouter.py
Purpose: Manages collections of routes and handles route matching logic. It supports nested routers with prefixes, provides decorators for route registration, and implements the core route dispatch mechanism that converts URL paths to handler functions.

🛤️ Route

Location: fastasgi/routing/route.py
Purpose: Represents individual route definitions with path pattern matching and parameter extraction. It compiles path patterns into regex, handles type conversion for path parameters, and implements priority-based route ordering for efficient matching.

📥 Request

Location: fastasgi/request/request.py
Purpose: Encapsulates HTTP request data and provides convenient access methods. It handles ASGI scope conversion, manages request body parsing (including multipart/form-data), and provides access to headers, cookies, and query parameters.

📤 Response

Location: fastasgi/response.py
Purpose: Represents HTTP responses with automatic content type detection and ASGI message generation. It handles various content types (text, JSON, HTML), manages response headers and cookies, and converts response data into ASGI message format.

🔗 MiddlewareChain

Location: fastasgi/middleware/middlewarechain.py
Purpose: Builds and manages the middleware execution pipeline using function composition. It creates a single callable from multiple middleware functions, handles middleware ordering, and ensures proper request/response flow through the chain.

📁 UploadFile

Location: fastasgi/request/upload_file.py
Purpose: Manages file uploads with memory and disk storage optimization. It provides a file-like interface for uploaded files, handles automatic cleanup of temporary files, and supports both in-memory and disk-based storage depending on file size.

🔢 HTTPStatus

Location: fastasgi/status.py
Purpose: Provides standardized HTTP status codes as an enumeration. It ensures consistent status code usage across the framework and includes both numeric codes and descriptive names for common HTTP responses.

💡 Next Steps: Now that you understand the core classes, the next sections will dive deeper into how these components interact, their internal implementation details, and how to extend them for your framework needs.