3.6 KiB
3.6 KiB
Generic REST API with FastAPI and SQLite
This is a RESTful API built with FastAPI and SQLite, providing a foundation for building robust web applications with Python.
Features
- FastAPI framework with automatic API documentation
- SQLAlchemy ORM with SQLite database
- Alembic database migrations
- Repository pattern for database operations
- Pydantic models for data validation
- CORS middleware enabled
- Health check endpoint
- RESTful CRUD operations
Requirements
- Python 3.8+
Installation
- Clone the repository:
git clone https://github.com/yourusername/genericrestapi.git
cd genericrestapi
- Install dependencies:
pip install -r requirements.txt
- Run database migrations:
alembic upgrade head
Environment Variables
This application uses the following environment variables:
- None required for basic operation as the app uses SQLite with a fixed path
Running the Application
Start the development server:
uvicorn main:app --reload
The API will be available at http://localhost:8000.
API Documentation
Once the application is running, you can access:
- Interactive API documentation: http://localhost:8000/docs
- ReDoc API documentation: http://localhost:8000/redoc
- OpenAPI specification: http://localhost:8000/openapi.json
API Endpoints
Health Check
GET /health
- Check API health status
Items API
GET /api/v1/items/
- List all items (with pagination)POST /api/v1/items/
- Create a new itemGET /api/v1/items/{item_id}
- Get a specific item by IDPUT /api/v1/items/{item_id}
- Update a specific itemDELETE /api/v1/items/{item_id}
- Delete a specific item
Project Structure
.
├── alembic.ini # Alembic configuration
├── app/ # Application package
│ ├── api/ # API package
│ │ ├── app.py # FastAPI application instance
│ │ ├── health.py # Health check endpoint
│ │ └── v1/ # API v1 endpoints
│ │ └── routes.py # API route definitions
│ ├── core/ # Core functionality
│ │ ├── config.py # Application configuration
│ │ └── exceptions.py # Custom exception classes
│ └── db/ # Database package
│ ├── models/ # SQLAlchemy models
│ │ └── item.py # Item model definition
│ ├── repositories/ # Repository pattern implementations
│ │ ├── base.py # Base repository class
│ │ └── item.py # Item repository
│ ├── schemas/ # Pydantic schemas
│ │ └── item.py # Item schemas
│ └── session.py # Database session configuration
├── main.py # Application entry point
├── migrations/ # Alembic migrations
│ ├── env.py # Alembic environment
│ ├── README # Migrations README
│ ├── script.py.mako # Migration script template
│ └── versions/ # Migration versions
│ └── 0001_initial_migration.py # Initial migration
└── requirements.txt # Python dependencies
Development
Running Tests
Tests can be run using pytest:
pytest
Code Formatting and Linting
This project uses Ruff for linting and code formatting:
ruff check .
ruff format .