2025-06-02 19:18:33 +00:00

3.0 KiB

REST API Service

A REST API service built with FastAPI and SQLite.

Features

  • FastAPI for high-performance API endpoints
  • SQLite database for simple data storage
  • Alembic for database migrations
  • Pydantic for data validation
  • CORS middleware configured
  • Health check endpoint
  • API documentation with OpenAPI

Project Structure

.
├── alembic.ini                   # Alembic configuration
├── app                           # Application code
│   ├── api                       # API endpoints
│   │   ├── v1                    # API version 1
│   │   │   ├── api.py            # API router
│   │   │   └── endpoints         # API endpoint modules
│   │   │       └── items.py      # Items endpoints
│   │   └── health.py             # Health check endpoint
│   ├── core                      # Core modules
│   │   └── config.py             # Application configuration
│   ├── db                        # Database code
│   │   └── base.py               # Database connection code
│   ├── models                    # SQLAlchemy models
│   │   └── item.py               # Item model
│   └── schemas                   # Pydantic schemas
│       └── item.py               # Item schemas
├── main.py                       # Application entry point
├── migrations                    # Alembic migrations
│   ├── env.py                    # Alembic environment
│   ├── script.py.mako            # Alembic script template
│   └── versions                  # Migration versions
│       └── 01_create_items_table.py # Initial migration
└── requirements.txt              # Python dependencies

Installation

  1. Clone the repository:
git clone <repository-url>
cd <repository-directory>
  1. Create 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. Configure environment variables:

Create a .env file based on .env.example.

Database Migrations

Initialize the database:

alembic upgrade head

Running the API

Start the API server:

uvicorn main:app --reload

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

API Endpoints

Items

  • 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

Environment Variables

  • APP_NAME: Application name
  • API_V1_STR: API v1 path prefix
  • CORS_ORIGINS: CORS allowed origins