-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
75 lines (61 loc) · 1.55 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from fastapi import FastAPI, HTTPException, Request
from sqlalchemy import text, CursorResult
from starlette.responses import JSONResponse
from apis.posts_apis import posts_router
from apis.user_apis import users_router
from config.database import database, async_session
# Create Web APP FastAPI
app: FastAPI = FastAPI(
title="Fast API MVC"
)
@app.exception_handler(HTTPException)
async def custom_http_exception_handler(
request: Request,
exc: HTTPException
) -> JSONResponse:
"""
Custom HTTPException Response
"""
return JSONResponse(
status_code=exc.status_code,
content={
"error": exc.detail
}
)
@app.on_event("startup")
async def startup():
"""
Connect To DataBase
"""
await database.connect()
@app.on_event("shutdown")
async def shutdown():
"""
Disconnect To DataBase
"""
await database.disconnect()
@app.get("/app-status")
async def app_status():
"""
Just APP Status
"""
return {
"status": "success"
}
@app.get("/db-status")
async def db_status():
"""
Just for DB Status of Checking Successful Working with DB
"""
async with async_session() as session:
# Perform a Simple Query to Check Database Connection
result: CursorResult = await session.execute(text("SELECT 1"))
return JSONResponse(
{
"result": result.scalar()
},
status_code=200
)
# Register Routers
app.include_router(users_router)
app.include_router(posts_router)