From d95334b86c0baae467c94eac5f08590a056bf56c Mon Sep 17 00:00:00 2001 From: atxr Date: Fri, 14 Apr 2023 18:53:35 +0200 Subject: [PATCH 1/4] Add support for local database Add new mongodb app in docker-compose This app has a volume in mongodb/ to store the data Setup two docker networks to separate backend and proxy traffic Update the backend to connect to localhost instead of remote database --- backend/app.js | 7 +++---- docker-compose.yml | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/backend/app.js b/backend/app.js index 6750ded..a188618 100644 --- a/backend/app.js +++ b/backend/app.js @@ -8,7 +8,7 @@ const articleRoutes = require('./routes/articles') const httpServer = createServer(app) require('dotenv').config({ path: `../.env.${process.env.NODE_ENV}` }) -const { DB_USER, DB_PASSWORD, DB_NAME, NODE_PORT, CLIENT_ORIGIN } = process.env +const { DB_PORT, DB_NAME, NODE_PORT, CLIENT_ORIGIN } = process.env app.use(cors({ origin: CLIENT_ORIGIN || 'http://localhost:3000' })) app.use(express.json({ limit: '1MB' })) @@ -19,10 +19,9 @@ app.get('/', (req, res) => { res.send('iscsc.fr is running') }) +uri = `mongodb://mongodb:${DB_PORT}/${DB_NAME}?retryWrites=true&w=majority` mongoose - .connect( - `mongodb+srv://${DB_USER}:${DB_PASSWORD}@iscsc.a11re32.mongodb.net/${DB_NAME}?retryWrites=true&w=majority` - ) + .connect(uri) .then(() => { httpServer.listen(NODE_PORT || 3001, () => { console.log(`Server listening: http://localhost:${NODE_PORT}`) diff --git a/docker-compose.yml b/docker-compose.yml index e21b480..0199119 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,23 @@ version: "3.8" services: + mongodb: + image: docker.io/bitnami/mongodb:4.4 + networks: + - database + restart: always + env_file: ./.env.production + ports: + - $DB_PORT:$DB_PORT + volumes: + - './mongodb:/data/db' + node-app: + depends_on: + - mongodb + networks: + - proxy + - database build: ./backend restart: unless-stopped env_file: ./.env.production @@ -11,6 +27,8 @@ services: react-app: depends_on: - node-app + networks: + - proxy env_file: ./.env.production build: context: ./frontend @@ -19,9 +37,12 @@ services: nginx: restart: always + networks: + - proxy depends_on: - react-app - node-app + - mongodb build: context: ./nginx args: @@ -40,3 +61,7 @@ services: volumes: - ./certbot/www/:/var/www/certbot/:rw - ./certbot/conf/:/etc/letsencrypt/:rw + +networks: + proxy: + database: From 9615de218ba608c716600573aa792cbd88cd3513 Mon Sep 17 00:00:00 2001 From: atxr Date: Fri, 14 Apr 2023 19:53:58 +0200 Subject: [PATCH 2/4] Update .env.example file --- .env.example | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.env.example b/.env.example index 24ec3af..915afad 100644 --- a/.env.example +++ b/.env.example @@ -12,8 +12,7 @@ SERVER_NAME=yourdomainename.com # your domain name, for the nginx configuration # mongodb setup DB_NAME=XXX -DB_USER=XXX -DB_PASSWORD=XXX +DB_PORT=XXX # cors CLIENT_ORIGIN=http://localhost:$REACT_PORT # URL of the frontend, to make a cors exception From 11179e4fa0e18b32edd9dbd787b379fa9bc7c850 Mon Sep 17 00:00:00 2001 From: atxr Date: Fri, 14 Apr 2023 19:58:16 +0200 Subject: [PATCH 3/4] Update README --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index dde87a9..1872fbc 100644 --- a/README.md +++ b/README.md @@ -70,8 +70,6 @@ cp .env.example .env.development After copying the example config of `.env`, you must fill in the missing information in this file. Check the example for more information. -> If you don't know how to deploy your database, consider using [Atlas](https://www.mongodb.com/atlas/database). - #### Backend From the root directory of the repository, do the following: From bc5bcbe5536570497d7fa1a95f254dc8b68e5542 Mon Sep 17 00:00:00 2001 From: atxr Date: Sat, 15 Apr 2023 12:11:44 +0200 Subject: [PATCH 4/4] Run prettier --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 0199119..d7a5c00 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,7 +10,7 @@ services: ports: - $DB_PORT:$DB_PORT volumes: - - './mongodb:/data/db' + - "./mongodb:/data/db" node-app: depends_on: