
- Set up FastAPI application with CORS support - Implement SQLAlchemy models and database session management - Create CRUD API endpoints for items management - Configure Alembic for database migrations - Add health check and service info endpoints - Include comprehensive documentation and project structure - Integrate Ruff for code quality and formatting
110 lines
2.8 KiB
Markdown
110 lines
2.8 KiB
Markdown
# REST API Service
|
|
|
|
A comprehensive REST API built with FastAPI, SQLAlchemy, and SQLite.
|
|
|
|
## Features
|
|
|
|
- **FastAPI**: Modern, fast web framework for building APIs
|
|
- **SQLAlchemy**: SQL toolkit and Object-Relational Mapping (ORM) library
|
|
- **SQLite**: Lightweight database for data persistence
|
|
- **Alembic**: Database migration tool
|
|
- **CORS**: Cross-Origin Resource Sharing enabled for all origins
|
|
- **Health Check**: Built-in health monitoring endpoint
|
|
- **API Documentation**: Automatic interactive API docs via OpenAPI/Swagger
|
|
|
|
## Project Structure
|
|
|
|
```
|
|
.
|
|
├── main.py # Application entry point
|
|
├── requirements.txt # Python dependencies
|
|
├── alembic.ini # Alembic configuration
|
|
├── alembic/ # Database migrations
|
|
│ ├── env.py
|
|
│ ├── script.py.mako
|
|
│ └── versions/
|
|
└── app/
|
|
├── api/ # API routes
|
|
│ ├── items.py # Items CRUD endpoints
|
|
│ └── routes.py # Main router
|
|
├── db/ # Database configuration
|
|
│ ├── base.py # SQLAlchemy Base
|
|
│ └── session.py # Database session
|
|
├── models/ # SQLAlchemy models
|
|
│ └── item.py # Item model
|
|
└── schemas/ # Pydantic schemas
|
|
└── item.py # Item schemas
|
|
```
|
|
|
|
## Installation
|
|
|
|
1. Install dependencies:
|
|
```bash
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
2. Run database migrations:
|
|
```bash
|
|
alembic upgrade head
|
|
```
|
|
|
|
## Running the Application
|
|
|
|
Start the server with uvicorn:
|
|
```bash
|
|
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
|
|
```
|
|
|
|
The API will be available at:
|
|
- **API Base**: http://localhost:8000
|
|
- **Interactive Docs**: http://localhost:8000/docs
|
|
- **ReDoc**: http://localhost:8000/redoc
|
|
- **OpenAPI Schema**: http://localhost:8000/openapi.json
|
|
|
|
## API Endpoints
|
|
|
|
### Core Endpoints
|
|
- `GET /` - Service information
|
|
- `GET /health` - Health check
|
|
|
|
### Items API (`/api/v1/items`)
|
|
- `POST /` - Create a new item
|
|
- `GET /` - List all items (with pagination)
|
|
- `GET /{item_id}` - Get item by ID
|
|
- `PUT /{item_id}` - Update item by ID
|
|
- `DELETE /{item_id}` - Delete item by ID
|
|
|
|
## Database
|
|
|
|
The application uses SQLite with the database file stored at `/app/storage/db/db.sqlite`.
|
|
|
|
### Running Migrations
|
|
|
|
Create a new migration:
|
|
```bash
|
|
alembic revision --autogenerate -m "Description of changes"
|
|
```
|
|
|
|
Apply migrations:
|
|
```bash
|
|
alembic upgrade head
|
|
```
|
|
|
|
## Development
|
|
|
|
### Code Quality
|
|
|
|
The project uses Ruff for code formatting and linting:
|
|
```bash
|
|
ruff check .
|
|
ruff format .
|
|
```
|
|
|
|
### Environment Variables
|
|
|
|
No environment variables are required for basic operation. The application uses SQLite with a default file path.
|
|
|
|
## License
|
|
|
|
This project is open source and available under the MIT License.
|