Add .env-example file and update configuration for environment variables
- Created .env-example for environment variable setup - Updated README to reflect changes in .env file creation - Modified config.py to include new environment variables and DATABASE_URL property - Enhanced database.py with connect and disconnect functions - Updated main.py to manage database connection lifecycle - Adjusted compose.yaml for consistent environment variable usage
This commit is contained in:
11
app/main.py
11
app/main.py
@ -1,14 +1,15 @@
|
||||
from contextlib import asynccontextmanager
|
||||
from fastapi import FastAPI
|
||||
from app.database import engine
|
||||
from app.database import connect, disconnect
|
||||
from app.routers import authors, books
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
yield # tables already created by Create.sql
|
||||
await engine.dispose()
|
||||
await connect()
|
||||
yield
|
||||
await disconnect()
|
||||
|
||||
app = FastAPI(title="Database API", lifespan=lifespan)
|
||||
|
||||
app.include_router(authors.router)
|
||||
app.include_router(books.router)
|
||||
for router in [authors.router, books.router]:
|
||||
app.include_router(router)
|
||||
|
||||
Reference in New Issue
Block a user