From edbf0233bad8bcc560495c78fa7d9f631581aff6 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Fri, 10 Mar 2023 17:17:23 +0400 Subject: [PATCH] Secret key multi-worker generating bugfix, volume instead of dir in mariadb, upd deps --- .env | 6 ++++++ app/common.py | 37 +++++++++++++++++++++++++++++-------- docker-compose.yml | 6 +++++- mypy.ini | 4 ---- pyproject.toml | 21 +++++++++++++++++++++ requirements.txt | 6 +++--- 6 files changed, 64 insertions(+), 16 deletions(-) delete mode 100644 mypy.ini create mode 100644 pyproject.toml diff --git a/.env b/.env index 6209581..cbec572 100644 --- a/.env +++ b/.env @@ -1,6 +1,12 @@ APP_HOST=0.0.0.0 APP_PORT=8000 +# Generate a strong secret key +# On Linux: openssl rand -hex 32 +# If this variable is not set, +# the key is generated automatically +#SECRET_KEY=secret + DB_HOST=${REPO_NAME_SNAKE}_db DB_PORT=3306 DB_USER=${REPO_NAME_SNAKE} diff --git a/app/common.py b/app/common.py index 6ebcc1a..cc8a9ad 100644 --- a/app/common.py +++ b/app/common.py @@ -7,26 +7,47 @@ from pydantic import BaseSettings # Directories file_dir = Path(__file__).parent -templates_dir = str( - file_dir.parent / 'templates' -) -static_dir = str( - file_dir.parent / 'static' -) +templates_dir = str(file_dir.parent / 'templates') +static_dir = str(file_dir.parent / 'static') # Main configuration class Settings(BaseSettings): debug: bool = False - secret_key: str = secrets.token_hex(32) + secret_key: str = 'secret' app_host: str = '127.0.0.1' app_port: int = 8000 +# Instantiate Settings class settings = Settings() - # Jinja templates handler templates = Jinja2Templates( directory=templates_dir, ) + + +def secret_key_check() -> None: + """Generates a secret key automatically + if the env var `secret_key` is not set + or contains text `secret`""" + + if settings.secret_key == 'secret': + + key_file = Path('/tmp/secret_key') + + if key_file.exists(): + with key_file.open('rt') as f: + secret_key = f.read() + + else: + secret_key = secrets.token_hex(32) + with key_file.open('wt') as f: + f.write(secret_key) + + settings.secret_key = secret_key + + +# Call the function +secret_key_check() diff --git a/docker-compose.yml b/docker-compose.yml index 8a90be8..051d19e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -20,10 +20,14 @@ services: container_name: ${REPO_NAME_SNAKE}_db restart: unless-stopped volumes: - - "./database:/var/lib/mysql" + #- "./database:/var/lib/mysql" + - "db_data:/var/lib/mysql" env_file: .env_db healthcheck: test: sh -c "mysqladmin ping -u$$$$MYSQL_USER -p$$$$MYSQL_PASSWORD" interval: 1s timeout: 3s retries: 20 + +volumes: + db_data: diff --git a/mypy.ini b/mypy.ini deleted file mode 100644 index 6f9d658..0000000 --- a/mypy.ini +++ /dev/null @@ -1,4 +0,0 @@ -[mypy] -show_error_codes = True -ignore_missing_imports = True -warn_redundant_casts = True diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..901443c --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,21 @@ +[project] +name = "app" +version = "1.0.0" +dependencies = [ + "fastapi", + "uvicorn[standard]", + "jinja2", + "starlette-wtf", + "sqlalchemy", + "sqlalchemy-utils", + "mysqlclient", + "python-dotenv", +] + +[tool.setuptools] +packages = [] + +[tool.mypy] +show_error_codes = true +ignore_missing_imports = true +warn_redundant_casts = true diff --git a/requirements.txt b/requirements.txt index 3b5e782..6517380 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,8 @@ fastapi~=0.93.0 -starlette~=0.25.0 -pydantic~=1.10.6 +starlette +pydantic -uvicorn[standard]~=0.20.0 +uvicorn[standard]~=0.21.0 gunicorn~=20.1.0 jinja2~=3.1.2