Document Analyzer Operator - Backend API
Production-ready FastAPI backend for the Document Analyzer Operator Platform.
FastAPI - Modern, fast web framework for building APIs
PostgreSQL - Primary database with async SQLAlchemy
Redis - Caching, session management, and token blacklist
JWT Authentication - Secure token-based authentication with refresh tokens
WebSocket - Real-time event streaming
Alembic - Database migrations
Docker - Containerized deployment
Type Safety - Full type hints with Pydantic validation
Testing - Comprehensive test suite with pytest
Python 3.11+
Docker and Docker Compose (for containerized deployment)
Poetry (for local development)
Option 1: Docker Compose (Recommended)
Clone the repository
Copy environment file
Update environment variables
Edit .env and set:
SECRET_KEY - Generate a secure random key
Database credentials (if not using defaults)
LLM API keys (optional)
Start services
Access the API
Option 2: Local Development
Install Poetry
curl -sSL https://install.python-poetry.org | python3 -
Install dependencies
Set up environment
cp .env.example .env
# Edit .env with your settings
Start PostgreSQL and Redis
Using Docker:
docker-compose up -d postgres redis
Or install locally.
Run database migrations
poetry run alembic upgrade head
Start the server
poetry run uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
Access the API
backend/
├── app/
│ ├── api/
│ │ ├── v1/
│ │ │ ├── routes/
│ │ │ │ ├── auth.py # Authentication endpoints
│ │ │ │ ├── agents.py # Agent CRUD endpoints
│ │ │ │ ├── health.py # Health check endpoints
│ │ │ │ └── websocket.py # WebSocket endpoint
│ │ │ └── router.py # API router configuration
│ │ └── deps.py # API dependencies
│ ├── core/
│ │ ├── settings.py # Application settings
│ │ ├── security.py # Security utilities
│ │ └── logging_config.py # Logging configuration
│ ├── db/
│ │ └── session.py # Database session management
│ ├── models/
│ │ ├── base.py # Base model with common fields
│ │ ├── user.py # User model
│ │ ├── agent.py # Agent model
│ │ ├── agent_type.py # Agent type model
│ │ ├── workflow.py # Workflow model
│ │ ├── task.py # Task model
│ │ ├── workspace.py # Workspace model
│ │ ├── knowledge_entity.py # Knowledge entity model
│ │ └── validation_result.py # Validation result model
│ ├── schemas/
│ │ ├── token.py # Token schemas
│ │ ├── user.py # User schemas
│ │ ├── agent.py # Agent schemas
│ │ └── auth.py # Authentication schemas
│ ├── services/ # Business logic services
│ ├── utils/ # Utility functions
│ ├── websocket/
│ │ ├── manager.py # WebSocket connection manager
│ │ └── events.py # Event system
│ └── main.py # FastAPI application entry point
├── alembic/
│ └── versions/ # Database migrations
├── tests/
│ ├── conftest.py # Pytest fixtures
│ ├── test_auth.py # Authentication tests
│ ├── test_agents.py # Agent tests
│ └── test_security.py # Security tests
├── .env.example # Environment variables template
├── docker-compose.yml # Docker Compose configuration
├── Dockerfile # Docker build configuration
├── pyproject.toml # Poetry dependencies
└── README.md # This file
Method
Endpoint
Description
POST
/api/v1/auth/login
Login and get tokens
POST
/api/v1/auth/logout
Logout and blacklist token
POST
/api/v1/auth/refresh
Refresh access token
GET
/api/v1/auth/me
Get current user info
POST
/api/v1/auth/register
Register new user
Method
Endpoint
Description
GET
/api/v1/agents
List user's agents
GET
/api/v1/agents/{id}
Get agent by ID
POST
/api/v1/agents
Create new agent
PATCH
/api/v1/agents/{id}
Update agent
DELETE
/api/v1/agents/{id}
Delete agent
Method
Endpoint
Description
GET
/api/v1/health
Basic health check
GET
/api/v1/ready
Readiness check
GET
/api/v1/live
Liveness check
Endpoint
Description
/api/v1/ws
Real-time event streaming
Variable
Default
Description
APP_NAME
Document Analyzer Operator
Application name
APP_DEBUG
false
Debug mode
APP_ENVIRONMENT
development
Environment (development/production)
SECRET_KEY
-
JWT secret key (required)
Variable
Default
Description
POSTGRES_USER
postgres
Database user
POSTGRES_PASSWORD
postgres
Database password
POSTGRES_HOST
localhost
Database host
POSTGRES_PORT
5432
Database port
POSTGRES_DB
document_analyzer
Database name
Variable
Default
Description
REDIS_HOST
localhost
Redis host
REDIS_PORT
6379
Redis port
poetry run pytest --cov=app
poetry run pytest tests/test_auth.py -v
poetry run alembic revision --autogenerate -m " Description of changes"
poetry run alembic upgrade head
poetry run alembic downgrade -1
poetry run alembic history
Change the SECRET_KEY in production - Generate a secure random key
Use HTTPS in production - Configure SSL/TLS
Set APP_DEBUG=false in production
Use strong passwords for database and Redis
Rotate API keys regularly
Enable rate limiting for production use
Review CORS settings for your domain
Database connection errors
Verify PostgreSQL is running: docker-compose ps
Check credentials in .env
Ensure network connectivity
Verify Redis is running: docker-compose ps
Check Redis URL in .env
Ensure dependencies are installed: poetry install
Activate virtual environment: poetry shell
MIT