126 lines
2.4 KiB
Markdown
126 lines
2.4 KiB
Markdown
# Generic REST API Service
|
|
|
|
A modern REST API service built with FastAPI and SQLite for handling item resources.
|
|
|
|
## Features
|
|
|
|
- FastAPI-based REST API with automatic OpenAPI documentation
|
|
- SQLAlchemy ORM with SQLite database
|
|
- Alembic database migrations
|
|
- CRUD operations for item resources
|
|
- Health check endpoint
|
|
- Async-ready with uvicorn
|
|
|
|
## Project Structure
|
|
|
|
```
|
|
.
|
|
├── alembic.ini
|
|
├── app/
|
|
│ ├── api/
|
|
│ │ ├── api.py
|
|
│ │ └── endpoints/
|
|
│ │ └── items.py
|
|
│ ├── core/
|
|
│ │ ├── config.py
|
|
│ │ └── database.py
|
|
│ ├── models/
|
|
│ │ ├── base.py
|
|
│ │ └── item.py
|
|
│ └── schemas/
|
|
│ └── item.py
|
|
├── main.py
|
|
├── migrations/
|
|
│ ├── env.py
|
|
│ ├── README
|
|
│ ├── script.py.mako
|
|
│ └── versions/
|
|
│ └── 001_initial_migration.py
|
|
└── requirements.txt
|
|
```
|
|
|
|
## Prerequisites
|
|
|
|
- Python 3.8+
|
|
- virtualenv (recommended)
|
|
|
|
## Installation
|
|
|
|
1. Clone the repository:
|
|
```bash
|
|
git clone <repository-url>
|
|
cd <repository-directory>
|
|
```
|
|
|
|
2. Create and activate a virtual environment (optional but recommended):
|
|
```bash
|
|
python -m venv venv
|
|
source venv/bin/activate # On Windows: venv\Scripts\activate
|
|
```
|
|
|
|
3. Install dependencies:
|
|
```bash
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
4. Initialize the database:
|
|
```bash
|
|
alembic upgrade head
|
|
```
|
|
|
|
## Running the API
|
|
|
|
Start the API server with:
|
|
|
|
```bash
|
|
uvicorn main:app --reload
|
|
```
|
|
|
|
The API will be available at http://localhost:8000
|
|
|
|
## API Documentation
|
|
|
|
FastAPI automatically generates interactive API documentation:
|
|
|
|
- Swagger UI: http://localhost:8000/docs
|
|
- ReDoc: http://localhost:8000/redoc
|
|
|
|
## API Endpoints
|
|
|
|
- **GET /health** - Health check endpoint
|
|
- **GET /api/v1/items/** - List all items
|
|
- **POST /api/v1/items/** - Create a new item
|
|
- **GET /api/v1/items/{item_id}** - Get a specific item
|
|
- **PUT /api/v1/items/{item_id}** - Update a specific item
|
|
- **DELETE /api/v1/items/{item_id}** - Delete a specific item
|
|
|
|
## Development
|
|
|
|
### Database Migrations
|
|
|
|
After modifying models, create a new migration:
|
|
|
|
```bash
|
|
alembic revision -m "description of changes"
|
|
```
|
|
|
|
Apply migrations:
|
|
|
|
```bash
|
|
alembic upgrade head
|
|
```
|
|
|
|
Revert migrations:
|
|
|
|
```bash
|
|
alembic downgrade -1 # Go back one migration
|
|
```
|
|
|
|
### Code Quality
|
|
|
|
Run linting with Ruff:
|
|
|
|
```bash
|
|
ruff check .
|
|
ruff check --fix . # Auto-fix issues
|
|
``` |