
Features implemented: - User authentication with JWT tokens and role-based access (developer/buyer) - Blockchain wallet linking and management with Ethereum integration - Carbon project creation and management for developers - Marketplace for browsing and purchasing carbon offsets - Transaction tracking with blockchain integration - Database models for users, projects, offsets, and transactions - Comprehensive API with authentication, wallet, project, and trading endpoints - Health check endpoint and platform information - SQLite database with Alembic migrations - Full API documentation with OpenAPI/Swagger Technical stack: - FastAPI with Python - SQLAlchemy ORM with SQLite - Web3.py for blockchain integration - JWT authentication with bcrypt - CORS enabled for frontend integration - Comprehensive error handling and validation Environment variables required: - SECRET_KEY (JWT secret) - BLOCKCHAIN_RPC_URL (optional, defaults to localhost)
88 lines
2.7 KiB
Python
88 lines
2.7 KiB
Python
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) |