diff --git a/.idea/url-shortener.iml b/.idea/url-shortener.iml index 2c80e12..da3bad5 100644 --- a/.idea/url-shortener.iml +++ b/.idea/url-shortener.iml @@ -6,5 +6,6 @@ + \ No newline at end of file diff --git a/index.html b/index.html index fe0f146..2491b3c 100644 --- a/index.html +++ b/index.html @@ -9,15 +9,15 @@

URL Shortener

Quickly shorten any URL!

-
+
- +
This is added to the short URL. Keep it simple.
- +
This is the link that you want to redirect to.
diff --git a/main.py b/main.py index 234251e..0db382c 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,9 @@ -from fastapi import FastAPI, Response, HTTPException +from typing import Annotated + +from fastapi import FastAPI, Response, HTTPException, Form from fastapi.responses import RedirectResponse, HTMLResponse import json + app = FastAPI() with open("urls.json", "r") as f: @@ -11,9 +14,6 @@ async def root(): with open("index.html", "r") as file: return HTMLResponse(file.read()) -@app.get("/{tag}/{slug}") -async def tag_slug(tag: str, slug: str):# - return RedirectResponse(urls[tag][slug]) @app.get("/add/{slug}/{to}") async def add(slug: str, to: str): @@ -21,15 +21,9 @@ async def add(slug: str, to: str): await _save() return Response() -@app.get("/add/{tag}/{slug}/{to}") -async def add(tag: str, slug: str, to: str): - urls[tag][slug] = to - await _save() - return Response() - -@app.post("/form/") -async def form(slug: str, link: str): - await add(slug, link) +@app.post("/form") +async def form(slug: Annotated[str, Form()], link: Annotated[str, Form()]): + await add(slug=slug, to=link) with open("submitted.html", "r") as file: return HTMLResponse(file.read()) @app.get("/_save") @@ -37,9 +31,9 @@ async def _save(): with open("urls.json", "w") as file: json.dump(urls, file, indent=2, sort_keys=True) -@app.get("/{slug:path}") +@app.get("/{slug}") async def slug_only(slug: str): if slug == "favicon.ico": return HTTPException(404) else: - return RedirectResponse(f"https://{urls["public"][slug]}") \ No newline at end of file + return RedirectResponse(urls["public"][slug]) \ No newline at end of file diff --git a/urls.json b/urls.json index 0196155..7ab911a 100644 --- a/urls.json +++ b/urls.json @@ -5,6 +5,6 @@ "public": { "hc": "https://hackclub.com", "portfolio": "https://craigg.dev", - "url-shortener": "go.craigg.dev" + "url-shortener": "https://go.craigg.dev" } } \ No newline at end of file