
- Set up project structure with FastAPI framework - Implement database models using SQLAlchemy - Create Alembic migrations for database schema - Build CRUD endpoints for Items resource - Add health check endpoint - Include API documentation with Swagger and ReDoc - Update README with project documentation
119 lines
3.3 KiB
Markdown
119 lines
3.3 KiB
Markdown
# 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
|
|
|
|
1. Clone the repository:
|
|
|
|
```bash
|
|
git clone <repository-url>
|
|
cd genericrestapiservice-e7xfpj
|
|
```
|
|
|
|
2. Create a virtual environment:
|
|
|
|
```bash
|
|
python -m venv venv
|
|
source venv/bin/activate # On Windows: venv\Scripts\activate
|
|
```
|
|
|
|
3. Install dependencies:
|
|
|
|
```bash
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
4. Run database migrations:
|
|
|
|
```bash
|
|
alembic upgrade head
|
|
```
|
|
|
|
5. Start the application:
|
|
|
|
```bash
|
|
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
|
|
|
|
1. Create a new module in `app/api/endpoints/`
|
|
2. Define your routes using FastAPI's router
|
|
3. Add your new router to `app/api/api.py`
|
|
|
|
### Adding a New Model
|
|
|
|
1. Define your SQLAlchemy model in `app/models/`
|
|
2. Define the Pydantic schemas in `app/schemas/`
|
|
3. Create CRUD utilities in `app/crud/`
|
|
4. Generate a migration: `alembic revision -m "Add new model"`
|
|
5. Edit the migration file in `migrations/versions/`
|
|
6. Apply the migration: `alembic upgrade head` |