
- Setup FastAPI project structure with main.py and requirements.txt - Implement SQLAlchemy ORM with SQLite database - Create Item model with CRUD operations - Implement health endpoint for monitoring - Setup Alembic for database migrations - Add comprehensive documentation in README.md - Configure Ruff for code linting
127 lines
3.7 KiB
Markdown
127 lines
3.7 KiB
Markdown
# REST API Service
|
|
|
|
A modern REST API service built with FastAPI and SQLite.
|
|
|
|
## Features
|
|
|
|
- **FastAPI Framework**: High-performance, easy to learn, fast to code, ready for production
|
|
- **SQLAlchemy ORM**: SQL toolkit and Object-Relational Mapping
|
|
- **SQLite Database**: Lightweight disk-based database
|
|
- **Alembic Migrations**: Database migration tool for SQLAlchemy
|
|
- **Pydantic Models**: Data validation and settings management
|
|
- **OpenAPI Documentation**: Interactive API documentation at /docs and /redoc
|
|
- **Health Check Endpoint**: Monitoring endpoint for service health
|
|
|
|
## Project Structure
|
|
|
|
```
|
|
.
|
|
├── alembic.ini # Alembic configuration
|
|
├── app # Application package
|
|
│ ├── api # API endpoints
|
|
│ │ ├── __init__.py # API router setup
|
|
│ │ ├── health.py # Health check endpoint
|
|
│ │ └── items.py # Items endpoints
|
|
│ ├── core # Core application configuration
|
|
│ │ └── config.py # Application settings
|
|
│ ├── crud # CRUD operations
|
|
│ │ ├── __init__.py # CRUD exports
|
|
│ │ ├── base.py # Base CRUD class
|
|
│ │ └── crud_item.py # Item CRUD operations
|
|
│ ├── db # Database setup
|
|
│ │ └── session.py # Database session management
|
|
│ ├── models # SQLAlchemy models
|
|
│ │ ├── __init__.py # Model exports
|
|
│ │ └── item.py # Item model
|
|
│ └── schemas # Pydantic schemas
|
|
│ ├── __init__.py # Schema exports
|
|
│ └── item.py # Item schemas
|
|
├── main.py # Application entry point
|
|
├── migrations # Database migrations
|
|
│ ├── env.py # Alembic environment configuration
|
|
│ ├── script.py.mako # Alembic template for migrations
|
|
│ └── versions # Migration scripts
|
|
│ └── initial_migration.py # Initial database schema
|
|
└── requirements.txt # Python dependencies
|
|
```
|
|
|
|
## Installation
|
|
|
|
1. Clone the repository:
|
|
```bash
|
|
git clone <repository-url>
|
|
cd <repository-name>
|
|
```
|
|
|
|
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
|
|
```
|
|
|
|
## Running the Application
|
|
|
|
Start the application with Uvicorn:
|
|
|
|
```bash
|
|
uvicorn main:app --reload
|
|
```
|
|
|
|
The API will be available at http://localhost:8000
|
|
|
|
## API Documentation
|
|
|
|
- **OpenAPI Swagger UI**: http://localhost:8000/docs
|
|
- **ReDoc**: http://localhost:8000/redoc
|
|
|
|
## API Endpoints
|
|
|
|
### Health Check
|
|
|
|
- `GET /api/v1/health`: Check the health of the API
|
|
|
|
### Items
|
|
|
|
- `GET /api/v1/items/`: Get all items
|
|
- `POST /api/v1/items/`: Create a new item
|
|
- `GET /api/v1/items/{item_id}`: Get an item by ID
|
|
- `PUT /api/v1/items/{item_id}`: Update an item
|
|
- `DELETE /api/v1/items/{item_id}`: Delete an item
|
|
|
|
## Database
|
|
|
|
The application uses SQLite as its database, stored at `/app/storage/db/db.sqlite`.
|
|
|
|
## Development
|
|
|
|
### Creating a new migration
|
|
|
|
After making changes to the SQLAlchemy models, create a new migration script:
|
|
|
|
```bash
|
|
alembic revision --autogenerate -m "Description of changes"
|
|
```
|
|
|
|
### Running migrations
|
|
|
|
Apply all pending migrations:
|
|
|
|
```bash
|
|
alembic upgrade head
|
|
```
|
|
|
|
Revert the last migration:
|
|
|
|
```bash
|
|
alembic downgrade -1
|
|
``` |