A simple FastAPI server application with a 3-layer architecture and HTML templates.
The application follows a 3-layer architecture:
-
Model Layer (
src/model.py): Defines the domain model and data persistence logicItemclass: Manages the item data and provides methods for CRUD operations
-
View Layer (
src/view.py): Defines the data structures for requests and responses- Request models: Define the structure of incoming data
- Response models: Define the structure of outgoing data
-
Controller Layer (
src/controller.py): Handles HTTP requests and orchestrates the application flow- Routes HTTP requests to appropriate handlers
- Uses the model layer for data operations
- Uses the view layer for data transformation
-
Templates (
src/templates/): HTML templates using Jinja2base.html: Base template with common layout and stylingitem_list.html: Template for displaying the list of itemsitem_create.html: Template for the item creation form
-
Main Application (
src/main.py): Entry point that configures and starts the FastAPI application
- install
rye - Run
rye sync - Run
rye run uvicorn src.main:app --reload
rye run pytest-
POST /api/items/: Create a new item via API- Request body:
{"name": "item_name"} - Response:
{"message": "Item added", "item": "item_name"}
- Request body:
-
GET /api/items/: Get all items via API- Response:
{"items": ["item1", "item2", ...]}
- Response:
-
GET /: Redirects to the items list page -
GET /items: Display the list of all items- Renders the
item_list.htmltemplate
- Renders the
-
GET /items/create: Display the item creation form- Renders the
item_create.htmltemplate
- Renders the
-
POST /items/: Process the item creation form submission- Redirects to
/itemsafter successful creation
- Redirects to