Automated Action e1b1b89511 Create FastAPI REST API with SQLite
Features:
- Project structure with FastAPI framework
- SQLAlchemy models with SQLite database
- Alembic migrations system
- CRUD operations for items
- API routers with endpoints for items
- Health endpoint for monitoring
- Error handling and validation
- Comprehensive documentation
2025-05-18 05:45:33 +00:00
2025-05-18 05:45:33 +00:00
2025-05-18 05:39:44 +00:00
2025-05-18 05:45:33 +00:00
2025-05-18 05:45:33 +00:00

Generic REST API Service

A generic REST API service built with FastAPI and SQLite.

Features

  • FastAPI framework for high performance
  • SQLite database with SQLAlchemy ORM
  • Alembic for database migrations
  • CRUD operations for resource management
  • Pydantic models for request/response validation
  • Error handling with custom exceptions
  • API documentation (Swagger UI and ReDoc)
  • Health check endpoint

Project Structure

.
├── app                     # Application package
│   ├── api                 # API endpoints
│   │   ├── v1              # API version 1
│   │   │   ├── health.py   # Health check endpoint
│   │   │   └── items.py    # Items endpoints
│   │   ├── deps.py         # Dependencies
│   │   ├── errors.py       # Error handlers and custom exceptions
│   │   └── router.py       # Main router
│   ├── core                # Core modules
│   │   └── config.py       # Configuration settings
│   ├── crud                # CRUD operations
│   │   ├── base.py         # Base CRUD class
│   │   └── crud_item.py    # Item CRUD operations
│   ├── db                  # Database
│   │   ├── base.py         # Base imports for Alembic
│   │   ├── base_class.py   # Base class for models
│   │   └── session.py      # Database session
│   ├── models              # SQLAlchemy models
│   │   └── item.py         # Item model
│   └── schemas             # Pydantic schemas
│       └── item.py         # Item schemas
├── migrations              # Alembic migrations
│   ├── versions            # Migration versions
│   │   └── 001_initial_migration.py  # Initial migration
│   ├── env.py              # Alembic environment
│   └── script.py.mako      # Migration script template
├── alembic.ini             # Alembic configuration
├── main.py                 # Application entry point
└── requirements.txt        # Dependencies

Setup and Installation

  1. Clone the repository:
git clone https://github.com/your-username/genericrestapiservice.git
cd genericrestapiservice
  1. Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt
  1. Run database migrations:
alembic upgrade head
  1. Start the application:
uvicorn main:app --reload

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

API Documentation

API Endpoints

Health Check

  • GET /api/v1/health - Check API and database health

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

Example Usage

Create an item

curl -X 'POST' \
  'http://localhost:8000/api/v1/items' \
  -H 'Content-Type: application/json' \
  -d '{
  "title": "My First Item",
  "description": "This is an example item",
  "is_active": true
}'

Get all items

curl -X 'GET' 'http://localhost:8000/api/v1/items'

Get an item by ID

curl -X 'GET' 'http://localhost:8000/api/v1/items/1'

Update an item

curl -X 'PUT' \
  'http://localhost:8000/api/v1/items/1' \
  -H 'Content-Type: application/json' \
  -d '{
  "title": "Updated Item",
  "description": "This item has been updated"
}'

Delete an item

curl -X 'DELETE' 'http://localhost:8000/api/v1/items/1'

Database Configuration

The API uses SQLite as the database engine. The database file is located at /app/storage/db/db.sqlite. This path is configured in app/core/config.py. You can change the database configuration by modifying this file.

Running with Docker

You can use Docker to run the API. Here's an example Dockerfile:

FROM python:3.11-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

# Create storage directory
RUN mkdir -p /app/storage/db

# Run migrations
RUN alembic upgrade head

# Expose the port
EXPOSE 8000

# Run the application
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

To build and run the Docker container:

docker build -t genericrestapiservice .
docker run -p 8000:8000 genericrestapiservice

Development

Code Style

This project uses Ruff for linting and formatting. To lint the code:

ruff check .

To format the code:

ruff format .
Description
Project: Generic REST API Service
Readme 42 KiB
Languages
Python 96.8%
Mako 3.2%