-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
36 lines (26 loc) · 851 Bytes
/
main.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
from fastapi import FastAPI, Form, HTTPException
from fastapi.responses import FileResponse, RedirectResponse, HTMLResponse
import uvicorn
import db
import os
domain = os.environ['domain']
app = FastAPI()
db.initializeDB()
@app.get("/")
async def serve_root():
return FileResponse("./static/index.html")
@app.post("/short")
async def post_short(url: str = Form(...)):
shorturl = db.shortUrl(url)
return HTMLResponse(
f'<a href="https://{domain}/{shorturl}" target="_blank">{domain}/{shorturl}</a>'
)
@app.get("/{shorturl}")
async def redirect(shorturl: str):
longurl = db.retriveURL(shorturl)
print(longurl)
if longurl: return RedirectResponse(url=longurl)
else:
raise HTTPException(status_code=404, detail="URL not found")
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000, log_level="info")