From 2531a1f8bc98c82ad18379f2f3527d2875913186 Mon Sep 17 00:00:00 2001
From: "guilhem.carron" <gcarron@grandlyon.com>
Date: Mon, 16 Aug 2021 10:37:02 +0200
Subject: [PATCH] Add local docker compose with hot reload and https

---
 Dockerfile.local         | 21 +++++++++++++++++++++
 README.md                | 15 +++++++++++++--
 docker-compose.local.yml | 13 +++++++++----
 nginx/site.conf          |  7 +++----
 package.json             |  2 +-
 src/hooks/useAuth.ts     |  4 ++--
 6 files changed, 49 insertions(+), 13 deletions(-)
 create mode 100644 Dockerfile.local

diff --git a/Dockerfile.local b/Dockerfile.local
new file mode 100644
index 00000000..c8e9a4fc
--- /dev/null
+++ b/Dockerfile.local
@@ -0,0 +1,21 @@
+# Stage 1, based on Nginx, to have only the compiled app, ready for production with Nginx
+FROM nginx:1.16
+COPY ./nginx/nginx.conf /etc/nginx/nginx.conf
+COPY ./nginx/site.prod.conf /etc/nginx/conf.d/default.conf
+EXPOSE 80
+CMD ["nginx", "-g", "daemon off;"]
+
+# Stage 0, "build-stage", based on Node.js, to build and compile the frontend
+FROM node:14-alpine as build-stage
+
+WORKDIR /app
+# install app dependencies
+COPY package*.json ./
+RUN npm install
+
+# add app
+COPY . ./
+
+# start app
+CMD ["npm", "start"] 
+
diff --git a/README.md b/README.md
index 669108c2..cfd87aae 100644
--- a/README.md
+++ b/README.md
@@ -1,8 +1,19 @@
 # Client - LLLE
 
 Projet client du backoffice LLLE
+
 ## Local usage
 
+In order to launch the projet in local with the backend working launch the following command
+
+```bash
+yarn docker-local-up
+```
+
+To stop it properly use
+
 ```bash
-docker-compose up -d
-```
\ No newline at end of file
+yarn docker-local-down
+```
+
+The nginx server handles https request and responses from the backend
diff --git a/docker-compose.local.yml b/docker-compose.local.yml
index c93ac0c1..f9f0b99f 100644
--- a/docker-compose.local.yml
+++ b/docker-compose.local.yml
@@ -4,13 +4,18 @@ services:
     container_name: front
     build:
       context: .
-      dockerfile: Dockerfile
+      dockerfile: Dockerfile.local
     depends_on:
-      database:
-        condition: service_healthy
+      - nginx
     volumes:
-      - '/app'
+      - '.:/app'
       - '/app/node_modules'
+    ports:
+      - 3000:3000
+
+  nginx:
+    image: nginx:1.16
+    volumes:
       - ./nginx/nginx.conf:/etc/nginx/nginx.conf
       - ./nginx/site.conf:/etc/nginx/conf.d/default.conf
       - ./cert.pem:/etc/nginx/cert.pem
diff --git a/nginx/site.conf b/nginx/site.conf
index 5849a845..8e57664c 100644
--- a/nginx/site.conf
+++ b/nginx/site.conf
@@ -6,10 +6,8 @@ server {
         ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
         ssl_ciphers         HIGH:!aNULL:!MD5;
 
-       location / {
-            root /usr/share/nginx/html;
-            index index.html index.htm;
-            try_files $uri $uri/ /index.html =404;
+        location / {
+            proxy_pass https://front;
         }
         location /api {  
             proxy_pass https://backend:1443/api;
@@ -20,6 +18,7 @@ server {
         location /OAuth2Callback {
             proxy_pass https://backend:1443/OAuth2Callback;
         }
+        
         location /Logout {
             proxy_pass https://backend:1443/Logout;
         }
diff --git a/package.json b/package.json
index 291c5b08..1352e11d 100644
--- a/package.json
+++ b/package.json
@@ -3,7 +3,7 @@
   "version": "0.1.0",
   "private": true,
   "scripts": {
-    "start": "react-scripts start",
+    "start": "set PORT=443 && react-scripts start",
     "build": "react-scripts build",
     "docker-local-up" : "docker-compose -f docker-compose.local.yml up",
     "docker-local-down" : "docker-compose -f docker-compose.local.yml down",
diff --git a/src/hooks/useAuth.ts b/src/hooks/useAuth.ts
index 656444a2..d27df270 100644
--- a/src/hooks/useAuth.ts
+++ b/src/hooks/useAuth.ts
@@ -16,7 +16,7 @@ export const useAuth = (): Auth => {
   //login user
   const loginUser = async (): Promise<void> => {
     try {
-      window.location.href="/OAuth2Login"
+      window.location.href = '/OAuth2Login'
       await setUserContext()
     } catch (e) {
       setError(e)
@@ -26,7 +26,7 @@ export const useAuth = (): Auth => {
   const logoutUser = async (): Promise<void> => {
     try {
       if (setUser) setUser(null)
-      window.location.href="/Logout"
+      window.location.href = '/Logout'
     } catch (e) {
       setError(e)
     }
-- 
GitLab