Compare commits

..

No commits in common. "c7da7e32b71f345f6786decae4d0d52be547ff11" and "058bcd13e5348ad05180884fb2b6afe1f2e78ca5" have entirely different histories.

14 changed files with 19 additions and 192 deletions

1
.gitignore vendored
View file

@ -2,7 +2,6 @@ __pycache__/
.mypy_cache/ .mypy_cache/
venv/ venv/
instance/
.vscode/ .vscode/
.idea/ .idea/

View file

@ -1,8 +1,8 @@
dev: dev:
FLASK_DEBUG="true" python3 -m flask run FLASK_DEBUG="true" flask run
prod: prod:
python3 -m gunicorn -w 4 "app:create_app()" flask run
format: format:
python3 -m autopep8 -r --in-place flaskapp/ python3 -m autopep8 -r --in-place flaskapp/

2
app.py
View file

@ -1 +1 @@
from flaskapp.app import create_app from flaskapp.app import app

View file

@ -0,0 +1,3 @@
"""Init file for the module"""
from .app import app

View file

@ -1,39 +1,20 @@
"""Flask web application main script""" """Flask web application
main script"""
import os from pathlib import Path
import secrets
from flask import Flask from flask import Flask
from . import routes
from . import errors
root = Path('..')
static = str(root / 'static')
tmpl = str(root / 'templates')
def create_app() -> Flask:
"""Flask app factory function"""
# Create an app object
app = Flask( app = Flask(
__name__, '${REPO_NAME_SNAKE}',
static_folder='../static', static_folder=static,
template_folder='../templates', template_folder=tmpl,
instance_relative_config=True,
)
# Get the token from environment
# or generate it using secrets
app.config['SECRET_KEY'] = os.getenv(
'SECRET_KEY',
secrets.token_hex(32),
) )
# Create instance/ directory if __name__ == '__main__':
try: app.run()
os.makedirs(app.instance_path)
except OSError:
pass
# Add routes
routes.add_routes(app)
errors.add_routes(app)
return app

View file

@ -1,48 +0,0 @@
"""Flask app error handlers"""
from pathlib import Path
from flask import Flask
from flask import render_template
# Add other HTTP error codes here
CODES = [404, 500]
def add_routes(app: Flask) -> None:
"""Add all error handlers
Args:
app (Flask): Flask application
"""
tmpl_dir = app.template_folder
if tmpl_dir is None:
return
tmpl = Path(__file__).parent / tmpl_dir
for code in CODES:
add_handler(app, tmpl, code)
def add_handler(
app: Flask,
tmpl: Path,
code: int) -> None:
"""Add Flask app error handler.
Only for internal use
Args:
app (Flask): Flask application
file (str): Template filename
code (int): Error code
"""
file = f'{code}.html'
if (tmpl / file).exists():
@app.errorhandler(code)
def handler(_e):
return render_template(file), code

View file

@ -1,16 +0,0 @@
"""Main Flask app routes"""
from flask import Flask
from flask import render_template
def add_routes(app: Flask) -> None:
"""Add main routes
Args:
app (Flask): Flask application
"""
@app.route('/')
def index():
return render_template('index.html')

View file

@ -1,2 +1 @@
flask==2.2.2 flask==2.2.2
gunicorn==20.1.0

View file

@ -1,31 +0,0 @@
body {
height: 100vh;
padding: 0;
margin: 0;
font-family: sans-serif;
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
background: #fff;
color: #000;
}
@media (prefers-color-scheme: dark) {
body {
background: #202023;
color: #eee;
}
}
header { margin-top: 5px; }
footer { margin-bottom: 5px; }
a {
color: #5b8a55;
}
a:hover {
filter: brightness(120%);
}

View file

@ -1,4 +0,0 @@
addEventListener('load', () => {
document.getElementById('js')
.innerText = new Date().toLocaleString()
})

View file

@ -1,11 +0,0 @@
{% extends "base.html" %}
{% block title %}404{% endblock %}
{% block content %}
<h1>404: Not Found</h1>
<p>
Go to the
<a href="/">main page</a>
</p>
{% endblock %}

View file

@ -1,15 +0,0 @@
{% extends "base.html" %}
{% block title %}500{% endblock %}
{% block content %}
<h1>500: ISE</h1>
<p>
An error occured while
processing your request.
</p>
<p>
Please, try again later
or contact web site admin.
</p>
{% endblock %}

View file

@ -1,18 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="${REPO_DESCRIPTION}">
<title>{% block title %}{% endblock %} | ${REPO_NAME}</title>
<link rel="stylesheet" href="/static/css/style.css">
<script src="/static/js/script.js"></script>
</head>
<body>
<header>${REPO_NAME}</header>
<article>
{% block content %}{% endblock %}
</article>
<footer id="js"></footer>
</body>
</html>

View file

@ -1,12 +0,0 @@
{% extends "base.html" %}
{% block title %}Main page{% endblock %}
{% block content %}
<h1>
This is the default main page of
<a href="https://git.dc09.ru/DarkCat09/tmpl-flask">
Flask app template
</a>
</h1>
{% endblock %}