forked from pocketzworld/url-shortener-tech-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
38 lines (27 loc) · 900 Bytes
/
server.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
from fastapi import FastAPI
from fastapi.responses import RedirectResponse
from pydantic import BaseModel
app = FastAPI()
BASE_URL: str = "http://locahost:8000"
class ShortenRequest(BaseModel):
url: str
@app.post("/url/shorten")
async def url_shorten(request: ShortenRequest):
"""
Given a URL, generate a short version of the URL that can be later resolved to the originally
specified URL.
"""
short_url = "new-short-url"
return {"short_url": f"{BASE_URL}/r/{short_url}"}
class ResolveRequest(BaseModel):
short_url: str
@app.get("/r/{short_url}")
async def url_resolve(short_url: str):
"""
Return a redirect response for a valid shortened URL string.
If the short URL is unknown, return an HTTP 404 response.
"""
return RedirectResponse("http://original/url")
@app.get("/")
async def index():
return "Your URL Shortener is running!"