-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
65 lines (50 loc) · 1.24 KB
/
main.py
File metadata and controls
65 lines (50 loc) · 1.24 KB
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
from fastapi import FastAPI
from api.views import router as api_router
from tortoise import Tortoise
from tortoise.contrib.fastapi import register_tortoise
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI(title="Garuda")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
TORTOISE_CONFIG = {
"connections": {
"read_write": "postgres://root:root@localhost:5432/garuda",
},
"apps": {
"garuda": {
"models": [
"api.models",
],
"default_connection": "read_write",
},
},
}
# startup tasks
@app.on_event("startup")
async def init_db() -> None:
"""
Initializes database with Tortoise ORM
"""
register_tortoise(
app,
config=TORTOISE_CONFIG,
generate_schemas=True,
add_exception_handlers=True,
)
# shutdown tasks
@app.on_event("shutdown")
async def close_process() -> None:
"""
Activities to perform on server shut-down
- Close Tortoise DB Connection
"""
await Tortoise.close_connections
@app.get("/ping")
async def root():
return {"message": "pong"}
app.include_router(api_router)