"""
Trusty Sign API Server
Starts FastAPI server with Trusty Sign routes
"""
import os
import sys
import uvicorn
from pathlib import Path
# Add src to path
sys.path.insert(0, str(Path(__file__).parent))
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from src.api.trusty_sign_routes import router as trusty_sign_router
# Create FastAPI app
app = FastAPI(
title="Trusty Sign API",
description="AI-powered signature assistant by Tinexta InfoCert",
version="1.0.0",
docs_url="/docs",
redoc_url="/redoc"
)
# CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # In production: restrict to PWA domain
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include Trusty Sign routes
app.include_router(trusty_sign_router)
@app.get("/")
async def root():
"""Root endpoint"""
return {
"service": "Trusty Sign API",
"version": "1.0.0",
"status": "running",
"docs": "/docs"
}
@app.get("/health")
async def health():
"""Global health check"""
return {"status": "healthy"}
@app.get("/attachments/{filename}")
async def serve_attachment(filename: str):
"""
Serve attachment files for Infocert signature
Files are stored in /app/attachments and need to be accessible via public URL
"""
attachments_dir = Path("/app/attachments")
file_path = attachments_dir / filename
# Security: prevent directory traversal
if not file_path.resolve().is_relative_to(attachments_dir.resolve()):
raise HTTPException(status_code=403, detail="Access forbidden")
if not file_path.exists():
raise HTTPException(status_code=404, detail="File not found")
return FileResponse(
path=str(file_path),
media_type="application/pdf",
filename=filename
)
if __name__ == "__main__":
# Get config from env
host = os.getenv("API_HOST", "0.0.0.0")
port = int(os.getenv("API_PORT", "9000"))
reload = os.getenv("API_RELOAD", "true").lower() == "true"
print("π Starting Trusty Sign API Server")
print(f"π Host: {host}")
print(f"π Port: {port}")
print(f"π Docs: http://{host}:{port}/docs")
print(f"π Reload: {reload}")
print()
uvicorn.run(
"start_trusty_sign_api:app",
host=host,
port=port,
reload=reload,
log_level="info"
)