# Task Manager API A RESTful API for managing tasks, built with FastAPI and SQLite. ## Features - Task CRUD operations - Task status and priority management - Task completion tracking - API documentation with Swagger UI and ReDoc - Health endpoint for monitoring ## Tech Stack - FastAPI: Modern, high-performance web framework for building APIs - SQLAlchemy: SQL toolkit and Object-Relational Mapping (ORM) - Alembic: Database migration tool - SQLite: Lightweight relational database - Pydantic: Data validation and settings management - Uvicorn: ASGI server for FastAPI applications ## API Endpoints ### API Information - `GET /`: Get API information and available endpoints ### Task Management - `GET /tasks`: Get all tasks - `POST /tasks`: Create a new task - `GET /tasks/{task_id}`: Get a specific task - `PUT /tasks/{task_id}`: Update a task - `DELETE /tasks/{task_id}`: Delete a task - `POST /tasks/{task_id}/complete`: Mark a task as completed ### Health and Diagnostic Endpoints - `GET /health`: Application health check - `GET /db-test`: Database connection diagnostic endpoint ## Example Curl Commands ### Create a new task ```bash curl -X 'POST' \ 'https://taskmanagerapi-ttkjqk.backend.im/tasks/' \ -H 'accept: application/json' \ -H 'Content-Type: application/json' \ -d '{ "title": "Complete project documentation", "description": "Write comprehensive documentation for the project", "priority": "high", "status": "todo", "due_date": "2023-06-30T18:00:00.000Z", "completed": false }' ``` ### Get all tasks ```bash curl -X 'GET' \ 'https://taskmanagerapi-ttkjqk.backend.im/tasks/' \ -H 'accept: application/json' ``` ### Get a specific task ```bash curl -X 'GET' \ 'https://taskmanagerapi-ttkjqk.backend.im/tasks/1' \ -H 'accept: application/json' ``` ### Update a task ```bash curl -X 'PUT' \ 'https://taskmanagerapi-ttkjqk.backend.im/tasks/1' \ -H 'accept: application/json' \ -H 'Content-Type: application/json' \ -d '{ "title": "Updated title", "description": "Updated description", "status": "in_progress" }' ``` ### Delete a task ```bash curl -X 'DELETE' \ 'https://taskmanagerapi-ttkjqk.backend.im/tasks/1' \ -H 'accept: application/json' ``` ### Mark a task as completed ```bash curl -X 'POST' \ 'https://taskmanagerapi-ttkjqk.backend.im/tasks/1/complete' \ -H 'accept: application/json' ``` ## Project Structure ``` taskmanagerapi/ ├── alembic/ # Database migrations │ └── versions/ # Migration scripts ├── app/ │ ├── api/ # API endpoints │ │ └── routers/ # API route definitions │ ├── core/ # Core application code │ ├── crud/ # CRUD operations │ ├── db/ # Database setup and models │ ├── models/ # SQLAlchemy models │ └── schemas/ # Pydantic schemas/models ├── main.py # Application entry point └── requirements.txt # Project dependencies ``` ## Database Configuration The application is configured to use SQLite with the following database locations (in order of priority): 1. Path defined in the `DB_PATH` environment variable (if set) 2. `/app/db/db.sqlite` (production environment) 3. `./db/db.sqlite` (local development) 4. `/tmp/taskmanager/db.sqlite` (fallback) The application automatically selects the first available and writable location from this list. ### Enhanced Database Reliability The application includes several features to ensure robust and reliable database operations: 1. **Multi-path discovery**: Automatically finds and uses the first writable database location 2. **Directory creation**: Creates database directories if they don't exist 3. **Database initialization**: Initializes the database with required tables using both SQLAlchemy and direct SQLite 4. **Connection retry logic**: Automatically retries database connections with exponential backoff 5. **SQLite optimizations**: Uses WAL journal mode and other SQLite performance optimizations 6. **Error diagnostic logging**: Comprehensive error logging for easier troubleshooting 7. **Fallback mechanisms**: Falls back to direct SQLite operations if SQLAlchemy fails ### Alembic Migration Enhancements Alembic migrations include robust error handling and initialization: 1. **Pre-migration checks**: Verifies database directories exist and are writable 2. **Auto-creation**: Creates necessary directories and initializes the database file 3. **Retry logic**: Implements retry logic for migration operations 4. **Detailed diagnostics**: Provides detailed error information for failed migrations ## Getting Started ### Installation 1. Clone the repository 2. Install dependencies: ``` pip install -r requirements.txt ``` ### Running Migrations ``` alembic upgrade head ``` The migration process will automatically: 1. Create the database directory if it doesn't exist 2. Initialize the database file if needed 3. Apply all migrations with retry logic for reliability ### Starting the Application ``` 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