Added HTML and server

This commit is contained in:
CragglesG 2024-09-27 17:01:12 +01:00
parent 1d0b82436d
commit d02ca243eb
14 changed files with 191 additions and 1 deletions

8
.idea/.gitignore vendored Normal file
View file

@ -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

12
.idea/dataSources.xml Normal file
View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="0@localhost" uuid="c906b92f-eb81-435d-b6a1-cb159b395045">
<driver-ref>redis</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>jdbc.RedisDriver</jdbc-driver>
<jdbc-url>jdbc:redis://localhost:6379/0</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
</component>
</project>

View file

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

7
.idea/misc.xml Normal file
View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="Python 3.12 (url-shortener)" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (url-shortener)" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/url-shortener.iml" filepath="$PROJECT_DIR$/.idea/url-shortener.iml" />
</modules>
</component>
</project>

10
.idea/url-shortener.iml Normal file
View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View file

@ -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

26
index.html Normal file
View file

@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en-us">
<head>
<title>URL Shortener</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body>
<h1 class="p-2">URL Shortener</h1>
<p class="ps-2">Quickly shorten any URL!</p>
<form action="/form" method="POST">
<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">
<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">
<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>
</form>
</body>
</html>

45
main.py Normal file
View file

@ -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]}")

29
qodana.yaml Normal file
View file

@ -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: <SomeEnabledInspectionId>
#Disable inspections
#exclude:
# - name: <SomeDisabledInspectionId>
# paths:
# - <path/where/not/run/inspection>
#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> #(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

14
submitted.html Normal file
View file

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en-us">
<head>
<title>URL Shortener</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body>
<h1 class="p-2">URL Shortener</h1>
<p class="ps-2">Your URL has been shortened!</p>
<a href="/" class="p-2">Shorten another one!</a>
</body>
</html>

9
test_main.http Normal file
View file

@ -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

10
urls.json Normal file
View file

@ -0,0 +1,10 @@
{
"ohfudge": {
"bags": "soon"
},
"public": {
"hc": "https://hackclub.com",
"portfolio": "https://craigg.dev",
"url-shortener": "go.craigg.dev"
}
}