From 40321f4be75992b7bf21018104b761cfcf6889df Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 16 Aug 2023 15:41:09 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(frontend):=20update=20Docker?= =?UTF-8?q?file=20to=20use=20node:20-alpine=20as=20base=20image=20for=20fr?= =?UTF-8?q?ontend=20build=20=E2=9C=A8=20feat(frontend):=20add=20support=20?= =?UTF-8?q?for=20BACKEND=5FURL=20environment=20variable=20in=20nginx.conf?= =?UTF-8?q?=20to=20configure=20backend=20URL=20=F0=9F=93=9D=20chore(fronte?= =?UTF-8?q?nd):=20add=20start-nginx.sh=20script=20to=20replace=20placehold?= =?UTF-8?q?er=20in=20nginx.conf=20with=20actual=20BACKEND=5FURL=20and=20st?= =?UTF-8?q?art=20nginx?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frontend/Dockerfile | 14 ++++++++---- src/frontend/nginx.conf | 45 +++++++++++++++++++++++++------------ src/frontend/start-nginx.sh | 8 +++++++ 3 files changed, 49 insertions(+), 18 deletions(-) create mode 100644 src/frontend/start-nginx.sh diff --git a/src/frontend/Dockerfile b/src/frontend/Dockerfile index 010811e7b..81abf18cd 100644 --- a/src/frontend/Dockerfile +++ b/src/frontend/Dockerfile @@ -1,10 +1,16 @@ -FROM node:14-alpine as frontend_build -ARG BACKEND +FROM node:20-alpine as frontend_build +ARG BACKEND_URL WORKDIR /app -COPY . /app +COPY ./src /app/src +COPY ./package.json ./package-lock.json ./tsconfig.json ./vite.config.ts ./index.html ./tailwind.config.js ./postcss.config.js ./prettier.config.js /app/ + RUN npm install RUN npm run build FROM nginx COPY --from=frontend_build /app/build/ /usr/share/nginx/html -COPY /nginx.conf /etc/nginx/conf.d/default.conf \ No newline at end of file +COPY /nginx.conf /etc/nginx/conf.d/default.conf +COPY start-nginx.sh /start-nginx.sh +RUN chmod +x /start-nginx.sh +ENV BACKEND_URL=$BACKEND_URL +CMD ["/start-nginx.sh"] \ No newline at end of file diff --git a/src/frontend/nginx.conf b/src/frontend/nginx.conf index df665f5b6..d069a206d 100644 --- a/src/frontend/nginx.conf +++ b/src/frontend/nginx.conf @@ -1,18 +1,35 @@ server { - gzip on; - gzip_comp_level 2; - gzip_min_length 1000; - gzip_types text/xml text/css; - gzip_http_version 1.1; - gzip_vary on; - gzip_disable "MSIE [4-6] \."; + gzip on; + gzip_comp_level 2; + gzip_min_length 1000; + gzip_types text/xml text/css; + gzip_http_version 1.1; + gzip_vary on; + gzip_disable "MSIE [4-6] \."; - listen 80; + listen 80; - location / { - root /usr/share/nginx/html; - index index.html index.htm; - try_files $uri $uri/ /index.html =404; - } - + location / { + root /usr/share/nginx/html; + index index.html index.htm; + try_files $uri $uri/ /index.html =404; + } + + location ~ ^/api/v1/ { # Matching the /api/v1/ route + proxy_pass __BACKEND_URL__; # URL of your backend service + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header Host $host; + proxy_cache_bypass $http_upgrade; + } + + location /health { # Matching the /health route + proxy_pass __BACKEND_URL__; # URL of your backend service + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header Host $host; + proxy_cache_bypass $http_upgrade; + } } diff --git a/src/frontend/start-nginx.sh b/src/frontend/start-nginx.sh new file mode 100644 index 000000000..dd20f5305 --- /dev/null +++ b/src/frontend/start-nginx.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +# Replace the placeholder with the actual value +sed -i "s|__BACKEND_URL__|$BACKEND_URL|g" /etc/nginx/conf.d/default.conf + + +# Start nginx +exec nginx -g 'daemon off;'