Bugfixes in docker-compose and DB, gunicorn for prod, env vars for app host and port

This commit is contained in:
DarkCat09 2023-03-03 18:10:17 +04:00
parent 2135126235
commit 71964355b7
8 changed files with 43 additions and 5 deletions

3
.env
View file

@ -1,3 +1,6 @@
APP_HOST=0.0.0.0
APP_PORT=8000
DB_HOST=${REPO_NAME_SNAKE}_db DB_HOST=${REPO_NAME_SNAKE}_db
DB_PORT=3306 DB_PORT=3306
DB_USER=${REPO_NAME_SNAKE} DB_USER=${REPO_NAME_SNAKE}

View file

@ -1,5 +1,5 @@
MYSQL_HOST=${REPO_NAME_SNAKE}_db
MYSQL_PORT=3306 MYSQL_PORT=3306
MYSQL_RANDOM_ROOT_PASSWORD=true
MYSQL_USER=${REPO_NAME_SNAKE} MYSQL_USER=${REPO_NAME_SNAKE}
MYSQL_PASSWORD= MYSQL_PASSWORD=
MYSQL_DATABASE=${REPO_NAME_SNAKE} MYSQL_DATABASE=${REPO_NAME_SNAKE}

View file

@ -1,6 +1,11 @@
DEBUG=true DEBUG=true
SESSION_KEY=debug SESSION_KEY=debug
CSRF_KEY=debug CSRF_KEY=debug
APP_HOST=127.0.0.1
APP_PORT=8000
DB_HOST=localhost DB_HOST=localhost
DB_PORT=3306 DB_PORT=3306
DB_USER=darkcat09 DB_USER=darkcat09

View file

@ -1,9 +1,14 @@
APP_HOST ?= 0.0.0.0
APP_PORT ?= 8000
dev: dev:
python3 -m dotenv -f .env_debug run \ python3 -m dotenv -f .env_debug run \
python3 -m uvicorn main:app --reload make cmd-dev
prod: prod:
python3 -m uvicorn main:app python3 -m gunicorn \
-w 4 -k uvicorn.workers.UvicornWorker \
-b $${APP_HOST}:$${APP_PORT} main:app
format: format:
python3 -m autopep8 -r --in-place app/ python3 -m autopep8 -r --in-place app/
@ -30,6 +35,12 @@ clean:
rm -rf __pycache__ rm -rf __pycache__
rm -rf .mypy_cache rm -rf .mypy_cache
cmd-dev:
python3 -m uvicorn main:app \
--reload \
--host $${APP_HOST} \
--port $${APP_PORT}
cmd-docker-build: cmd-docker-build:
docker build \ docker build \
-t ${REPO_OWNER_LOWER}/${REPO_NAME_SNAKE}:latest \ -t ${REPO_OWNER_LOWER}/${REPO_NAME_SNAKE}:latest \

View file

@ -20,6 +20,8 @@ class Settings(BaseSettings):
debug: bool = False debug: bool = False
session_key: str = secrets.token_hex(32) session_key: str = secrets.token_hex(32)
csrf_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() settings = Settings()

View file

@ -1,6 +1,7 @@
version: "3" version: "3"
services: services:
${REPO_NAME_SNAKE}: ${REPO_NAME_SNAKE}:
image: ${REPO_OWNER_LOWER}/${REPO_NAME_SNAKE}:latest image: ${REPO_OWNER_LOWER}/${REPO_NAME_SNAKE}:latest
container_name: ${REPO_NAME_SNAKE} container_name: ${REPO_NAME_SNAKE}
@ -10,10 +11,20 @@ services:
links: links:
- ${REPO_NAME_SNAKE}_db - ${REPO_NAME_SNAKE}_db
env_file: .env env_file: .env
depends_on:
${REPO_NAME_SNAKE}_db:
condition: service_healthy
${REPO_NAME_SNAKE}_db: ${REPO_NAME_SNAKE}_db:
image: mariadb:latest image: mariadb:latest
container_name: ${REPO_NAME_SNAKE}_db container_name: ${REPO_NAME_SNAKE}_db
restart: unless-stopped restart: unless-stopped
volumes: volumes:
- "./database:/var/lib/mysql" - "./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

View file

@ -3,7 +3,12 @@
import uvicorn import uvicorn
from app.main import app from app.main import app
from app.common import settings
if __name__ == '__main__': if __name__ == '__main__':
uvicorn.run(app) uvicorn.run(
app=app,
host=settings.app_host,
port=settings.app_port,
)

View file

@ -1,5 +1,6 @@
fastapi==0.92.0 fastapi==0.92.0
uvicorn[standard]==0.20.0 uvicorn[standard]==0.20.0
gunicorn==20.1.0
jinja2==3.1.2 jinja2==3.1.2
starlette-wtf==0.4.3 starlette-wtf==0.4.3
sqlalchemy==2.0.4 sqlalchemy==2.0.4