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 @@ + + + + + redis + true + jdbc.RedisDriver + jdbc:redis://localhost:6379/0 + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..7c0f5e3 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..f678fc4 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/url-shortener.iml b/.idea/url-shortener.iml new file mode 100644 index 0000000..2c80e12 --- /dev/null +++ b/.idea/url-shortener.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/LICENSE b/LICENSE index e09e5f5..42f2c03 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2024 Craig +Copyright (c) 2024 Craig Giles Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/index.html b/index.html new file mode 100644 index 0000000..fe0f146 --- /dev/null +++ b/index.html @@ -0,0 +1,26 @@ + + + + URL Shortener + + + + + +

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.
+
+ +
+ + \ 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: + +#Disable inspections +#exclude: +# - name: +# paths: +# - + +#Execute shell command before Qodana execution (Applied in CI/CD pipeline) +#bootstrap: sh ./prepare-qodana.sh + +#Install IDE plugins before Qodana execution (Applied in CI/CD pipeline) +#plugins: +# - id: #(plugin id can be found at https://plugins.jetbrains.com) + +#Specify Qodana linter for analysis (Applied in CI/CD pipeline) +linter: jetbrains/qodana-python:latest diff --git a/submitted.html b/submitted.html new file mode 100644 index 0000000..4b4ce45 --- /dev/null +++ b/submitted.html @@ -0,0 +1,14 @@ + + + + URL Shortener + + + + + +

URL Shortener

+

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