From 71964355b7d630ad992840a3effe6f46d964baa1 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Fri, 3 Mar 2023 18:10:17 +0400 Subject: [PATCH] Bugfixes in docker-compose and DB, gunicorn for prod, env vars for app host and port --- .env | 3 +++ .env_db | 2 +- .env_debug | 5 +++++ Makefile | 15 +++++++++++++-- app/common.py | 2 ++ docker-compose.yml | 13 ++++++++++++- main.py | 7 ++++++- requirements.txt | 1 + 8 files changed, 43 insertions(+), 5 deletions(-) diff --git a/.env b/.env index dfb73ea..f815eb6 100644 --- a/.env +++ b/.env @@ -1,3 +1,6 @@ +APP_HOST=0.0.0.0 +APP_PORT=8000 + DB_HOST=${REPO_NAME_SNAKE}_db DB_PORT=3306 DB_USER=${REPO_NAME_SNAKE} diff --git a/.env_db b/.env_db index 40ffad0..bcaf662 100644 --- a/.env_db +++ b/.env_db @@ -1,5 +1,5 @@ -MYSQL_HOST=${REPO_NAME_SNAKE}_db MYSQL_PORT=3306 +MYSQL_RANDOM_ROOT_PASSWORD=true MYSQL_USER=${REPO_NAME_SNAKE} MYSQL_PASSWORD= MYSQL_DATABASE=${REPO_NAME_SNAKE} diff --git a/.env_debug b/.env_debug index 14fc4ff..2aec99c 100644 --- a/.env_debug +++ b/.env_debug @@ -1,6 +1,11 @@ DEBUG=true + SESSION_KEY=debug CSRF_KEY=debug + +APP_HOST=127.0.0.1 +APP_PORT=8000 + DB_HOST=localhost DB_PORT=3306 DB_USER=darkcat09 diff --git a/Makefile b/Makefile index 64abc52..5561480 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,14 @@ +APP_HOST ?= 0.0.0.0 +APP_PORT ?= 8000 + dev: python3 -m dotenv -f .env_debug run \ - python3 -m uvicorn main:app --reload + make cmd-dev prod: - python3 -m uvicorn main:app + python3 -m gunicorn \ + -w 4 -k uvicorn.workers.UvicornWorker \ + -b $${APP_HOST}:$${APP_PORT} main:app format: python3 -m autopep8 -r --in-place app/ @@ -30,6 +35,12 @@ clean: rm -rf __pycache__ rm -rf .mypy_cache +cmd-dev: + python3 -m uvicorn main:app \ + --reload \ + --host $${APP_HOST} \ + --port $${APP_PORT} + cmd-docker-build: docker build \ -t ${REPO_OWNER_LOWER}/${REPO_NAME_SNAKE}:latest \ diff --git a/app/common.py b/app/common.py index 97010d2..d440dcb 100644 --- a/app/common.py +++ b/app/common.py @@ -20,6 +20,8 @@ class Settings(BaseSettings): debug: bool = False session_key: str = secrets.token_hex(32) csrf_key: str = secrets.token_hex(32) + app_host: str = '127.0.0.1' + app_port: int = 8000 settings = Settings() diff --git a/docker-compose.yml b/docker-compose.yml index ed99f8a..20f5392 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,6 +1,7 @@ version: "3" services: + ${REPO_NAME_SNAKE}: image: ${REPO_OWNER_LOWER}/${REPO_NAME_SNAKE}:latest container_name: ${REPO_NAME_SNAKE} @@ -10,10 +11,20 @@ services: links: - ${REPO_NAME_SNAKE}_db env_file: .env + depends_on: + ${REPO_NAME_SNAKE}_db: + condition: service_healthy + ${REPO_NAME_SNAKE}_db: image: mariadb:latest container_name: ${REPO_NAME_SNAKE}_db restart: unless-stopped volumes: - "./database:/var/lib/mysql" - env_file: .env + env_file: .env_db + healthcheck: + test: sh -c "mysqladmin ping -u$$$$MYSQL_USER -p$$$$MYSQL_PASSWORD" + interval: 1s + timeout: 3s + retries: 20 + start_period: 10s diff --git a/main.py b/main.py index 664c498..b70ba18 100755 --- a/main.py +++ b/main.py @@ -3,7 +3,12 @@ import uvicorn from app.main import app +from app.common import settings if __name__ == '__main__': - uvicorn.run(app) + uvicorn.run( + app=app, + host=settings.app_host, + port=settings.app_port, + ) diff --git a/requirements.txt b/requirements.txt index ad31c53..a8174e0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ fastapi==0.92.0 uvicorn[standard]==0.20.0 +gunicorn==20.1.0 jinja2==3.1.2 starlette-wtf==0.4.3 sqlalchemy==2.0.4