Improved structure
- Flask app factory instead of initializing `app` variable - Gunicorn for production server - Makefile: python3 -m - .gitignore: instance dir
This commit is contained in:
parent
058bcd13e5
commit
9ead8a457c
6 changed files with 30 additions and 18 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -2,6 +2,7 @@ __pycache__/
|
||||||
.mypy_cache/
|
.mypy_cache/
|
||||||
|
|
||||||
venv/
|
venv/
|
||||||
|
instance/
|
||||||
|
|
||||||
.vscode/
|
.vscode/
|
||||||
.idea/
|
.idea/
|
||||||
|
|
4
Makefile
4
Makefile
|
@ -1,8 +1,8 @@
|
||||||
dev:
|
dev:
|
||||||
FLASK_DEBUG="true" flask run
|
FLASK_DEBUG="true" python3 -m flask run
|
||||||
|
|
||||||
prod:
|
prod:
|
||||||
flask run
|
python3 -m gunicorn -w 4 "app:create_app()"
|
||||||
|
|
||||||
format:
|
format:
|
||||||
python3 -m autopep8 -r --in-place flaskapp/
|
python3 -m autopep8 -r --in-place flaskapp/
|
||||||
|
|
2
app.py
2
app.py
|
@ -1 +1 @@
|
||||||
from flaskapp.app import app
|
from flaskapp.app import create_app
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
"""Init file for the module"""
|
|
||||||
|
|
||||||
from .app import app
|
|
|
@ -1,20 +1,33 @@
|
||||||
"""Flask web application
|
"""Flask web application main script"""
|
||||||
main script"""
|
|
||||||
|
|
||||||
|
import os
|
||||||
|
import secrets
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
|
|
||||||
|
|
||||||
root = Path('..')
|
def create_app() -> Flask:
|
||||||
static = str(root / 'static')
|
"""Flask app factory function"""
|
||||||
tmpl = str(root / 'templates')
|
|
||||||
|
|
||||||
app = Flask(
|
root = Path('..')
|
||||||
'${REPO_NAME_SNAKE}',
|
static = str(root / 'static')
|
||||||
|
tmpl = str(root / 'templates')
|
||||||
|
|
||||||
|
app = Flask(
|
||||||
|
__name__,
|
||||||
static_folder=static,
|
static_folder=static,
|
||||||
template_folder=tmpl,
|
template_folder=tmpl,
|
||||||
)
|
instance_relative_config=True,
|
||||||
|
)
|
||||||
|
app.config['SECRET_KEY'] = os.getenv(
|
||||||
|
'SECRET_KEY',
|
||||||
|
secrets.token_hex(32),
|
||||||
|
)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
try:
|
||||||
app.run()
|
os.makedirs(app.instance_path)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return app
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
flask==2.2.2
|
flask==2.2.2
|
||||||
|
gunicorn==20.1.0
|
||||||
|
|
Loading…
Add table
Reference in a new issue