-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/karpathy lab init #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
07142ab
8abd5c7
593dbcc
518f2ee
68ba6cd
12ae863
98a4e82
1a33328
9a17805
3878de9
2aded84
2f0c512
ed2f661
51cda12
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,41 +1,7 @@ | ||
| import os | ||
| import sys | ||
| from pathlib import Path | ||
| from fastapi import APIRouter | ||
|
|
||
| # Add the project root to Python path | ||
| project_root = Path(__file__).parent.parent | ||
| sys.path.insert(0, str(project_root)) | ||
| router = APIRouter() | ||
|
|
||
| # Set environment variables for production | ||
| os.environ.setdefault("ENVIRONMENT", "production") | ||
| os.environ.setdefault( | ||
| "SECRET_KEY", os.environ.get("SECRET_KEY", "vercel-production-key-change-in-env") | ||
| ) | ||
|
|
||
| # Import the FastAPI app | ||
| from app.main import app | ||
|
|
||
| # Vercel expects the app to be named 'app' | ||
| # If your FastAPI app is named differently, change this | ||
| app = app | ||
|
|
||
|
|
||
| # Optional: Add Vercel-specific middleware or configuration | ||
| @app.middleware("http") | ||
| async def add_vercel_headers(request, call_next): | ||
| response = await call_next(request) | ||
| response.headers["X-Vercel-Cache"] = "MISS" | ||
| return response | ||
|
|
||
|
|
||
| # Health check endpoint for Vercel | ||
| @app.get("/api/health") | ||
| async def health_check(): | ||
| return {"status": "healthy", "platform": "vercel", "app": "NeuroBank FastAPI"} | ||
|
|
||
|
|
||
| # For local development | ||
| if __name__ == "__main__": | ||
| import uvicorn | ||
|
|
||
| uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 8000))) | ||
| @router.get("/") | ||
| def root(): | ||
| return {"status": "ok"} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,253 +1 @@ | ||
| import datetime | ||
| import logging | ||
| import os | ||
|
|
||
Neiland85 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| from fastapi import FastAPI | ||
| from fastapi.middleware.cors import CORSMiddleware | ||
| from fastapi.responses import JSONResponse | ||
|
|
||
| from .backoffice import router as backoffice_router | ||
| from .routers import operator | ||
| from .utils.logging import setup_logging | ||
|
|
||
| # Configuración constantes | ||
| APP_NAME = "NeuroBank FastAPI Toolkit" | ||
| APP_VERSION = "1.0.0" | ||
| APP_DESCRIPTION = """ | ||
| ## 🏦 NeuroBank FastAPI Toolkit | ||
| **Professional banking operations API** with enterprise-grade features and **admin backoffice dashboard**: | ||
| ### 🚀 Key Features | ||
| - **Banking Operations**: Comprehensive account management and transactions | ||
| - **Admin Dashboard**: Visual backoffice panel at `/backoffice/` with real-time metrics | ||
| - **Security First**: API key authentication and request validation | ||
| - **Production Ready**: AWS serverless deployment with monitoring | ||
| - **High Performance**: Async operations with optimized response times | ||
| ### 🎨 Backoffice Dashboard | ||
| - **Real-time Metrics**: Live transaction monitoring and system health | ||
| - **Interactive Charts**: Chart.js visualizations for business intelligence | ||
| - **Transaction Management**: Advanced filtering and administration tools | ||
| - **Responsive Design**: Bootstrap 5 with professional banking UI | ||
| - **Protected Admin Panels**: Secure administrative access | ||
| ### 🛠️ Technical Stack | ||
| - **FastAPI**: Modern, fast web framework for building APIs | ||
| - **Jinja2**: Template engine for dynamic HTML generation | ||
| - **Bootstrap 5**: Professional UI framework with responsive design | ||
| - **Chart.js**: Interactive data visualizations | ||
| - **Pydantic**: Data validation using Python type annotations | ||
| - **AWS Lambda**: Serverless compute platform | ||
| - **CloudWatch**: Monitoring and logging | ||
| ### 📚 API Documentation | ||
| - **Swagger UI**: Available at `/docs` (interactive documentation) | ||
| - **ReDoc**: Available at `/redoc` (alternative documentation) | ||
| - **Admin Dashboard**: Available at `/backoffice/` (visual interface) | ||
| ### 🔐 Authentication | ||
| All endpoints require a valid API key in the `X-API-Key` header. | ||
| ### 📊 Health Monitoring | ||
| - Health check endpoint at `/health` | ||
| - Comprehensive logging with structured JSON format | ||
| - CloudWatch integration for production monitoring | ||
| --- | ||
| *Built with ❤️ for modern banking infrastructure* | ||
| """ | ||
|
|
||
| # Configurar logging | ||
| setup_logging() | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
| # Crear la aplicación FastAPI con documentación mejorada | ||
| app = FastAPI( | ||
| title=APP_NAME, | ||
| description=APP_DESCRIPTION, | ||
| version=APP_VERSION, | ||
| docs_url="/docs", | ||
| redoc_url="/redoc", | ||
| contact={ | ||
| "name": "NeuroBank Development Team", | ||
| "email": "[email protected]", | ||
| }, | ||
| license_info={ | ||
| "name": "MIT License", | ||
| "url": "https://opensource.org/licenses/MIT", | ||
| }, | ||
| servers=[ | ||
| {"url": "https://api.neurobank.com", "description": "Production server"}, | ||
| {"url": "https://staging-api.neurobank.com", "description": "Staging server"}, | ||
| {"url": "http://localhost:8000", "description": "Development server"}, | ||
| ], | ||
| ) | ||
|
|
||
| # Configurar CORS - usando configuración de Railway | ||
| from .config import get_settings | ||
|
|
||
| settings = get_settings() | ||
|
|
||
| app.add_middleware( | ||
| CORSMiddleware, | ||
| allow_origins=settings.cors_origins, | ||
| allow_credentials=True, | ||
| allow_methods=["GET", "POST", "PUT", "DELETE"], | ||
| allow_headers=["*"], | ||
| ) | ||
|
|
||
| # Incluir routers | ||
| app.include_router(operator.router, prefix="/api", tags=["api"]) | ||
| app.include_router(backoffice_router.router, tags=["backoffice"]) | ||
|
|
||
|
|
||
| # Health check endpoint | ||
| @app.get( | ||
| "/health", | ||
| summary="🏥 Health Check", | ||
| description="Comprehensive health check endpoint for monitoring service status", | ||
| response_description="Service health status with detailed information", | ||
| tags=["Health & Monitoring"], | ||
| responses={ | ||
| 200: { | ||
| "description": "Service is healthy and operational", | ||
| "content": { | ||
| "application/json": { | ||
| "example": { | ||
| "status": "healthy", | ||
| "service": "NeuroBank FastAPI Toolkit", | ||
| "version": "1.0.0", | ||
| "timestamp": "2025-07-20T15:30:45.123456Z", | ||
| "environment": "production", | ||
| "uptime_seconds": 3600, | ||
| } | ||
| } | ||
| }, | ||
| } | ||
| }, | ||
| ) | ||
| async def health_check(): | ||
| """ | ||
| **Endpoint de verificación de salud del sistema** | ||
| Retorna información detallada sobre: | ||
| - ✅ Estado del servicio | ||
| - 📊 Versión actual | ||
| - ⏰ Timestamp de respuesta | ||
| - 🌍 Entorno de ejecución | ||
| - ⏱️ Tiempo de actividad | ||
| **Casos de uso:** | ||
| - Monitoreo automatizado (Kubernetes, Docker, AWS) | ||
| - Load balancers health checks | ||
| - Verificación de deployments | ||
| - Debugging y troubleshooting | ||
| """ | ||
| import datetime | ||
| import os | ||
|
|
||
| return JSONResponse( | ||
| status_code=200, | ||
| content={ | ||
| "status": "healthy", | ||
| "service": APP_NAME, | ||
| "version": APP_VERSION, | ||
| "timestamp": f"{datetime.datetime.now(datetime.timezone.utc).isoformat()}", | ||
| "environment": os.getenv("ENVIRONMENT", "production"), | ||
| "railway": { | ||
| "project_name": os.getenv("RAILWAY_PROJECT_NAME", "unknown"), | ||
| "project_id": os.getenv("RAILWAY_PROJECT_ID", "unknown"), | ||
| "service_name": os.getenv("RAILWAY_SERVICE_NAME", "unknown"), | ||
| "environment_name": os.getenv("RAILWAY_ENVIRONMENT_NAME", "unknown"), | ||
| "private_domain": os.getenv("RAILWAY_PRIVATE_DOMAIN", "unknown"), | ||
| }, | ||
| "uptime_seconds": "N/A", # Se puede implementar con un contador global | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| # Root endpoint | ||
| @app.get( | ||
| "/", | ||
| summary="🏠 API Root", | ||
| description="Welcome endpoint with API information and navigation links", | ||
| response_description="API overview with quick navigation", | ||
| tags=["Information"], | ||
| responses={ | ||
| 200: { | ||
| "description": "API information and navigation links", | ||
| "content": { | ||
| "application/json": { | ||
| "example": { | ||
| "message": "Welcome to NeuroBank FastAPI Toolkit", | ||
| "version": "1.0.0", | ||
| "status": "operational", | ||
| "documentation": {"swagger_ui": "/docs", "redoc": "/redoc"}, | ||
| "endpoints": { | ||
| "health_check": "/health", | ||
| "operator_operations": "/operator", | ||
| }, | ||
| "features": [ | ||
| "🏦 Banking Operations", | ||
| "🔐 API Key Authentication", | ||
| "📊 Real-time Monitoring", | ||
| "☁️ AWS Serverless Ready", | ||
| ], | ||
| } | ||
| } | ||
| }, | ||
| } | ||
| }, | ||
| ) | ||
| async def root(): | ||
| """ | ||
| **Endpoint de bienvenida de la API** | ||
| Proporciona información general sobre la API incluyendo: | ||
| - 📋 Información básica del servicio | ||
| - 🔗 Enlaces de navegación rápida | ||
| - 📚 Acceso a documentación | ||
| - ⚡ Estado operacional | ||
| - 🎯 Características principales | ||
| **Útil para:** | ||
| - Verificación rápida de conectividad | ||
| - Descubrimiento de endpoints principales | ||
| - Validación de versión de API | ||
| - Navegación inicial para desarrolladores | ||
| """ | ||
| return { | ||
| "message": f"Welcome to {APP_NAME}", | ||
| "version": APP_VERSION, | ||
| "status": "operational", | ||
| "documentation": {"swagger_ui": "/docs", "redoc": "/redoc"}, | ||
| "endpoints": {"health_check": "/health", "operator_operations": "/operator"}, | ||
| "features": [ | ||
| "🏦 Banking Operations", | ||
| "🔐 API Key Authentication", | ||
| "📊 Real-time Monitoring", | ||
| "☁️ AWS Serverless Ready", | ||
| ], | ||
| } | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import uvicorn | ||
| import uvloop | ||
|
|
||
| # Use uvloop for better performance | ||
| uvloop.install() | ||
|
|
||
| port = int(os.getenv("PORT", 8000)) | ||
| workers = int(os.getenv("WORKERS", 1)) # Single worker for Railway | ||
|
|
||
| uvicorn.run( | ||
| "app.main:app", | ||
| host="0.0.0.0", | ||
| port=port, | ||
| workers=workers, | ||
| loop="uvloop", | ||
| access_log=True, | ||
| timeout_keep_alive=120, | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| #!/bin/bash | ||
| autoflake --in-place --remove-unused-variables --remove-all-unused-imports -r app | ||
|
||
Uh oh!
There was an error while loading. Please reload this page.