
- Set up FastAPI application with SQLite database - Implement User and Item models with relationships - Add CRUD operations for users and items - Configure Alembic for database migrations - Include API documentation at /docs and /redoc - Add health check endpoint at /health - Enable CORS for all origins - Structure code with proper separation of concerns
2.3 KiB
2.3 KiB
REST API Service
A comprehensive REST API built with FastAPI, SQLAlchemy, and SQLite.
Features
- User management (CRUD operations)
- Item management with user ownership
- SQLite database with Alembic migrations
- Automatic API documentation with FastAPI
- CORS enabled for all origins
- Health check endpoint
Project Structure
├── main.py # FastAPI application entry point
├── requirements.txt # Python dependencies
├── alembic.ini # Alembic configuration
├── alembic/ # Database migrations
├── app/
│ ├── api/ # API routes
│ ├── crud/ # Database operations
│ ├── db/ # Database configuration
│ ├── models/ # SQLAlchemy models
│ └── schemas/ # Pydantic schemas
└── storage/ # Application storage directory
Installation
- Install dependencies:
pip install -r requirements.txt
- Run database migrations:
alembic upgrade head
- Start the application:
uvicorn main:app --reload
API Endpoints
- GET /: Service information and links
- GET /health: Health check endpoint
- GET /docs: Interactive API documentation
- GET /redoc: Alternative API documentation
Users
- POST /api/v1/users/: Create a new user
- GET /api/v1/users/: List all users
- GET /api/v1/users/{user_id}: Get user by ID
- PUT /api/v1/users/{user_id}: Update user
- DELETE /api/v1/users/{user_id}: Delete user
Items
- POST /api/v1/items/: Create a new item
- GET /api/v1/items/: List all items
- GET /api/v1/items/{item_id}: Get item by ID
- PUT /api/v1/items/{item_id}: Update item
- DELETE /api/v1/items/{item_id}: Delete item
- GET /api/v1/items/users/{user_id}/items: Get items by user
Database
The application uses SQLite as the database, stored at /app/storage/db/db.sqlite
.
Environment Variables
No environment variables are currently required for basic operation.
Development
The application includes:
- Automatic code formatting with Ruff
- Database migrations with Alembic
- Comprehensive CRUD operations
- Input validation with Pydantic
- Automatic API documentation