
- Set up project structure with FastAPI, SQLAlchemy, and Alembic - Create database models for User and Item - Implement CRUD operations for all models - Create API endpoints with validation - Add health check endpoint - Configure CORS middleware - Set up database migrations - Add comprehensive documentation in README
Generic REST API Service
A RESTful API built with FastAPI and SQLite, providing endpoints for user and item management.
Features
- User management (create, read, update, delete)
- Item management (create, read, update, delete)
- SQLite database with SQLAlchemy ORM
- Alembic migrations
- OpenAPI documentation (Swagger UI)
- Password hashing with bcrypt
- Health check endpoint
Requirements
- Python 3.8+
- FastAPI
- SQLAlchemy
- Alembic
- Uvicorn
- Pydantic
- Other dependencies as listed in requirements.txt
Project Structure
.
├── app/
│ ├── api/
│ │ ├── endpoints/
│ │ │ ├── items.py
│ │ │ └── users.py
│ │ ├── api.py
│ │ └── deps.py
│ ├── core/
│ │ ├── config.py
│ │ └── security.py
│ ├── crud/
│ │ ├── base.py
│ │ ├── crud_item.py
│ │ └── crud_user.py
│ ├── database/
│ │ ├── base.py
│ │ ├── base_class.py
│ │ └── session.py
│ ├── models/
│ │ ├── item.py
│ │ └── user.py
│ └── schemas/
│ ├── item.py
│ └── user.py
├── migrations/
│ └── versions/
│ └── 20240520_initial.py
├── storage/
│ └── db/
│ └── db.sqlite
├── alembic.ini
├── main.py
└── requirements.txt
Installation
- Clone the repository:
git clone <repository-url>
cd genericrestapiservice
- Install dependencies:
pip install -r requirements.txt
- Run database migrations:
alembic upgrade head
Running the Application
Start the application using Uvicorn:
uvicorn main:app --host 0.0.0.0 --port 8000 --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 /health
- Check the API health status
Users
GET /api/v1/users/
- List all usersPOST /api/v1/users/
- Create a new userGET /api/v1/users/{user_id}
- Get user detailsPUT /api/v1/users/{user_id}
- Update user informationDELETE /api/v1/users/{user_id}
- Delete a user
Items
GET /api/v1/items/
- List all itemsPOST /api/v1/items/
- Create a new itemGET /api/v1/items/{id}
- Get item detailsPUT /api/v1/items/{id}
- Update item informationDELETE /api/v1/items/{id}
- Delete an itemGET /api/v1/items/user/{owner_id}
- List all items owned by a specific user
Development
Database Migrations
Generate a new migration:
alembic revision -m "description"
Apply migrations:
alembic upgrade head
Revert migrations:
alembic downgrade -1
Description
Languages
Python
97.6%
Mako
2.4%