Skip to content

Commit 7321483

Browse files
committed
feat: update main.py with enhanced settings and configurations
1 parent 40b5f75 commit 7321483

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

main.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,48 @@
44
from fastapi import FastAPI
55
from fastapi.middleware.cors import CORSMiddleware
66
from starlette.requests import Request
7+
from starlette.responses import JSONResponse
78

89
from api.db.database import Base, engine
910
from api.utils.logging_config import setup_logging
1011
from api.v1.routes import api_version_one
1112

13+
# Create all database tables
1214
Base.metadata.create_all(bind=engine)
1315

1416

1517
@asynccontextmanager
1618
async def lifespan(app: FastAPI):
19+
# Setup tasks before starting the application
20+
# e.g., connecting to databases, initializing resources
1721
yield
22+
# Cleanup tasks before shutting down the application
23+
# e.g., closing database connections, releasing resources
1824

1925

2026
# Setup logging configuration
2127
setup_logging()
2228

23-
app = FastAPI(lifespan=lifespan)
29+
app = FastAPI(
30+
title="FastAPI Web",
31+
description="This is the API documentation for the FastAPI Web Project.",
32+
version="0.1.0",
33+
lifespan=lifespan,
34+
contact={
35+
"name": "Seyi Pythonian",
36+
"url": "https://www.github.com/Pythonian/fastapi_web",
37+
"email": "seyipythonian@gmail.com",
38+
},
39+
license_info={
40+
"name": "MIT License",
41+
"url": "https://opensource.org/licenses/MIT",
42+
},
43+
docs_url="/docs",
44+
redoc_url="/redoc",
45+
openapi_url="/openapi.json",
46+
)
2447

48+
# CORS settings
2549
origins = [
2650
"http://localhost:3000",
2751
"http://localhost:3001",
@@ -35,16 +59,20 @@ async def lifespan(app: FastAPI):
3559
allow_headers=["*"],
3660
)
3761

62+
# Include the API version 1 router
3863
app.include_router(api_version_one)
3964

4065

41-
@app.get("/", tags=["Home"])
42-
async def get_root(request: Request) -> dict:
43-
return {
44-
"message": "Welcome to API",
45-
"URL": "",
46-
}
66+
# Root endpoint
67+
@app.get("/", tags=["Home"], response_class=JSONResponse)
68+
async def get_root(request: Request) -> JSONResponse:
69+
return JSONResponse(
70+
{
71+
"message": "Welcome to My API",
72+
"URL": request.url._url,
73+
}
74+
)
4775

4876

4977
if __name__ == "__main__":
50-
uvicorn.run("main:app", port=8000, reload=True)
78+
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)

0 commit comments

Comments
 (0)