
- Fix 'No module named app' error by adding project root to Python path - Ensure consistent database path between app and migrations - Update database configuration to work in both development and deployment - Set appropriate SQLite dialect configuration with render_as_batch - Update documentation with additional migration instructions - Fix imports order to comply with linting standards
124 lines
3.6 KiB
Markdown
124 lines
3.6 KiB
Markdown
# Generic REST API Service
|
|
|
|
This is a FastAPI application that provides a simple generic REST API service with CRUD operations for an "Item" resource.
|
|
|
|
## Features
|
|
|
|
- FastAPI-based RESTful API
|
|
- SQLite database with SQLAlchemy ORM
|
|
- Alembic for database migrations
|
|
- Pydantic for data validation
|
|
- Comprehensive CRUD operations
|
|
- Health check endpoint
|
|
- Automatic OpenAPI documentation
|
|
|
|
## Project Structure
|
|
|
|
```
|
|
├── alembic/ # Database migration files
|
|
│ ├── versions/ # Migration version scripts
|
|
│ ├── env.py # Alembic environment configuration
|
|
│ └── script.py.mako # Migration script template
|
|
├── app/ # Application package
|
|
│ ├── api/ # API routes
|
|
│ │ ├── endpoints/ # API endpoint modules
|
|
│ │ ├── api.py # API router setup
|
|
│ │ └── deps.py # Dependency injection functions
|
|
│ ├── core/ # Core application modules
|
|
│ │ └── config.py # Application configuration
|
|
│ ├── crud/ # CRUD operation modules
|
|
│ │ ├── base.py # Base CRUD operations
|
|
│ │ └── crud_item.py # Item-specific CRUD operations
|
|
│ ├── db/ # Database modules
|
|
│ │ ├── base.py # Import all models
|
|
│ │ ├── base_class.py # Base model class
|
|
│ │ ├── init_db.py # Database initialization
|
|
│ │ └── session.py # DB session setup
|
|
│ ├── models/ # SQLAlchemy models
|
|
│ │ └── item.py # Item model
|
|
│ └── schemas/ # Pydantic schemas
|
|
│ └── item.py # Item schema
|
|
├── alembic.ini # Alembic configuration
|
|
├── main.py # Application entry point
|
|
└── requirements.txt # Project dependencies
|
|
```
|
|
|
|
## Setup and Installation
|
|
|
|
1. Clone the repository:
|
|
|
|
```bash
|
|
git clone <repository-url>
|
|
cd generic-rest-api-service
|
|
```
|
|
|
|
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. Run database migrations:
|
|
|
|
```bash
|
|
# Make sure you are in the project root directory
|
|
alembic upgrade head
|
|
|
|
# If you encounter any module import errors, you can also try:
|
|
PYTHONPATH=$PWD alembic upgrade head
|
|
```
|
|
|
|
5. Start the application:
|
|
|
|
```bash
|
|
uvicorn main:app --reload
|
|
```
|
|
|
|
The application will be available at http://127.0.0.1:8000.
|
|
|
|
## API Documentation
|
|
|
|
Once the application is running, you can access the automatically generated API documentation:
|
|
|
|
- Swagger UI: http://127.0.0.1:8000/docs
|
|
- ReDoc: http://127.0.0.1:8000/redoc
|
|
|
|
## API Endpoints
|
|
|
|
### Health Check
|
|
|
|
- GET `/api/v1/health`: Check if the service is healthy
|
|
|
|
### Items
|
|
|
|
- GET `/api/v1/items`: List all items
|
|
- Query parameters:
|
|
- `skip`: Number of items to skip (default: 0)
|
|
- `limit`: Maximum number of items to return (default: 100)
|
|
- `active_only`: If true, return only active items (default: false)
|
|
- POST `/api/v1/items`: Create a new item
|
|
- GET `/api/v1/items/{id}`: Get a specific item by ID
|
|
- PUT `/api/v1/items/{id}`: Update a specific item
|
|
- DELETE `/api/v1/items/{id}`: Delete a specific item (returns 204 No Content)
|
|
|
|
## Development
|
|
|
|
### Running Tests
|
|
|
|
```bash
|
|
# Add test command here
|
|
```
|
|
|
|
### Linting with Ruff
|
|
|
|
```bash
|
|
ruff check .
|
|
ruff format .
|
|
``` |