From 91147ec4ad7b18620fd39ce40f81f98da47e95c5 Mon Sep 17 00:00:00 2001 From: Jean Loui Bernard <45553309+JeanExtreme002@users.noreply.github.com> Date: Mon, 15 Jul 2024 09:25:18 +0000 Subject: [PATCH] Use .env file for docker-compose --- .env | 19 +++++++++++++++++++ COMMENTS.md | 2 -- back/.env.sample | 5 ++--- back/src/config.py | 4 ++-- docker-compose.yml | 20 +++++++++++--------- 5 files changed, 34 insertions(+), 16 deletions(-) create mode 100644 .env diff --git a/.env b/.env new file mode 100644 index 0000000..c7d9039 --- /dev/null +++ b/.env @@ -0,0 +1,19 @@ +[Elasticsearch] +ELASTICSEARCH_HOST="elasticsearch" +ELASTICSEARCH_PORT=9200 +ELASTICSEARCH_MAX_RESULTS=20 +ELASTICSEARCH_TIMEOUT=10 + +[Back-end] +BACKEND_HOST="0.0.0.0" +BACKEND_PORT=5000 +WORKERS_COUNT=1 +RELOAD="true" +LOG_LEVEL="INFO" + +[Front-end] +REACT_APP_API_URL="http://localhost:5000/graphql" +REACT_APP_PORT=3000 + +[SEED] +seed=false \ No newline at end of file diff --git a/COMMENTS.md b/COMMENTS.md index 6f90f89..c1247b1 100644 --- a/COMMENTS.md +++ b/COMMENTS.md @@ -75,5 +75,3 @@ Irei dedicar essa seção para falar de coisas que eu faria, ou gostaria de impl - Implementar testes automatizados de UI; - Caso esse projeto fosse deployado para uma cloud, eu implementaria um CD no workflow do GitHub Actions; - -- Utilizar alguma ferramenta para passar as variáveis de ambiente de um arquivo para o *docker-compose*. diff --git a/back/.env.sample b/back/.env.sample index 6981476..16bab19 100644 --- a/back/.env.sample +++ b/back/.env.sample @@ -1,7 +1,6 @@ [Application] -ENVIRONMENT="DEV" -APPLICATION_HOST="0.0.0.0" -APPLICATION_PORT=5000 +BACKEND_HOST="0.0.0.0" +BACKEND_PORT=5000 WORKERS_COUNT=1 RELOAD="true" LOG_LEVEL="INFO" diff --git a/back/src/config.py b/back/src/config.py index 41cc9c7..f9b68af 100644 --- a/back/src/config.py +++ b/back/src/config.py @@ -20,8 +20,8 @@ class Config: Base configuration. """ - HOST = os.getenv("APPLICATION_HOST", "0.0.0.0") - PORT = int(os.getenv("APPLICATION_PORT", "5000")) + HOST = os.getenv("BACKEND_HOST", "0.0.0.0") + PORT = int(os.getenv("BACKEND_PORT", "5000")) WORKERS_COUNT = int(os.getenv("WORKERS_COUNT", "1")) RELOAD = os.getenv("RELOAD", "true").lower() == "true" LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO").lower() diff --git a/docker-compose.yml b/docker-compose.yml index cf64475..90417bb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,30 +6,31 @@ services: environment: - discovery.type=single-node ports: - - "9200:9200" + - "${ELASTICSEARCH_PORT}:${ELASTICSEARCH_PORT}" - "9300:9300" healthcheck: - test: ["CMD-SHELL", "curl -f http://localhost:9200 || exit 1"] + test: ["CMD-SHELL", "curl -f http://localhost:${ELASTICSEARCH_PORT} || exit 1"] interval: 10s timeout: 5s retries: 5 backend: image: python:3.10 + env_file: .env working_dir: /app volumes: - ./back:/app ports: - - "5000:5000" + - "${BACKEND_PORT}:${BACKEND_PORT}" command: > sh -c 'pip install poetry && poetry install && (echo "Waiting for Elasticsearch to be ready..." && - until curl -s http://elasticsearch:9200/_cluster/health | grep -q "\"status\":\"green\|yellow\""; do + until curl -s http://elasticsearch:${ELASTICSEARCH_PORT}/_cluster/health | grep -q "\"status\":\"green\|yellow\""; do echo "Elasticsearch not ready yet, waiting..."; sleep 5; done) && - + echo "Elasticsearch is ready!"; if [ "$seed" = "true" ]; then @@ -40,21 +41,22 @@ services: depends_on: - elasticsearch healthcheck: - test: ["CMD-SHELL", "curl -f http://localhost:5000 || exit 1"] + test: ["CMD-SHELL", "curl -f http://localhost:${BACKEND_PORT} || exit 1"] interval: 10s timeout: 5s retries: 5 frontend: image: node:16 + env_file: .env working_dir: /app volumes: - ./front:/app ports: - - "3000:3000" - command: sh -c "npm install && npm start" + - "${REACT_APP_PORT}:${REACT_APP_PORT}" + command: sh -c "npm install && export PORT=${REACT_APP_PORT} && npm start" healthcheck: - test: ["CMD-SHELL", "curl -f http://localhost:3000 || exit 1"] + test: ["CMD-SHELL", "curl -f http://localhost:${REACT_APP_PORT} || exit 1"] interval: 10s timeout: 5s retries: 5