Skip to content

Commit

Permalink
Merge pull request #74 from feliciagan/separate-db
Browse files Browse the repository at this point in the history
separate mongodb instances for each service
  • Loading branch information
nicolelim02 authored Oct 20, 2024
2 parents bfa435e + c12affc commit 08fa523
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 57 deletions.
16 changes: 0 additions & 16 deletions backend/.env.sample

This file was deleted.

16 changes: 4 additions & 12 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,12 @@

> Before proceeding to each microservice for more instructions:
1. Set-up either a local or cloud MongoDB.
1. Set up cloud MongoDB if not using docker. We recommend this if you are just testing out each microservice separately to avoid needing to manually set up multiple instances of local MongoDB. Else, if you are using docker-compose.yml to run PeerPrep, check out the READMEs in the different backend microservices to set up the env for the local MongoDB instances.

2. Set-up Firebase.
2. Set up Firebase.

3. Follow the instructions [here](https://nodejs.org/en/download/package-manager) to set up Node v20.

## Setting-up local MongoDB (only if you are using Docker)

1. In the `backend` directory, create a copy of the `.env.sample` file and name it `.env`.

2. To set up credentials for the MongoDB database, update `MONGO_INITDB_ROOT_USERNAME`, `MONGO_INITDB_ROOT_PASSWORD` of the `.env` file.

3. Your local Mongo URI will be `mongodb://<MONGO_INITDB_ROOT_USERNAME>:<MONGO_INITDB_ROOT_PASSWORD>@mongo:27017/`. Take note of it as we will be using in the `.env` files in the various microservices later on.

4. You can view the MongoDB collections locally using Mongo Express. To set up Mongo Express, update `ME_CONFIG_BASICAUTH_USERNAME` and `ME_CONFIG_BASICAUTH_PASSWORD`. The username and password will be the login credentials when you access Mongo Express at http://localhost:8081.

## Setting-up cloud MongoDB (in production)

> This guide references the [user-service README in the PeerPrep-UserService repository](https://github.com/CS3219-AY2425S1/PeerPrep-UserService/blob/main/user-service/README.md)
Expand Down Expand Up @@ -97,6 +87,8 @@

## Setting-up Firebase

> For ease of testing, you can set up just one firebase to use across all the microservices that need it.
1. Go to https://console.firebase.google.com/u/0/.

2. Create a project and choose a project name. Navigate to `Storage` and click on it to activate it.
Expand Down
2 changes: 1 addition & 1 deletion backend/matching-service/.env.sample
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NODE_ENV=development
PORT=3002
SERVICE_PORT=3002

ORIGINS=http://localhost:5173,http://127.0.0.1:5173

Expand Down
2 changes: 1 addition & 1 deletion backend/matching-service/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ io.on("connection", (socket) => {
handleWebsocketMatchEvents(socket);
});

const PORT = process.env.PORT || 3002;
const PORT = process.env.SERVICE_PORT || 3002;

if (process.env.NODE_ENV !== "test") {
connectRabbitMq()
Expand Down
24 changes: 20 additions & 4 deletions backend/question-service/.env.sample
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
NODE_ENV=development
PORT=3000

MONGO_CLOUD_URI=<MONGO_CLOUD_URI>
MONGO_LOCAL_URI=<MONGO_LOCAL_URI>
SERVICE_PORT=3000

FIREBASE_PROJECT_ID=<FIREBASE_PROJECT_ID>
FIREBASE_PRIVATE_KEY=<FIREBASE_PRIVATE_KEY>
Expand All @@ -14,3 +11,22 @@ ORIGINS=http://localhost:5173,http://127.0.0.1:5173
USER_SERVICE_URL=http://user-service:3001/api

MONGO_URI_TEST=mongodb://mongo:mongo@test-mongo:27017/

# if using cloud MongoDB, replace with actual URI (run service separately)
MONGO_CLOUD_URI=<MONGO_CLOUD_URI>

# if using local MongoDB (run service with docker-compose)
## MongoDB credentials
MONGO_INITDB_ROOT_USERNAME=root
MONGO_INITDB_ROOT_PASSWORD=example

## Mongo Express credentials
ME_CONFIG_BASICAUTH_USERNAME=admin
ME_CONFIG_BASICAUTH_PASSWORD=password

## Do not change anything below this line
ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_INITDB_ROOT_USERNAME}
ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_INITDB_ROOT_PASSWORD}
ME_CONFIG_MONGODB_URL=mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@question-service-mongo:27017/

MONGO_LOCAL_URI=mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@question-service-mongo:27017/
6 changes: 5 additions & 1 deletion backend/question-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@

2. To connect to your cloud MongoDB instead of your local MongoDB, set the `NODE_ENV` to `production` instead of `development`.

3. Update `MONGO_CLOUD_URI`, `MONGO_LOCAL_URI`, `FIREBASE_PROJECT_ID`, `FIREBASE_PRIVATE_KEY`, `FIREBASE_CLIENT_EMAIL`, `FIREBASE_STORAGE_BUCKET`.
3. Update `FIREBASE_PROJECT_ID`, `FIREBASE_PRIVATE_KEY`, `FIREBASE_CLIENT_EMAIL`, `FIREBASE_STORAGE_BUCKET`, `MONGO_CLOUD_URI` with the env variables obtained from following the instructions in the backend README. Then update `MONGO_INITDB_ROOT_USERNAME`, `MONGO_INITDB_ROOT_PASSWORD` to change your MongoDB credentials if necessary.

4. You can view the MongoDB collections locally using Mongo Express. To set up Mongo Express, update `ME_CONFIG_BASICAUTH_USERNAME` and `ME_CONFIG_BASICAUTH_PASSWORD`. The username and password will be the login credentials when you access Mongo Express at http://localhost:8081.

## Running Question Service without Docker

> Make sure you have the cloud MongoDB URI in your .env file and set NODE_ENV to production already.
1. Open Command Line/Terminal and navigate into the `question-service` directory.

2. Run the command: `npm install`. This will install all the necessary dependencies.
Expand Down
2 changes: 1 addition & 1 deletion backend/question-service/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import app from "./app.ts";
import connectDB from "./config/db.ts";

const PORT = process.env.PORT || 3000;
const PORT = process.env.SERVICE_PORT || 3000;

if (process.env.NODE_ENV !== "test") {
connectDB()
Expand Down
26 changes: 21 additions & 5 deletions backend/user-service/.env.sample
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
NODE_ENV=development
PORT=3001

MONGO_CLOUD_URI=<MONGO_CLOUD_URI>
MONGO_LOCAL_URI=<MONGO_LOCAL_URI>
SERVICE_PORT=3001

# Secret for creating JWT signature
JWT_SECRET=<JWT_SECRET>
Expand Down Expand Up @@ -34,4 +31,23 @@ REDIS_URI=redis://redis:6379 # Uncomment if you're running the user service usin

# Test
MONGO_URI_TEST=mongodb://mongo:mongo@test-mongo:27017/
REDIS_URI_TEST=redis://test-redis:6379
REDIS_URI_TEST=redis://test-redis:6379

# if using cloud MongoDB, replace with actual URI (run service separately)
MONGO_CLOUD_URI=<MONGO_CLOUD_URI>

# if using local MongoDB (run service with docker-compose)
## MongoDB credentials
MONGO_INITDB_ROOT_USERNAME=root
MONGO_INITDB_ROOT_PASSWORD=example

## Mongo Express credentials
ME_CONFIG_BASICAUTH_USERNAME=admin
ME_CONFIG_BASICAUTH_PASSWORD=password

## Do not change anything below this line
ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_INITDB_ROOT_USERNAME}
ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_INITDB_ROOT_PASSWORD}
ME_CONFIG_MONGODB_URL=mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@user-service-mongo:27017/

