
- Set up project structure with FastAPI - Configure SQLAlchemy with SQLite - Implement user and item models - Set up Alembic for database migrations - Create CRUD operations for models - Implement API endpoints for users and items - Add authentication functionality - Add health check endpoint - Configure Ruff for linting - Update README with comprehensive documentation
Generic REST API Service
A robust and scalable REST API service built with FastAPI and SQLite.
Features
- Modern FastAPI Framework: Fully typed and async-ready API framework
- SQLAlchemy ORM: Object-relational mapping for database interactions
- Alembic Migrations: Automated database schema migrations
- Pydantic Models: Request and response validation
- Dependency Injection: Clean, modular code structure
- OpenAPI Documentation: Auto-generated, interactive API docs
- Health Check Endpoint: API status monitoring
- User Authentication: Basic auth implementation
Project Structure
.
├── alembic.ini # Alembic configuration
├── app # Main application package
│ ├── api # API endpoints
│ │ ├── dependencies # FastAPI dependencies
│ │ └── v1 # API version 1
│ │ ├── api.py # API router
│ │ └── endpoints # API endpoint modules
│ ├── core # Core application code
│ │ ├── app.py # FastAPI application factory
│ │ ├── config.py # Application configuration
│ │ └── security.py # Security utilities
│ ├── crud # CRUD operations
│ ├── db # Database utilities
│ │ └── session.py # Database session management
│ ├── models # SQLAlchemy models
│ └── schemas # Pydantic schemas
├── main.py # Application entry point
├── migrations # Alembic migrations
│ ├── env.py # Alembic environment
│ ├── script.py.mako # Alembic script template
│ └── versions # Migration versions
└── requirements.txt # Project dependencies
Getting Started
Prerequisites
- Python 3.8 or higher
- pip (Python package manager)
Installation
-
Clone the repository:
git clone https://github.com/yourusername/genericrestapiservice.git cd genericrestapiservice
-
Create a virtual environment (optional but recommended):
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 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
- Alternative API documentation: http://localhost:8000/redoc
API Endpoints
Authentication
POST /api/v1/auth/login
- Get access tokenGET /api/v1/auth/me
- Get current user info
Users
GET /api/v1/users/
- List all users (admin only)POST /api/v1/users/
- Create a new userGET /api/v1/users/{user_id}
- Get user detailsPUT /api/v1/users/{user_id}
- Update user (admin only)DELETE /api/v1/users/{user_id}
- Delete user (admin only)
Items
GET /api/v1/items/
- List items (filtered by user)POST /api/v1/items/
- Create a new itemGET /api/v1/items/{id}
- Get item detailsPUT /api/v1/items/{id}
- Update itemDELETE /api/v1/items/{id}
- Delete item
Health Check
GET /health
- API health statusGET /api/v1/health
- API health status (versioned)
Development
Adding New Models
- Define SQLAlchemy model in
app/models/
- Define Pydantic schemas in
app/schemas/
- Create CRUD operations in
app/crud/
- Create a new migration with Alembic:
alembic revision --autogenerate -m "Add new model"
- Create API endpoints in
app/api/v1/endpoints/
- Register endpoints in
app/api/v1/api.py
Running Tests
pytest
License
This project is licensed under the MIT License - see the LICENSE file for details.
Description
Languages
Python
98.3%
Mako
1.7%