
- Set up FastAPI application with CORS support - Created SQLite database configuration with absolute paths - Implemented Todo model with SQLAlchemy - Added full CRUD operations for todos - Created API endpoints with proper error handling - Set up Alembic for database migrations - Added health check and base URL endpoints - Updated README with comprehensive documentation - Configured project structure following best practices Features: - Complete Todo CRUD API - SQLite database with proper path configuration - Database migrations with Alembic - API documentation at /docs and /redoc - Health check endpoint at /health - CORS enabled for all origins - Proper error handling and validation
148 lines
3.6 KiB
Markdown
148 lines
3.6 KiB
Markdown
# Todo API
|
|
|
|
A simple Todo API built with FastAPI and SQLite. This application provides a RESTful API for managing todo items with full CRUD operations.
|
|
|
|
## Features
|
|
|
|
- Create, read, update, and delete todo items
|
|
- SQLite database with SQLAlchemy ORM
|
|
- Database migrations with Alembic
|
|
- Automatic API documentation with FastAPI
|
|
- CORS support for cross-origin requests
|
|
- Health check endpoint
|
|
- Proper error handling and validation
|
|
|
|
## Project Structure
|
|
|
|
```
|
|
todoapp-h5yaal/
|
|
├── main.py # FastAPI application entry point
|
|
├── requirements.txt # Python dependencies
|
|
├── alembic.ini # Alembic configuration
|
|
├── alembic/ # Database migrations
|
|
│ ├── env.py
|
|
│ └── versions/
|
|
│ └── 001_initial_migration.py
|
|
└── app/
|
|
├── __init__.py
|
|
├── api/
|
|
│ ├── __init__.py
|
|
│ └── todos.py # Todo API endpoints
|
|
├── db/
|
|
│ ├── __init__.py
|
|
│ ├── base.py # SQLAlchemy Base
|
|
│ ├── session.py # Database session configuration
|
|
│ └── crud.py # Database operations
|
|
└── models/
|
|
├── __init__.py
|
|
├── todo.py # Todo SQLAlchemy model
|
|
└── schemas.py # Pydantic schemas
|
|
```
|
|
|
|
## Installation
|
|
|
|
1. Install dependencies:
|
|
```bash
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
2. Run database migrations:
|
|
```bash
|
|
alembic upgrade head
|
|
```
|
|
|
|
## Running the Application
|
|
|
|
Start the development server:
|
|
```bash
|
|
uvicorn main:app --reload --host 0.0.0.0 --port 8000
|
|
```
|
|
|
|
The application will be available at:
|
|
- API: http://localhost:8000
|
|
- Documentation: http://localhost:8000/docs
|
|
- Alternative docs: http://localhost:8000/redoc
|
|
- OpenAPI JSON: http://localhost:8000/openapi.json
|
|
- Health check: http://localhost:8000/health
|
|
|
|
## API Endpoints
|
|
|
|
### Base Endpoints
|
|
- `GET /` - API information and links
|
|
- `GET /health` - Health check endpoint
|
|
|
|
### Todo Endpoints
|
|
- `GET /api/v1/todos` - Get all todos (with pagination)
|
|
- `GET /api/v1/todos/{todo_id}` - Get a specific todo
|
|
- `POST /api/v1/todos` - Create a new todo
|
|
- `PUT /api/v1/todos/{todo_id}` - Update a todo
|
|
- `DELETE /api/v1/todos/{todo_id}` - Delete a todo
|
|
|
|
### Example API Usage
|
|
|
|
Create a todo:
|
|
```bash
|
|
curl -X POST "http://localhost:8000/api/v1/todos" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{"title": "Buy groceries", "description": "Milk, bread, eggs", "completed": false}'
|
|
```
|
|
|
|
Get all todos:
|
|
```bash
|
|
curl "http://localhost:8000/api/v1/todos"
|
|
```
|
|
|
|
Update a todo:
|
|
```bash
|
|
curl -X PUT "http://localhost:8000/api/v1/todos/1" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{"completed": true}'
|
|
```
|
|
|
|
## Database
|
|
|
|
The application uses SQLite database stored at `/app/storage/db/db.sqlite`. The database schema is managed through Alembic migrations.
|
|
|
|
### Database Schema
|
|
|
|
**Todos Table:**
|
|
- `id` (Integer, Primary Key)
|
|
- `title` (String, Required)
|
|
- `description` (Text, Optional)
|
|
- `completed` (Boolean, Default: False)
|
|
- `created_at` (DateTime)
|
|
- `updated_at` (DateTime)
|
|
|
|
## Development
|
|
|
|
### Running Migrations
|
|
|
|
Create a new migration:
|
|
```bash
|
|
alembic revision --autogenerate -m "Description of changes"
|
|
```
|
|
|
|
Apply migrations:
|
|
```bash
|
|
alembic upgrade head
|
|
```
|
|
|
|
### Code Quality
|
|
|
|
The project uses Ruff for linting and code formatting:
|
|
```bash
|
|
ruff check . --fix
|
|
```
|
|
|
|
## Environment Variables
|
|
|
|
This application doesn't currently require any environment variables, but you can extend it by adding configuration for:
|
|
- Database URL
|
|
- CORS origins
|
|
- Logging level
|
|
- API keys (if authentication is added)
|
|
|
|
## License
|
|
|
|
This project is generated by BackendIM.
|