-
Notifications
You must be signed in to change notification settings - Fork 1
/
route.py
42 lines (36 loc) · 1.5 KB
/
route.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
from fastapi import APIRouter, Request, HTTPException
from fastapi.responses import RedirectResponse
from config.database import collection_name
from models.models import UrlMappingModel
from schema.schema import UrlMappingSchema
from utils.utils import create_short_url, DNS as dns
from config.cache import cached_obj as cache
router = APIRouter()
db = UrlMappingModel(collection_name, cache)
@router.get("/")
async def ping():
return {"ping": "pong"}
@router.post("/longurl")
async def shorten_url(url_mapping: UrlMappingSchema):
long_url = url_mapping.long_url
print("long_url", long_url)
short_url = db.get_short_url(long_url)
if not short_url:
short_url = create_short_url()
# if the short URL already in DB, keep creating new ones
while db.get_long_url(short_url):
short_url = create_short_url()
print("new short= ", short_url)
db.insert_url_mapping(short_url, long_url)
short_url = f"http://{dns}/{short_url}"
return {"shortenedUrl": short_url}
@router.get("/{short_url}")
async def redirect_to_long_url(short_url: str):
long_url = db.get_long_url(short_url)
print("for short_url", short_url, "Redirect to long url", long_url)
if long_url:
# cache.set(short_url, long_url, time=3600) # Cache for 1 hour
return RedirectResponse(url=long_url, status_code=301)
# return RedirectResponse(url=long_url, status_code=307)
else:
raise HTTPException(status_code=404, detail="Short URL not found")