Skip to content

Commit

Permalink
Migrate database to localhost (#92)
Browse files Browse the repository at this point in the history
This PR removes the *hardcoded* values of the remote atlas database.
It introduces a new local database deployment in the docker-compose
workflow.
The database is secured thanks to docker networking.

I tested the whole migration ✔️ 

⚠️ This PR doesn't migrate the current atlas db to the new local
one. This action must be done "by hand" when the website will be bumped.
  • Loading branch information
atxr authored Apr 16, 2023
1 parent e34757f commit 5764dba
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
3 changes: 1 addition & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 3 additions & 4 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' }))
Expand All @@ -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}`)
Expand Down
25 changes: 25 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,6 +27,8 @@ services:
react-app:
depends_on:
- node-app
networks:
- proxy
env_file: ./.env.production
build:
context: ./frontend
Expand All @@ -19,9 +37,12 @@ services:

nginx:
restart: always
networks:
- proxy
depends_on:
- react-app
- node-app
- mongodb
build:
context: ./nginx
args:
Expand All @@ -40,3 +61,7 @@ services:
volumes:
- ./certbot/www/:/var/www/certbot/:rw
- ./certbot/conf/:/etc/letsencrypt/:rw

networks:
proxy:
database:

0 comments on commit 5764dba

Please sign in to comment.