mirror of
https://github.com/CragglesG/url-shortener.git
synced 2024-11-23 05:23:40 +00:00
Fixed all bugs
This commit is contained in:
parent
d02ca243eb
commit
acc583b24e
4 changed files with 14 additions and 19 deletions
|
@ -6,5 +6,6 @@
|
|||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="bootstrap" level="application" />
|
||||
</component>
|
||||
</module>
|
|
@ -9,15 +9,15 @@
|
|||
<body>
|
||||
<h1 class="p-2">URL Shortener</h1>
|
||||
<p class="ps-2">Quickly shorten any URL!</p>
|
||||
<form action="/form" method="POST">
|
||||
<form action="/form" method="POST" enctype="application/x-www-form-urlencoded">
|
||||
<div class="mb-3 p-2">
|
||||
<label for="slug" class="form-label">Slug</label>
|
||||
<input type="text" class="form-control" id="slug" aria-describedby="emailHelp">
|
||||
<input type="text" class="form-control" id="slug" name="slug">
|
||||
<div id="slugHelp" class="form-text">This is added to the short URL. Keep it simple.</div>
|
||||
</div>
|
||||
<div class="mb-3 p-2">
|
||||
<label for="link" class="form-label">Link</label>
|
||||
<input type="text" class="form-control" id="link">
|
||||
<input type="url" class="form-control" id="link" name="link">
|
||||
<div id="linkHelp" class="form-text">This is the link that you want to redirect to.</div>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-success" style="margin-left:.5rem;">Submit</button>
|
||||
|
|
24
main.py
24
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]}")
|
||||
return RedirectResponse(urls["public"][slug])
|
|
@ -5,6 +5,6 @@
|
|||
"public": {
|
||||
"hc": "https://hackclub.com",
|
||||
"portfolio": "https://craigg.dev",
|
||||
"url-shortener": "go.craigg.dev"
|
||||
"url-shortener": "https://go.craigg.dev"
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue