
- Add pydantic-settings to requirements.txt - Add fallback import for BaseSettings from pydantic if pydantic_settings is not available - Fix ModuleNotFoundError during database migration
REST API Service
A modern REST API service built with FastAPI and SQLite.
Features
- FastAPI Framework: High-performance, easy to learn, fast to code, ready for production
- SQLAlchemy ORM: SQL toolkit and Object-Relational Mapping
- SQLite Database: Lightweight disk-based database
- Alembic Migrations: Database migration tool for SQLAlchemy
- Pydantic Models: Data validation and settings management
- OpenAPI Documentation: Interactive API documentation at /docs and /redoc
- Health Check Endpoint: Monitoring endpoint for service health
Project Structure
.
├── alembic.ini # Alembic configuration
├── app # Application package
│ ├── api # API endpoints
│ │ ├── __init__.py # API router setup
│ │ ├── health.py # Health check endpoint
│ │ └── items.py # Items endpoints
│ ├── core # Core application configuration
│ │ └── config.py # Application settings
│ ├── crud # CRUD operations
│ │ ├── __init__.py # CRUD exports
│ │ ├── base.py # Base CRUD class
│ │ └── crud_item.py # Item CRUD operations
│ ├── db # Database setup
│ │ └── session.py # Database session management
│ ├── models # SQLAlchemy models
│ │ ├── __init__.py # Model exports
│ │ └── item.py # Item model
│ └── schemas # Pydantic schemas
│ ├── __init__.py # Schema exports
│ └── item.py # Item schemas
├── main.py # Application entry point
├── migrations # Database migrations
│ ├── env.py # Alembic environment configuration
│ ├── script.py.mako # Alembic template for migrations
│ └── versions # Migration scripts
│ └── initial_migration.py # Initial database schema
└── requirements.txt # Python dependencies
Installation
-
Clone the repository:
git clone <repository-url> cd <repository-name>
-
Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Run database migrations:
alembic upgrade head
Running the Application
Start the application with Uvicorn:
uvicorn main:app --reload
The API will be available at http://localhost:8000
API Documentation
- OpenAPI Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
API Endpoints
Health Check
GET /api/v1/health
: Check the health of the API
Items
GET /api/v1/items/
: Get all itemsPOST /api/v1/items/
: Create a new itemGET /api/v1/items/{item_id}
: Get an item by IDPUT /api/v1/items/{item_id}
: Update an itemDELETE /api/v1/items/{item_id}
: Delete an item
Database
The application uses SQLite as its database, stored at /app/storage/db/db.sqlite
.
Development
Creating a new migration
After making changes to the SQLAlchemy models, create a new migration script:
alembic revision --autogenerate -m "Description of changes"
Running migrations
Apply all pending migrations:
alembic upgrade head
Revert the last migration:
alembic downgrade -1
Description
Languages
Python
96.5%
Mako
3.5%