Add the basics

This commit is contained in:
Scott Wallace 2021-12-20 15:09:30 +00:00
parent 260ce435b0
commit 4636acd05d
Signed by: scott
GPG key ID: AA742FDC5AFE2A72
11 changed files with 393 additions and 12 deletions

36
Dockerfile Normal file
View file

@ -0,0 +1,36 @@
# Build an intermediate image for the Python requirements
FROM python:3.9-slim-buster as intermediate
# Install Git and Python requirements
RUN apt update && apt install -y build-essential
COPY requirements.txt .
RUN python -m pip install --user -r requirements.txt
# Start from a fresh image
FROM python:3.9-slim-buster
# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1
# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1
RUN useradd -u 4000 -d /app appuser
WORKDIR /app
# Copy over the Python requirements from the intermediate container
COPY --from=intermediate /root/.local /app/.local
RUN chown -R appuser: /app
USER appuser
# Set the main execution command
ENTRYPOINT [".local/bin/waitress-serve", "main:app"]
# HEALTHCHECK --interval=5s --timeout=30s --start-period=5s --retries=3 CMD [ "/app/healthcheck.sh" ]
EXPOSE 8080/tcp
# Copy in the code
COPY slinky slinky/
COPY templates templates/
COPY main.py .

21
docker-compose.yaml Normal file
View file

@ -0,0 +1,21 @@
---
version: "3"
services:
slinky:
container_name: slinky
build:
context: .
image: slinky:latest
networks:
- traefik
labels:
- traefik.enable=true
- traefik.http.routers.slinky.entrypoints=web
- traefik.http.routers.slinky.service=slinky
- traefik.http.services.slinky.loadbalancer.server.port=8080
restart: unless-stopped
networks:
traefik:
external:
name: traefik_internal

31
main.py
View file

@ -1,16 +1,23 @@
import sys
"""
Main Flask-based app for Slinky
"""
from flask import Flask, render_template
from flask_bootstrap import Bootstrap
from flask.wrappers import Response
from slinky.web import slinky_webapp
app = Flask(__name__)
app.register_blueprint(slinky_webapp)
Bootstrap(app)
def url_home() -> Response:
...
@app.route('/')
def index() -> str:
"""
Index/Landing page
if __name__ == '__main__':
def main() -> int:
# Start Flask
return 0
sys.exit(main())
Returns:
str: string of page content
"""
return render_template('index.html')

View file

@ -1 +1,5 @@
flask
flask_bootstrap
flask_wtf
waitress
Flask-SQLAlchemy

View file

@ -1,7 +1,58 @@
"""
Main code
"""
import random
import string
from datetime import datetime
from typing import Optional
from slinky import db
def random_string(length: int = 4) -> str:
"""
Create a random, alphanumeric string of the length specified
Args:
length (int, optional): length of string to generate. Defaults to 4.
Returns:
str: random alphanumeric string
"""
allowed_chars: str = string.ascii_letters + string.digits
return ''.join(random.SystemRandom().choice(allowed_chars) for _ in range(length))
def add_shortcode(
url: str,
length: int = 4,
fixed_views: int = 0,
expiry: datetime = datetime.max,
) -> str:
"""
Add a shortcode to the DB
Args:
url (str): URL to redirect to
fixed_views (int, optional): number of views to serve before expiring.
Defaults to 0 (no limit).
expiry (int, optional): date of expiry. Defaults to 0 (no limit).
Returns:
str: shortcode for the redirect
"""
session = db.session()
shortcode = random_string(length=length)
dbentry = db.ShortURL(
shortcode=shortcode,
url=url,
fixed_views=fixed_views,
expiry=expiry,
)
session.add(dbentry)
session.commit()
session.close()
return shortcode

35
slinky/db.py Normal file
View file

@ -0,0 +1,35 @@
"""
DB component
"""
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session, sessionmaker
Base = declarative_base()
class ShortURL(Base): # pylint: disable=too-few-public-methods
"""
Class to describe the DB schema for ShortURLs
"""
__tablename__ = 'shorturl'
id = Column(Integer, primary_key=True)
shortcode = Column(String(128), unique=True, nullable=False)
url = Column(String(2048), unique=False, nullable=False)
fixed_views = Column(Integer, unique=False, nullable=False)
expiry = Column(Integer, unique=False, nullable=False)
def session() -> Session:
"""
Create a DB session
Returns:
Session: the DB session object
"""
engine = create_engine('sqlite:////tmp/test.db')
Base.metadata.create_all(engine)
new_session = sessionmaker(bind=engine)
return new_session()

39
slinky/templates/add.html Normal file
View file

