Files
orion/main.py
2025-09-21 13:00:10 +02:00

171 lines
5.9 KiB
Python

import logging
from datetime import datetime
from fastapi import Depends, FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse, RedirectResponse
from sqlalchemy import text
from sqlalchemy.orm import Session
from app.api.main import api_router
from app.core.config import settings
from app.core.database import get_db
from app.core.lifespan import lifespan
logger = logging.getLogger(__name__)
# FastAPI app with lifespan
app = FastAPI(
title=settings.project_name,
description=settings.description,
version=settings.version,
lifespan=lifespan,
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=settings.allowed_hosts,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include API router
app.include_router(api_router, prefix="/api/v1")
# Public Routes (no authentication required)
# Core application endpoints (Public Routes, no authentication required)
@app.get("/", include_in_schema=False)
async def root():
"""Redirect root to documentation"""
return RedirectResponse(url="/documentation")
@app.get("/health")
def health_check(db: Session = Depends(get_db)):
"""Health check endpoint"""
try:
# Test database connection
db.execute(text("SELECT 1"))
return {
"status": "healthy",
"timestamp": datetime.utcnow(),
"message": f"{settings.project_name} v{settings.version}",
"docs": {
"swagger": "/docs",
"redoc": "/redoc",
"openapi": "/openapi.json",
"complete": "Documentation site URL here",
},
"features": [
"JWT Authentication",
"Marketplace-aware product import",
"Multi-shop product management",
"Stock management with location tracking",
],
"supported_marketplaces": [
"Letzshop",
],
"auth_required": "Most endpoints require Bearer token authentication",
}
except Exception as e:
logger.error(f"Health check failed: {e}")
raise HTTPException(status_code=503, detail="Service unhealthy")
# Add this temporary endpoint to your router:
# Documentation redirect endpoints
@app.get("/documentation", response_class=HTMLResponse, include_in_schema=False)
async def documentation_page():
"""Enhanced documentation hub page"""
return """
<!DOCTYPE html>
<html>
<head>
<title>Letzshop Import - Documentation</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
max-width: 800px;
margin: 40px auto;
padding: 20px;
line-height: 1.6;
}
.header { text-align: center; margin-bottom: 40px; }
.logo { font-size: 2em; margin-bottom: 10px; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 20px; margin: 30px 0; }
.card {
border: 1px solid #e1e5e9;
border-radius: 8px;
padding: 20px;
transition: box-shadow 0.2s;
text-decoration: none;
color: inherit;
}
.card:hover { box-shadow: 0 4px 12px rgba(0,0,0,0.1); }
.card h3 { margin-top: 0; color: #1976d2; }
.badge {
display: inline-block;
background: #1976d2;
color: white;
padding: 4px 8px;
border-radius: 4px;
font-size: 0.8em;
margin-left: 10px;
}
</style>
</head>
<body>
<div class="header">
<div class="logo">📚 Letzshop Import Documentation</div>
<p>Choose your documentation experience</p>
</div>
<div class="cards">
<a href="/docs" class="card">
<h3>🔧 Interactive API Docs <span class="badge">LIVE</span></h3>
<p>Swagger UI interface for testing API endpoints directly in your browser. Perfect for development and API exploration.</p>
</a>
<a href="/redoc" class="card">
<h3>📖 API Reference</h3>
<p>Clean, readable API documentation with ReDoc. Great for understanding API structure and parameters.</p>
</a>
<a href="#" class="card">
<h3>📚 Complete Documentation</h3>
<p>Comprehensive project documentation with guides, tutorials, architecture, and development info.</p>
</a>
<a href="#" class="card">
<h3>🚀 Getting Started</h3>
<p>Step-by-step installation and setup guide to get you up and running quickly.</p>
</a>
<a href="#" class="card">
<h3>🧪 Testing Guide</h3>
<p>Testing conventions, how to run tests, and contribute to the test suite.</p>
</a>
<a href="/health" class="card">
<h3>💚 System Health</h3>
<p>Check the current status and health of the API and its dependencies.</p>
</a>
</div>
<div style="text-align: center; margin-top: 40px; color: #666;">
<p>Need help? Check our <a href="https://github.com/yourusername/letzshop-import/issues">GitHub Issues</a></p>
</div>
</body>
</html>
"""
if __name__ == "__main__":
import uvicorn
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)