-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
97 lines (74 loc) · 2.62 KB
/
app.py
File metadata and controls
97 lines (74 loc) · 2.62 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from endpoints import HomeEndpoint
from fastapi import FastAPI
from fastapi.responses import JSONResponse, RedirectResponse
from piccolo_admin.endpoints import create_admin
from piccolo_api.crud.serializers import create_pydantic_model
from piccolo_app import APP_CONFIG
from piccolo.engine import engine_finder
from starlette.routing import Route, Mount
from starlette.staticfiles import StaticFiles
from tables import URL
from typing import Dict, List
app = FastAPI(
routes=[
Route("/", HomeEndpoint),
Mount(
"/admin/",
create_admin(
tables=APP_CONFIG.table_classes,
# Required when running under HTTPS:
# allowed_hosts=['my_site.com']
),
),
Mount("/static/", StaticFiles(directory="static")),
],
)
URLModelIn: Dict = create_pydantic_model(table=URL, model_name="URLModelIn")
URLModelOut: Dict = create_pydantic_model(
table=URL, include_default_columns=True, model_name="URLModelOut"
)
@app.get("/urls/", response_model=List[URLModelOut])
async def urls():
return await URL.select().order_by(URL.id)
@app.get("/{url_key}")
async def forward_to_target_url(url_key: str):
from crud import get_url_by_key
if (db_url := await get_url_by_key(url_key)) is not None:
return RedirectResponse(db_url)
return JSONResponse({}, status_code=404)
@app.post("/urls/", response_model=URLModelOut)
async def create_url(url_model: URLModelIn):
from crud import create_db_url
url = URL(**(await create_db_url(url_model)))
await url.save()
return url.to_dict()
@app.put("/urls/{url_id}/", response_model=URLModelOut)
async def update_url(url_id: int, url_model: URLModelIn):
url = await URL.objects().get(URL.id == url_id)
if not url:
return JSONResponse({}, status_code=404)
for key, value in url_model.dict().items():
setattr(url, key, value)
await url.save()
return url.to_dict()
@app.delete("/urls/{url_id}/")
async def delete_url(url_id: int):
url = await URL.objects().get(URL.id == url_id)
if not url:
return JSONResponse({}, status_code=404)
await url.remove()
return JSONResponse({})
@app.on_event("startup")
async def open_database_connection_pool():
try:
engine = engine_finder()
await engine.start_connection_pool()
except Exception:
print("Unable to connect to the database")
@app.on_event("shutdown")
async def close_database_connection_pool():
try:
engine = engine_finder()
await engine.close_connection_pool()
except Exception:
print("Unable to connect to the database")