MONGO_LOCAL_URI=mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@user-service-mongo:27017/
12 changes: 8 additions & 4 deletions backend/user-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

- `MONGO_CLOUD_URI`

- `MONGO_LOCAL_URI`

- `FIREBASE_PROJECT_ID`

- `FIREBASE_PRIVATE_KEY`
Expand All @@ -32,9 +30,13 @@

- `REDIS_URI`

4. A default admin account (`email: [email protected]` and `password: Admin@123`) wil be created. If you wish to change the default credentials, update them in `.env`. Alternatively, you can also edit your credentials and user profile after you have created the default account.
You can also update `MONGO_INITDB_ROOT_USERNAME`, `MONGO_INITDB_ROOT_PASSWORD` to change your MongoDB credentials if necessary.

4. You can view the MongoDB collections locally using Mongo Express. To set up Mongo Express, update `ME_CONFIG_BASICAUTH_USERNAME` and `ME_CONFIG_BASICAUTH_PASSWORD`. The username and password will be the login credentials when you access Mongo Express at http://localhost:8082.

5. To view the contents stored in Redis,
5. A default admin account (`email: [email protected]` and `password: Admin@123`) wil be created. If you wish to change the default credentials, update them in `.env`. Alternatively, you can also edit your credentials and user profile after you have created the default account.

