from fastapi import FastAPI, Depends from fastapi.middleware.cors import CORSMiddleware from fastapi.openapi.utils import get_openapi from sqlalchemy.orm import Session from app.db.session import get_db, engine from app.db.base import Base from app.api import auth, wallet, projects, trading import os # Create database tables Base.metadata.create_all(bind=engine) app = FastAPI( title="Carbon Offset Trading Platform", description="A blockchain-enabled platform for trading carbon offsets between project developers and buyers", version="1.0.0", docs_url="/docs", redoc_url="/redoc" ) # CORS configuration app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Include routers app.include_router(auth.router, prefix="/auth", tags=["Authentication"]) app.include_router(wallet.router, prefix="/wallet", tags=["Wallet"]) app.include_router(projects.router, prefix="/projects", tags=["Projects"]) app.include_router(trading.router, prefix="/trading", tags=["Trading"]) @app.get("/") async def root(): """Base endpoint with platform information""" return { "title": "Carbon Offset Trading Platform", "description": "A blockchain-enabled platform for trading carbon offsets", "version": "1.0.0", "documentation": "/docs", "health_check": "/health" } @app.get("/health") async def health_check(db: Session = Depends(get_db)): """Health check endpoint""" try: # Test database connection db.execute("SELECT 1") db_status = "healthy" except Exception as e: db_status = f"unhealthy: {str(e)}" # Check environment variables env_status = "healthy" required_envs = ["SECRET_KEY"] missing_envs = [env for env in required_envs if not os.getenv(env)] if missing_envs: env_status = f"missing environment variables: {', '.join(missing_envs)}" return { "status": "healthy" if db_status == "healthy" and env_status == "healthy" else "unhealthy", "database": db_status, "environment": env_status, "version": "1.0.0" } # Custom OpenAPI schema def custom_openapi(): if app.openapi_schema: return app.openapi_schema openapi_schema = get_openapi( title="Carbon Offset Trading Platform API", version="1.0.0", description="A blockchain-enabled platform for trading carbon offsets between project developers and buyers", routes=app.routes, ) app.openapi_schema = openapi_schema return app.openapi_schema app.openapi = custom_openapi if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000)