diff --git a/.gitignore b/.gitignore index ecc3336..6e43a82 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ __pycache__/ .mypy_cache/ venv/ +instance/ .vscode/ .idea/ diff --git a/Makefile b/Makefile index 8788c91..d505860 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ dev: - FLASK_DEBUG="true" flask run + FLASK_DEBUG="true" python3 -m flask run prod: - flask run + python3 -m gunicorn -w 4 "app:create_app()" format: python3 -m autopep8 -r --in-place flaskapp/ diff --git a/app.py b/app.py index 338aac7..3847282 100644 --- a/app.py +++ b/app.py @@ -1 +1 @@ -from flaskapp.app import app +from flaskapp.app import create_app diff --git a/flaskapp/__init__.py b/flaskapp/__init__.py index 3d5eb81..e69de29 100644 --- a/flaskapp/__init__.py +++ b/flaskapp/__init__.py @@ -1,3 +0,0 @@ -"""Init file for the module""" - -from .app import app diff --git a/flaskapp/app.py b/flaskapp/app.py index ffa173c..ca9d819 100644 --- a/flaskapp/app.py +++ b/flaskapp/app.py @@ -1,20 +1,33 @@ -"""Flask web application -main script""" +"""Flask web application main script""" +import os +import secrets from pathlib import Path from flask import Flask -root = Path('..') -static = str(root / 'static') -tmpl = str(root / 'templates') +def create_app() -> Flask: + """Flask app factory function""" -app = Flask( - '${REPO_NAME_SNAKE}', - static_folder=static, - template_folder=tmpl, -) + root = Path('..') + static = str(root / 'static') + tmpl = str(root / 'templates') -if __name__ == '__main__': - app.run() + app = Flask( + __name__, + static_folder=static, + template_folder=tmpl, + instance_relative_config=True, + ) + app.config['SECRET_KEY'] = os.getenv( + 'SECRET_KEY', + secrets.token_hex(32), + ) + + try: + os.makedirs(app.instance_path) + except OSError: + pass + + return app diff --git a/requirements.txt b/requirements.txt index f87e031..72e3f57 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ flask==2.2.2 +gunicorn==20.1.0