Automated Action e1b1b89511 Create FastAPI REST API with SQLite
Features:
- Project structure with FastAPI framework
- SQLAlchemy models with SQLite database
- Alembic migrations system
- CRUD operations for items
- API routers with endpoints for items
- Health endpoint for monitoring
- Error handling and validation
- Comprehensive documentation
2025-05-18 05:45:33 +00:00

198 lines
4.7 KiB
Markdown

# Generic REST API Service
A generic REST API service built with FastAPI and SQLite.
## Features
- FastAPI framework for high performance
- SQLite database with SQLAlchemy ORM
- Alembic for database migrations
- CRUD operations for resource management
- Pydantic models for request/response validation
- Error handling with custom exceptions
- API documentation (Swagger UI and ReDoc)
- Health check endpoint
## Project Structure
```
.
├── app # Application package
│ ├── api # API endpoints
│ │ ├── v1 # API version 1
│ │ │ ├── health.py # Health check endpoint
│ │ │ └── items.py # Items endpoints
│ │ ├── deps.py # Dependencies
│ │ ├── errors.py # Error handlers and custom exceptions
│ │ └── router.py # Main router
│ ├── core # Core modules
│ │ └── config.py # Configuration settings
│ ├── crud # CRUD operations
│ │ ├── base.py # Base CRUD class
│ │ └── crud_item.py # Item CRUD operations
│ ├── db # Database
│ │ ├── base.py # Base imports for Alembic
│ │ ├── base_class.py # Base class for models
│ │ └── session.py # Database session
│ ├── models # SQLAlchemy models
│ │ └── item.py # Item model
│ └── schemas # Pydantic schemas
│ └── item.py # Item schemas
├── migrations # Alembic migrations
│ ├── versions # Migration versions
│ │ └── 001_initial_migration.py # Initial migration
│ ├── env.py # Alembic environment
│ └── script.py.mako # Migration script template
├── alembic.ini # Alembic configuration
├── main.py # Application entry point
└── requirements.txt # Dependencies
```
## Setup and Installation
1. Clone the repository:
```bash
git clone https://github.com/your-username/genericrestapiservice.git
cd genericrestapiservice
```
2. Create and activate 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` - Check API and database health
### 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
## Example Usage
### Create an item
```bash
curl -X 'POST' \
'http://localhost:8000/api/v1/items' \
-H 'Content-Type: application/json' \
-d '{
"title": "My First Item",
"description": "This is an example item",
"is_active": true
}'
```
### Get all items
```bash
curl -X 'GET' 'http://localhost:8000/api/v1/items'
```
### Get an item by ID
```bash
curl -X 'GET' 'http://localhost:8000/api/v1/items/1'
```
### Update an item
```bash
curl -X 'PUT' \
'http://localhost:8000/api/v1/items/1' \
-H 'Content-Type: application/json' \
-d '{
"title": "Updated Item",
"description": "This item has been updated"
}'
```
### Delete an item
```bash
curl -X 'DELETE' 'http://localhost:8000/api/v1/items/1'
```
## Database Configuration
The API uses SQLite as the database engine. The database file is located at `/app/storage/db/db.sqlite`. This path is configured in `app/core/config.py`. You can change the database configuration by modifying this file.
## Running with Docker
You can use Docker to run the API. Here's an example Dockerfile:
```dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
# Create storage directory
RUN mkdir -p /app/storage/db
# Run migrations
RUN alembic upgrade head
# Expose the port
EXPOSE 8000
# Run the application
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
```
To build and run the Docker container:
```bash
docker build -t genericrestapiservice .
docker run -p 8000:8000 genericrestapiservice
```
## Development
### Code Style
This project uses Ruff for linting and formatting. To lint the code:
```bash
ruff check .
```
To format the code:
```bash
ruff format .
```