2025-05-16 14:34:27 +00:00

2.4 KiB

Generic REST API Service

A modern REST API service built with FastAPI and SQLite for handling item resources.

Features

  • FastAPI-based REST API with automatic OpenAPI documentation
  • SQLAlchemy ORM with SQLite database
  • Alembic database migrations
  • CRUD operations for item resources
  • Health check endpoint
  • Async-ready with uvicorn

Project Structure

.
├── alembic.ini
├── app/
│   ├── api/
│   │   ├── api.py
│   │   └── endpoints/
│   │       └── items.py
│   ├── core/
│   │   ├── config.py
│   │   └── database.py
│   ├── models/
│   │   ├── base.py
│   │   └── item.py
│   └── schemas/
│       └── item.py
├── main.py
├── migrations/
│   ├── env.py
│   ├── README
│   ├── script.py.mako
│   └── versions/
│       └── 001_initial_migration.py
└── requirements.txt

Prerequisites

  • Python 3.8+
  • virtualenv (recommended)

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd <repository-directory>
    
  2. Create and activate a virtual environment (optional but recommended):

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

    pip install -r requirements.txt
    
  4. Initialize the database:

    alembic upgrade head
    

Running the API

Start the API server with:

uvicorn main:app --reload

The API will be available at http://localhost:8000

API Documentation

FastAPI automatically generates interactive API documentation:

API Endpoints

  • GET /health - Health check endpoint
  • GET /api/v1/items/ - List all items
  • POST /api/v1/items/ - Create a new item
  • GET /api/v1/items/{item_id} - Get a specific item
  • PUT /api/v1/items/{item_id} - Update a specific item
  • DELETE /api/v1/items/{item_id} - Delete a specific item

Development

Database Migrations

After modifying models, create a new migration:

alembic revision -m "description of changes"

Apply migrations:

alembic upgrade head

Revert migrations:

alembic downgrade -1  # Go back one migration

Code Quality

Run linting with Ruff:

ruff check .
ruff check --fix .  # Auto-fix issues