From e7a96dac69711d8625f95a1e0d9ee0b1a0764169 Mon Sep 17 00:00:00 2001 From: VitorCarvalho67 Date: Mon, 29 Jan 2024 11:09:25 -0300 Subject: [PATCH] Updade .env.example, .gitignore, and include nginx for load balancing --- .env.example | 2 ++ .gitignore | 1 + docker-compose.yml | 30 +++++++++++++++++++++++++++++- nginx.conf | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 .env.example create mode 100644 nginx.conf diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..dfc7718 --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +# DATABASE_URL="mysql://testuser:123pass@database:3306/biblioteca" +# JWT_ACCESS_SECRET=fghjdsklgh \ No newline at end of file diff --git a/.gitignore b/.gitignore index 11ddd8d..ce2c952 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules # Keep environment variables out of version control .env +data \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 5348321..601c1f2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,12 +1,40 @@ version: '3' services: - server: + api01: &api build: ./ ports: - "3000:3000" depends_on: - database + deploy: + resources: + limits: + cpus: '0.15' + memory: '0.4GB' + command: sh -c "sleep 10 && npx prisma migrate deploy" + networks: + - API + + api02: + <<: *api + ports: + - "3001:3000" + + nginx: # Load Balancer + image: nginx:latest + volumes: + - ./nginx.conf:/etc/nginx/nginx.conf:ro + depends_on: + - api01 + - api02 + deploy: + resources: + limits: + cpus: '0.15' + memory: '0.5GB' + ports: + - "9999:9999" networks: - API diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..7c4d1a2 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,46 @@ +worker_processes auto; +worker_rlimit_nofile 500000; + +events { + use epoll; + worker_connections 1024; +} + +http { + access_log off; + error_log /dev/null emerg; + + upstream api { + server api01:3000; + server api02:3001; # Alterei de localhost para o nome do serviço + keepalive 200; + } + + server { + listen 9999; + location / { + proxy_buffering off; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_set_header Connection ""; + + # Simple requests + if ($request_method ~* "(GET|POST)") { + add_header "Access-Control-Allow-Origin" *; + } + + # Preflighted requests + if ($request_method = OPTIONS ) { + add_header "Access-Control-Allow-Origin" *; + add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS, HEAD"; + add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept"; + return 200; + } + + proxy_http_version 1.1; + proxy_pass http://api; + } + } +} \ No newline at end of file