Automated Action 87c716d3c6 Fix pydantic_settings import error
- Add pydantic-settings to requirements.txt
- Add fallback import for BaseSettings from pydantic if pydantic_settings is not available
- Fix ModuleNotFoundError during database migration
2025-05-23 10:41:26 +00:00
2025-05-23 10:41:26 +00:00
2025-05-23 10:04:35 +00:00

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

  1. Clone the repository:

    git clone <repository-url>
    cd <repository-name>
    
  2. Create a virtual environment:

    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
    
  3. Install dependencies:

    pip install -r requirements.txt
    
  4. 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

API Endpoints

Health Check

  • GET /api/v1/health: Check the health of the API

Items

  • GET /api/v1/items/: Get all items
  • POST /api/v1/items/: Create a new item
  • GET /api/v1/items/{item_id}: Get an item by ID
  • PUT /api/v1/items/{item_id}: Update an item
  • DELETE /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
Project: REST API Service
Readme 42 KiB
Languages
Python 96.5%
Mako 3.5%