From ece6ccd1d196c9bf54cf0af628b12e874ab9b756 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rafael=20L=C3=A1szl=C3=B3?= <rlacko99@gmail.com>
Date: Wed, 3 Feb 2021 15:26:30 +0100
Subject: [PATCH] replace npm serve with nginx

---
 .gitlab-ci.yml | 20 +++++++++++++++--
 Dockerfile     | 20 +++++------------
 nginx.conf     | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++
 package.json   |  3 +--
 4 files changed, 85 insertions(+), 19 deletions(-)
 create mode 100644 nginx.conf

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 6c76f28..1aa583b 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -1,11 +1,27 @@
-image: node:latest
-
 variables:
   CONTAINER_IMAGE: 'registry.kszk.bme.hu/kszk/devteam/$CI_PROJECT_NAME:$CI_COMMIT_REF_NAME'
 
 stages:
+  - Pre Build
   - Docker Build
 
+PreBuild:
+  stage: Pre Build
+  tags: [docker]
+  image: node:latest
+  script:
+    - npm install
+    - npm run build
+  cache:
+    paths:
+      - build/
+  artifacts:
+    expire_in: 1 day
+    paths:
+      - build/
+  only:
+    - tags
+
 Docker build:
   stage: Docker Build
   tags: [docker]
diff --git a/Dockerfile b/Dockerfile
index 04857ef..8647bbd 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,19 +1,9 @@
-FROM node:10-alpine
+FROM nginx:1.19.2
 
-WORKDIR /opt/app
-ENV TZ Europe/Budapest
-RUN apk add --no-cache tzdata && ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
+# copy the files builded with npm run build
+COPY build /var/www
+COPY nginx.conf /etc/nginx/nginx.conf
 
-# Copying application files
-COPY ./src ./src
-COPY ./public ./public
-COPY package.json package-lock.json ./
-
-# Installing dependencies
-RUN npm install 
-RUN npm install serve
-
-# Running
 EXPOSE 3000
-ENTRYPOINT ["npm", "run", "deploy"]
 
+ENTRYPOINT ["nginx","-g","daemon off;"]
diff --git a/nginx.conf b/nginx.conf
new file mode 100644
index 0000000..70e3220
--- /dev/null
+++ b/nginx.conf
@@ -0,0 +1,61 @@
+# auto detects a good number of processes to run
+worker_processes auto;
+
+#Provides the configuration file context in which the directives that affect connection processing are specified.
+events {
+    # Sets the maximum number of simultaneous connections that can be opened by a worker process.
+    worker_connections 8000;
+    # Tells the worker to accept multiple connections at a time
+    multi_accept on;
+}
+
+
+http {
+    # what times to include
+    include       /etc/nginx/mime.types;
+    # what is the default one
+    default_type  application/octet-stream;
+
+    # Sets the path, format, and configuration for a buffered log write
+    log_format compression '$remote_addr - $remote_user [$time_local] '
+        '"$request" $status $upstream_addr '
+        '"$http_referer" "$http_user_agent"';
+
+    server {
+        # listen on port 80
+        listen 3000;
+        # save logs here
+        access_log /var/log/nginx/access.log compression;
+
+        # where the root here
+        root /var/www;
+        # what file to server as index
+        index index.html index.htm;
+
+        location / {
+            # First attempt to serve request as file, then
+            # as directory, then fall back to redirecting to index.html
+            try_files $uri $uri/ /index.html;
+        }
+
+        # Media: images, icons, video, audio, HTC
+        location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
+          expires 1M;
+          access_log off;
+          add_header Cache-Control "public";
+        }
+
+        # Javascript and CSS files
+        location ~* \.(?:css|js)$ {
+            try_files $uri =404;
+            expires 1y;
+            access_log off;
+            add_header Cache-Control "public";
+        }
+
+        # Any route containing a file extension (e.g. /devicesfile.js)
+        location ~ ^.+\..+$ {
+            try_files $uri =404;
+        }
+    }
+}
diff --git a/package.json b/package.json
index 55fee57..3ebf8e5 100644
--- a/package.json
+++ b/package.json
@@ -28,8 +28,7 @@
     "start": "react-scripts start",
     "build": "react-scripts build",
     "test": "react-scripts test --env=jsdom",
-    "eject": "react-scripts eject",
-    "deploy": "serve -l 3000 -s build"
+    "eject": "react-scripts eject"
   },
   "devDependencies": {
     "eslint": "6.8.0",
-- 
GitLab