Automated Action 55fa6c2bbd Create comprehensive FastAPI REST API service
- Set up FastAPI application with CORS support
- Implement SQLAlchemy models and database session management
- Create CRUD API endpoints for items management
- Configure Alembic for database migrations
- Add health check and service info endpoints
- Include comprehensive documentation and project structure
- Integrate Ruff for code quality and formatting
2025-06-19 04:39:38 +00:00

2.8 KiB

REST API Service

A comprehensive REST API built with FastAPI, SQLAlchemy, and SQLite.

Features

  • FastAPI: Modern, fast web framework for building APIs
  • SQLAlchemy: SQL toolkit and Object-Relational Mapping (ORM) library
  • SQLite: Lightweight database for data persistence
  • Alembic: Database migration tool
  • CORS: Cross-Origin Resource Sharing enabled for all origins
  • Health Check: Built-in health monitoring endpoint
  • API Documentation: Automatic interactive API docs via OpenAPI/Swagger

Project Structure

.
├── main.py                 # Application entry point
├── requirements.txt        # Python dependencies
├── alembic.ini            # Alembic configuration
├── alembic/               # Database migrations
│   ├── env.py
│   ├── script.py.mako
│   └── versions/
└── app/
    ├── api/               # API routes
    │   ├── items.py       # Items CRUD endpoints
    │   └── routes.py      # Main router
    ├── db/                # Database configuration
    │   ├── base.py        # SQLAlchemy Base
    │   └── session.py     # Database session
    ├── models/            # SQLAlchemy models
    │   └── item.py        # Item model
    └── schemas/           # Pydantic schemas
        └── item.py        # Item schemas

Installation

  1. Install dependencies:
pip install -r requirements.txt
  1. Run database migrations:
alembic upgrade head

Running the Application

Start the server with uvicorn:

uvicorn main:app --host 0.0.0.0 --port 8000 --reload

The API will be available at:

API Endpoints

Core Endpoints

  • GET / - Service information
  • GET /health - Health check

Items API (/api/v1/items)

  • POST / - Create a new item
  • GET / - List all items (with pagination)
  • GET /{item_id} - Get item by ID
  • PUT /{item_id} - Update item by ID
  • DELETE /{item_id} - Delete item by ID

Database

The application uses SQLite with the database file stored at /app/storage/db/db.sqlite.

Running Migrations

Create a new migration:

alembic revision --autogenerate -m "Description of changes"

Apply migrations:

alembic upgrade head

Development

Code Quality

The project uses Ruff for code formatting and linting:

ruff check .
ruff format .

Environment Variables

No environment variables are required for basic operation. The application uses SQLite with a default file path.

License

This project is open source and available under the MIT License.