From d02ca243eb44409189abe534e6c369321abcbee7 Mon Sep 17 00:00:00 2001
From: CragglesG <121510295+CragglesG@users.noreply.github.com>
Date: Fri, 27 Sep 2024 17:01:12 +0100
Subject: [PATCH] Added HTML and server
---
.idea/.gitignore | 8 ++++
.idea/dataSources.xml | 12 +++++
.../inspectionProfiles/profiles_settings.xml | 6 +++
.idea/misc.xml | 7 +++
.idea/modules.xml | 8 ++++
.idea/url-shortener.iml | 10 +++++
.idea/vcs.xml | 6 +++
LICENSE | 2 +-
index.html | 26 +++++++++++
main.py | 45 +++++++++++++++++++
qodana.yaml | 29 ++++++++++++
submitted.html | 14 ++++++
test_main.http | 9 ++++
urls.json | 10 +++++
14 files changed, 191 insertions(+), 1 deletion(-)
create mode 100644 .idea/.gitignore
create mode 100644 .idea/dataSources.xml
create mode 100644 .idea/inspectionProfiles/profiles_settings.xml
create mode 100644 .idea/misc.xml
create mode 100644 .idea/modules.xml
create mode 100644 .idea/url-shortener.iml
create mode 100644 .idea/vcs.xml
create mode 100644 index.html
create mode 100644 main.py
create mode 100644 qodana.yaml
create mode 100644 submitted.html
create mode 100644 test_main.http
create mode 100644 urls.json
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