27 lines
776 B
Python
27 lines
776 B
Python
from fastapi import FastAPI
|
|
from .models import UrlPayload
|
|
from .utils import generate_shortcode
|
|
|
|
app = FastAPI()
|
|
|
|
# temporary data store
|
|
url_mapping = {}
|
|
|
|
@app.get("/{shortcode}")
|
|
async def get_original_url(shortcode: str):
|
|
if shortcode in url_mapping:
|
|
return {"original_url": url_mapping[shortcode]}
|
|
else:
|
|
raise HTTPException(status_code=404, detail="Shortcode not found")
|
|
|
|
|
|
@app.post("/shorten/")
|
|
async def create_short_url(url_payload: UrlPayload):
|
|
original_url = url_payload.url
|
|
shortcode = generate_shortcode()
|
|
|
|
if shortcode in url_mapping:
|
|
raise HTTPException(status_code=409, detail="Shortcode already in use")
|
|
|
|
url_mapping[shortcode] = str(original_url)
|
|
return {"shortcode": shortcode, "original_url": original_url} |