initial commit, adding flask structure, README, routes

- routes defined in routes.py uses the <path:..> function to catch
  every non-empty url comming along
- request.full_path is used to get the entire url, as <path:> would
  remove eg GET '?' from the url, which we don't want
  for the link forwarding.
- request.full_path[1:] removes the '/' from the url
This commit is contained in:
2021-08-12 11:45:31 +02:00
commit 85850a9d63
13 changed files with 159 additions and 0 deletions

8
app/__init__.py Normal file
View File

@ -0,0 +1,8 @@
from flask import Flask
app = Flask(__name__,
static_folder='static',
template_folder='template')
from app import routes

42
app/routes.py Normal file
View File

@ -0,0 +1,42 @@
from flask import render_template,redirect,request, jsonify
from app import app
import os, json
import random, string
app.secret_key = os.environ.get("SECRET_KEY") or os.urandom(24)
app.view_functions['static']
SHORT_HOST = 'short.maenle.tech'
@app.route("/to/<path:short>", host=SHORT_HOST)
def short(short):
f = open('app/static/res/short.json', 'r')
data = json.load(f)
for s in data:
if s['short'] == short:
return redirect(s['path'], code=302)
return jsonify(success=False)
@app.route("/<path:path>", host=SHORT_HOST)
def long(path):
full_path = request.full_path[1:]
f = open('app/static/res/short.json', 'r')
data = json.load(f)
for s in data:
if s['path'] == full_path:
return jsonify(path="short.maenle.tech/to/"+s['short'])
letters = string.ascii_lowercase
short = ''.join(random.choice(letters) for i in range(12))
data.append({'path': full_path, 'short': short})
f = open('app/static/res/short.json', 'w')
json.dump(data, f)
f.close()
template = render_template("short.html", path="short.maenle.tech/to/"+short)
return template

10
app/static/css/style.css Normal file
View File

@ -0,0 +1,10 @@
html
{
font-family: Segoe UI, Frutiger, sans-serif;
width: 100%; height: 100%
}
body {
margin: 0px;
}

BIN
app/static/res/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

@ -0,0 +1 @@
[]

18
app/template/short.html Normal file
View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8", content="width=device-width", name="viewport"/>
<title>Link Shortening</title>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/style.css') }}">
<link rel="shortcut icon" type="image/jpg" href="{{ url_for('static', filename='res/favicon.ico') }}">
</head>
<body>
<h2>
Your Link:
</h2>
{{ path }}
</body>
</html>