@ -0,0 +1,39 @@
{% include '_head.html' %}
<!-- Begin page content -->
<main class="container">
<div class="container">
<h1 class="mt-5">Add a shortcode</h1>
</div>
<br />
<form action="/add" method="post">
{{ form.url.label }} {{ form.url }}<br />
{{ form.length.label }} {{ form.length }}<br />
{{ form.fixed_views.label }} {{ form.fixed_views }} (0 = unlimited)<br />
{{ form.expiry.label}} {{ form.expiry(class='datepicker') }} (leave as default for unlimited)<br />
<button id="submit" class="btn btn-primary" type="submit" onclick="waiting();" style="margin: 1em 0;">
Create shortcode
<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
</button>
</form>
<div id="content">
{% if shortcode -%}
<table class="table table-striped table-sm">
<thead>
<tr>
<th scope="col">Short URL</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{request.host_url}}{{ shortcode }}</td>
</tr>
</tbody>
</table>
{% endif -%}
</div>
</main>
{% include '_tail.html' %}

79
slinky/web.py Normal file
View file

@ -0,0 +1,79 @@
"""
Web component
"""
from datetime import datetime
from flask import Blueprint, render_template
from flask_wtf import FlaskForm
from wtforms import DateTimeLocalField, IntegerField, StringField
from wtforms.validators import DataRequired, Length
from slinky import add_shortcode
slinky_webapp = Blueprint('webapp', __name__, template_folder='templates')
class ShortURLForm(FlaskForm): # type: ignore[misc]
"""
Web form definition
"""
url = StringField(
'URL',
validators=[DataRequired(), Length(1, 2048)],
render_kw={
'size': 64,
'maxlength': 2048,
'placeholder': 'e.g. www.example.com',
},
)
fixed_views = IntegerField(
'Fixed number of views',
validators=[DataRequired()],
render_kw={
'size': 3,
'value': 0,
},
)
length = IntegerField(
'Shortcode length',
validators=[DataRequired()],
render_kw={
'size': 3,
'value': 4,
},
)
expiry = DateTimeLocalField(
'Expiry',
format='%Y-%m-%dT%H:%M',
render_kw={
'size': 8,
'maxlength': 10,
},
)
@slinky_webapp.route('/add', methods=['GET', 'POST'])
def add() -> str:
"""
Create and add a new shorturl
Returns:
str: shortcode for the URL
"""
shortcode = ''
url = ''
form = ShortURLForm(meta={'csrf': False})
if form.is_submitted():
url = form.url.data.strip()
length = form.length.data
fixed_views = form.fixed_views.data
expiry = form.expiry.data or datetime.max
if url:
shortcode = add_shortcode(url, length, fixed_views, expiry)
return render_template('add.html', form=form, shortcode=shortcode)

95
templates/_head.html Normal file
View file

@ -0,0 +1,95 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<title>Slinky</title>
<link rel="canonical" href="https://getbootstrap.com/docs/5.0/examples/navbar-static/">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
<link rel="stylesheet" href="{{url_for('bootstrap.static', filename='datepicker.css')}}">
<meta name="theme-color" content="#7952b3">
<style>
.bd-placeholder-img {
font-size: 1.125rem;
text-anchor: middle;
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
}
.badge a {
color: white;
text-decoration: none;
}
.badge.text-dark a {
color: black;
}
@media (min-width: 768px) {
.bd-placeholder-img-lg {
font-size: 3.5rem;
}
}
[role=status] {
display: none;
}
.tooltip>.tooltip-inner {
max-width: 60em !important;
text-align: left !important;
}
.highlight {
background-color: #f48024;
color: #1d1d1e;
border-radius: 2px;
-webkit-box-decoration-break: clone;
box-decoration-break: clone;
padding: 0 1px 0 1px;
}
.checkboxlist ul {
list-style-type: none;
margin: 0;
padding: 1em;
}
</style>
<!-- Custom styles for this template -->
<!-- <link href="navbar-top.css" rel="stylesheet"> -->
<script>
// <![CDATA[
function waiting() {
document.querySelectorAll('[role="status"]')[0].style.display = "inline-block";
}
// ]]>
</script>
</head>
<body>
<nav class="navbar navbar-expand-md navbar-dark bg-dark mb-4">
<div class="container-fluid">
<a class="navbar-brand" href="/">Slinky</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarCollapse"
aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav me-auto mb-2 mb-md-0">
<li class="nav-item">
<a class="nav-link" href="/add">Add</a>
</li>
</ul>
</div>
</div>
</nav>

12
templates/_tail.html Normal file
View file

@ -0,0 +1,12 @@
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous">
</script>
<script>
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl);
});
</script>
</body>
</html>

2
templates/index.html Normal file
View file

@ -0,0 +1,2 @@
{% include '_head.html' -%}
{% include '_tail.html' -%}