
- Modified database connection to use project-relative paths - Updated Alembic configuration to dynamically use the SQLAlchemy URL - Added a root endpoint for easy testing - Fixed imports in migration environment
Generic REST API Service
A RESTful API built with FastAPI and SQLite.
Features
- FastAPI framework for high performance
- SQLAlchemy ORM with SQLite database
- Alembic for database migrations
- Pydantic for data validation
- CRUD operations for resources
- API documentation (Swagger UI and ReDoc)
- Health check endpoint
Project Structure
.
├── alembic.ini # Alembic configuration
├── app
│ ├── api # API endpoints
│ │ ├── api.py # Main API router
│ │ ├── endpoints # API endpoint modules
│ │ ├── health.py # Health check endpoint
│ │ └── items.py # Items CRUD endpoints
│ ├── core # Core application components
│ │ └── config.py # Application settings
│ ├── crud # CRUD operations
│ │ └── item.py # Item CRUD
│ ├── db # Database setup
│ │ └── database.py # DB connection, session
│ ├── models # SQLAlchemy models
│ │ └── item.py # Item model
│ ├── schemas # Pydantic schemas
│ │ └── item.py # Item schemas
│ └── storage # Storage for DB and files
│ └── db # Database storage
├── migrations # Alembic migrations
│ ├── env.py # Alembic environment
│ ├── script.py.mako # Migration script template
│ └── versions # Migration versions
│ └── 001_create_items_table.py # First migration
├── main.py # Application entry point
└── requirements.txt # Project dependencies
Installation
Prerequisites
- Python 3.8+
- pip
Setup
- Clone the repository:
git clone <repository-url>
cd genericrestapiservice-e7xfpj
- Create a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
- Run database migrations:
alembic upgrade head
- Start the application:
uvicorn main:app --reload
The API will be available at http://localhost:8000.
API Documentation
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
API Endpoints
- Health Check:
GET /api/v1/health
- List Items:
GET /api/v1/items
- Create Item:
POST /api/v1/items
- Get Item:
GET /api/v1/items/{item_id}
- Update Item:
PUT /api/v1/items/{item_id}
- Delete Item:
DELETE /api/v1/items/{item_id}
Development
Adding a New Endpoint
- Create a new module in
app/api/endpoints/
- Define your routes using FastAPI's router
- Add your new router to
app/api/api.py
Adding a New Model
- Define your SQLAlchemy model in
app/models/
- Define the Pydantic schemas in
app/schemas/
- Create CRUD utilities in
app/crud/
- Generate a migration:
alembic revision -m "Add new model"
- Edit the migration file in
migrations/versions/
- Apply the migration:
alembic upgrade head
Description
Languages
Python
96.5%
Mako
3.5%