Files
roa2web-service-auto/data-entry-app/backend/app/main.py
Marius Mutu 41ae97180e feat: Add OCR integration for automatic receipt data extraction
Implement Tesseract-based OCR to automatically extract vendor name,
date, total amount, and VAT from uploaded receipt images/PDFs,
reducing manual data entry and improving accuracy.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-12 11:48:29 +02:00

90 lines
2.2 KiB
Python

"""FastAPI application entry point for Data Entry App."""
import sys
from pathlib import Path
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
# Add shared modules to path
project_root = Path(__file__).parent.parent.parent.parent
sys.path.insert(0, str(project_root / "shared"))
from app.config import settings
from app.db.database import init_db
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application lifespan - startup and shutdown events."""
# Startup
print(f"Starting {settings.app_name} v{settings.app_version}")
# Initialize database
await init_db()
print("Database initialized")
# Ensure upload directory exists
settings.upload_path_resolved
print(f"Upload path: {settings.upload_path_resolved}")
yield
# Shutdown
print("Shutting down...")
# Create FastAPI app
app = FastAPI(
title=settings.app_name,
version=settings.app_version,
description="API pentru introducere bonuri fiscale cu workflow de aprobare",
lifespan=lifespan,
)
# CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_origins_list,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Mount static files for uploads (optional - can serve through nginx in prod)
uploads_path = Path(settings.upload_path)
if uploads_path.exists():
app.mount("/uploads", StaticFiles(directory=str(uploads_path)), name="uploads")
# Health check endpoint
@app.get("/health")
async def health_check():
"""Health check endpoint."""
return {
"status": "healthy",
"app": settings.app_name,
"version": settings.app_version,
}
# Import and include routers
from app.routers import receipts, ocr
app.include_router(receipts.router, prefix="/api/receipts", tags=["receipts"])
app.include_router(ocr.router, prefix="/api/ocr", tags=["ocr"])
# Root endpoint
@app.get("/")
async def root():
"""Root endpoint - API information."""
return {
"name": settings.app_name,
"version": settings.app_version,
"docs": "/docs",
"health": "/health",
}