The backend component to interface with the smart contract.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 rivejä
1.0 KiB

  1. from contextlib import asynccontextmanager
  2. from fastapi import FastAPI
  3. from fastapi.middleware.cors import CORSMiddleware
  4. from app.core.config import settings
  5. from app.api.v1.api import api_router
  6. from app.db.base import engine, Base
  7. async def init_db():
  8. async with engine.begin() as conn:
  9. await conn.run_sync(Base.metadata.create_all)
  10. @asynccontextmanager
  11. async def lifespan(application: FastAPI):
  12. await init_db()
  13. yield
  14. app = FastAPI(
  15. title=settings.PROJECT_NAME,
  16. version=settings.VERSION,
  17. description=settings.DESCRIPTION,
  18. openapi_url=f"{settings.API_V1_STR}/openapi.json",
  19. lifespan=lifespan,
  20. )
  21. # Set up CORS middleware
  22. app.add_middleware(
  23. CORSMiddleware,
  24. allow_origins=settings.ALLOWED_ORIGINS,
  25. allow_credentials=True,
  26. allow_methods=["*"],
  27. allow_headers=["*"],
  28. )
  29. # Include API router
  30. app.include_router(router=api_router, prefix=settings.API_V1_STR)
  31. if __name__ == "__main__":
  32. import uvicorn
  33. uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True)