Files
roa2web-service-auto/data-entry-app/backend/app/main.py
Marius Mutu 3295f60faa feat: Improve Windows deployment and fix production paths
Data Entry App:
- Fix shared path finding for both dev and production environments
- Add base URL support for IIS subdirectory deployment (/data-entry/)
- Use import.meta.env.BASE_URL in router for correct path handling
- Add email-validator and python-jose dependencies

Deployment Scripts:
- Enhance Build-ROA2WEB.ps1 with improved build process
- Update ROA2WEB-Console.ps1 with Data Entry support
- Improve Publish-And-Deploy.ps1 deployment workflow
- Update deploy-config.json with new settings

Gitignore:
- Add more build cache patterns to ignore
- Add temp frontend build directories

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-18 19:44:15 +02:00

174 lines
5.2 KiB
Python

"""FastAPI application entry point for Data Entry App."""
import sys
import logging
import threading
from pathlib import Path
from contextlib import asynccontextmanager
# Load .env file BEFORE any imports that use os.getenv()
from dotenv import load_dotenv
load_dotenv()
from fastapi import FastAPI
# Configure logging to show INFO level messages
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
datefmt='%H:%M:%S'
)
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
# Add shared modules to path
# Development: data-entry-app/backend/app/main.py -> 4 parents to project root
# Production: data-entry-backend/app/main.py -> 3 parents to roa2web root
def find_shared_path():
"""Find shared folder - works in both dev and production."""
current = Path(__file__).parent # app/
# Try different parent levels to find shared folder
for levels in range(1, 6):
candidate = current
for _ in range(levels):
candidate = candidate.parent
shared_path = candidate / "shared"
if shared_path.exists() and (shared_path / "auth").exists():
return shared_path
# Fallback to original logic
return Path(__file__).parent.parent.parent.parent / "shared"
shared_path = find_shared_path()
sys.path.insert(0, str(shared_path))
from app.config import settings
from app.db.database import init_db
# Import Oracle pool for auth service
from database.oracle_pool import oracle_pool
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application lifespan - startup and shutdown events."""
# Startup
print(f"Starting {settings.app_name} v{settings.app_version}")
# Initialize Oracle pool (required for authentication)
try:
await oracle_pool.initialize()
print("Oracle pool initialized")
except Exception as e:
print(f"Warning: Oracle pool initialization failed: {e}")
print("Authentication will not work without Oracle connection")
# Initialize SQLite database
await init_db()
print("Database initialized")
# Ensure upload directory exists
settings.upload_path_resolved
print(f"Upload path: {settings.upload_path_resolved}")
# Pre-initialize OCR engine in background (PaddleOCR takes 15-20s)
def init_ocr_background():
try:
from app.services.ocr_service import ocr_service
ocr_service.ocr_engine._init_paddle_lazy()
print("OCR engine ready")
except Exception as e:
print(f"Warning: OCR engine pre-load failed: {e}")
print("Starting OCR engine pre-load (background)...")
threading.Thread(target=init_ocr_background, daemon=True).start()
yield
# Shutdown
print("Shutting down...")
try:
await oracle_pool.close()
print("Oracle pool closed")
except Exception as e:
print(f"Warning: Oracle pool close failed: {e}")
# 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=["*"],
)
# Authentication middleware
from auth.middleware import AuthenticationMiddleware
app.add_middleware(
AuthenticationMiddleware,
excluded_paths=["/docs", "/redoc", "/openapi.json", "/health", "/", "/api/auth/login", "/api/auth/refresh"]
)
# 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, nomenclature
app.include_router(receipts.router, prefix="/api/receipts", tags=["receipts"])
app.include_router(ocr.router, prefix="/api/ocr", tags=["ocr"])
app.include_router(nomenclature.router, prefix="/api/nomenclature", tags=["nomenclature"])
# Auth router
from auth.routes import create_auth_router
auth_router = create_auth_router(prefix="") # No prefix - we set it in include_router
app.include_router(auth_router, prefix="/api/auth", tags=["auth"])
# Shared routes (companies, calendar)
from routes.companies import create_companies_router
from routes.calendar import create_calendar_router
companies_router = create_companies_router(oracle_pool) # No cache for data-entry
calendar_router = create_calendar_router(oracle_pool)
app.include_router(companies_router, prefix="/api/companies", tags=["companies"])
app.include_router(calendar_router, prefix="/api/calendar", tags=["calendar"])
# Root endpoint
@app.get("/")
async def root():
"""Root endpoint - API information."""
return {
"name": settings.app_name,
"version": settings.app_version,
"docs": "/docs",
"health": "/health",
}