ethosnet/main.py
2024-09-30 00:26:05 +03:00

75 lines
1.9 KiB
Python

import logging
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from app.api.api_v1.api import api_router
from app.core.config import settings
from app.core.errors import CustomException
from app.db.session import SessionLocal
from app.core.logging import setup_logging
# Set up logging
setup_logging()
logger = logging.getLogger(__name__)
app = FastAPI(
title=settings.APP_NAME,
openapi_url=f"{settings.API_V1_STR}/openapi.json"
)
# Set up CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=settings.CORS_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include API router
app.include_router(api_router, prefix=settings.API_V1_STR)
@app.middleware("http")
async def db_session_middleware(request: Request, call_next):
request.state.db = SessionLocal()
response = await call_next(request)
request.state.db.close()
return response
@app.exception_handler(CustomException)
async def custom_exception_handler(request: Request, exc: CustomException):
return JSONResponse(
status_code=exc.status_code,
content={"message": exc.detail},
)
@app.on_event("startup")
async def startup_event():
logger.info("Starting up EthosNet application")
# Initialize services, connect to databases, etc.
# For example:
# init_db()
# init_gaia_nodes()
# init_ethereum_connection()
@app.on_event("shutdown")
async def shutdown_event():
logger.info("Shutting down EthosNet application")
# Clean up resources, close connections, etc.
@app.get("/")
async def root():
return {"message": "Welcome to EthosNet API"}
@app.get("/health")
async def health_check():
return {"status": "healthy"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(
"main:app",
host=settings.HOST,
port=settings.PORT,
reload=settings.DEBUG
)