
- 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
2.8 KiB
2.8 KiB
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
- Install dependencies:
pip install -r requirements.txt
- Run database migrations:
alembic upgrade head
Running the Application
Start the server with uvicorn:
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 informationGET /health
- Health check
Items API (/api/v1/items
)
POST /
- Create a new itemGET /
- List all items (with pagination)GET /{item_id}
- Get item by IDPUT /{item_id}
- Update item by IDDELETE /{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:
alembic revision --autogenerate -m "Description of changes"
Apply migrations:
alembic upgrade head
Development
Code Quality
The project uses Ruff for code formatting and linting:
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.