diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..13566b8
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml
new file mode 100644
index 0000000..4a3782d
--- /dev/null
+++ b/.idea/dataSources.xml
@@ -0,0 +1,12 @@
+
+
Quickly shorten any URL!
+ + + \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..234251e --- /dev/null +++ b/main.py @@ -0,0 +1,45 @@ +from fastapi import FastAPI, Response, HTTPException +from fastapi.responses import RedirectResponse, HTMLResponse +import json +app = FastAPI() + +with open("urls.json", "r") as f: + urls = json.load(f) + +@app.get("/") +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): + urls["public"][slug] = to + 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) + with open("submitted.html", "r") as file: + return HTMLResponse(file.read()) +@app.get("/_save") +async def _save(): + with open("urls.json", "w") as file: + json.dump(urls, file, indent=2, sort_keys=True) + +@app.get("/{slug:path}") +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 diff --git a/qodana.yaml b/qodana.yaml new file mode 100644 index 0000000..84e3e49 --- /dev/null +++ b/qodana.yaml @@ -0,0 +1,29 @@ +#-------------------------------------------------------------------------------# +# Qodana analysis is configured by qodana.yaml file # +# https://www.jetbrains.com/help/qodana/qodana-yaml.html # +#-------------------------------------------------------------------------------# +version: "1.0" + +#Specify inspection profile for code analysis +profile: + name: qodana.starter + +#Enable inspections +#include: +# - name:Your URL has been shortened!
+ Shorten another one! + + \ No newline at end of file diff --git a/test_main.http b/test_main.http new file mode 100644 index 0000000..8ff7270 --- /dev/null +++ b/test_main.http @@ -0,0 +1,9 @@ +# Test your FastAPI endpoints + +GET http://127.0.0.1:8000/ +Accept: text/html + +### + +GET http://127.0.0.1:8000/_save +Accept: application/json diff --git a/urls.json b/urls.json new file mode 100644 index 0000000..0196155 --- /dev/null +++ b/urls.json @@ -0,0 +1,10 @@ +{ + "ohfudge": { + "bags": "soon" + }, + "public": { + "hc": "https://hackclub.com", + "portfolio": "https://craigg.dev", + "url-shortener": "go.craigg.dev" + } +} \ No newline at end of file