6. To view the contents stored in Redis,

1. Go to [http://localhost:5540](http://localhost:5540).

Expand All @@ -44,6 +46,8 @@

## Running User Service Individually

> Make sure you have the cloud MongoDB URI in your .env file and set NODE_ENV to production already.
1. Set up and run Redis using `docker compose run --rm --name redis -p 6379:6379 redis`.

2. Open Command Line/Terminal and navigate into the `user-service` directory.
Expand Down
2 changes: 1 addition & 1 deletion backend/user-service/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { connectRedis } from "./config/redis.ts";

dotenv.config();

const port = process.env.PORT || 3001;
const port = process.env.SERVICE_PORT || 3001;

const server = http.createServer(index);

Expand Down
4 changes: 2 additions & 2 deletions docker-compose-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
# env_file: ./backend/user-service/.env
environment:
- NODE_ENV=test
- PORT=3001
- SERVICE_PORT=3001
- JWT_SECRET
- ADMIN_FIRST_NAME=Admin
- ADMIN_LAST_NAME=User
Expand Down Expand Up @@ -37,7 +37,7 @@ services:
# env_file: ./backend/question-service/.env
environment:
- NODE_ENV=test
- PORT=3000
- SERVICE_PORT=3000
- FIREBASE_PROJECT_ID
- FIREBASE_PRIVATE_KEY
- FIREBASE_CLIENT_EMAIL
Expand Down
42 changes: 33 additions & 9 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
ports:
- 3001:3001
depends_on:
- mongo
- user-service-mongo
- redis
networks:
- peerprep-network
Expand All @@ -26,7 +26,7 @@ services:
ports:
- 3000:3000
depends_on:
- mongo
- question-service-mongo
- user-service
networks:
- peerprep-network
Expand Down Expand Up @@ -72,28 +72,51 @@ services:
- /frontend/node_modules
restart: on-failure

mongo:
question-service-mongo:
image: mongo
restart: always
ports:
- 27017:27017
networks:
- peerprep-network
volumes:
- mongo-data:/data/db
- question-service-mongo-data:/data/db
env_file:
- ./backend/.env
- ./backend/question-service/.env

mongo-express:
question-service-mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
networks:
- peerprep-network
depends_on:
- mongo
env_file: ./backend/.env
- question-service-mongo
env_file: ./backend/question-service/.env

user-service-mongo:
image: mongo
restart: always
ports:
- 27018:27017
networks:
- peerprep-network
volumes:
- user-service-mongo-data:/data/db
env_file:
- ./backend/user-service/.env

user-service-mongo-express:
image: mongo-express
restart: always
ports:
- 8082:8081
networks:
- peerprep-network
depends_on:
- user-service-mongo
env_file: ./backend/user-service/.env

rabbitmq:
image: rabbitmq:4.0-management
Expand Down Expand Up @@ -142,7 +165,8 @@ services:
restart: true

volumes:
mongo-data:
question-service-mongo-data:
user-service-mongo-data:
redis-data:
redis-insight-data:

Expand Down

0 comments on commit 08fa523

Please sign in to comment.