Skip to content

Commit 08fa523

Browse files
authored
Merge pull request #74 from feliciagan/separate-db
separate mongodb instances for each service
2 parents bfa435e + c12affc commit 08fa523

File tree

12 files changed

+97
-57
lines changed

12 files changed

+97
-57
lines changed

backend/.env.sample

Lines changed: 0 additions & 16 deletions
This file was deleted.

backend/README.md

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,12 @@
22

33
> Before proceeding to each microservice for more instructions:
44
5-
1. Set-up either a local or cloud MongoDB.
5+
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.
66

7-
2. Set-up Firebase.
7+
2. Set up Firebase.
88

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

11-
## Setting-up local MongoDB (only if you are using Docker)
12-
13-
1. In the `backend` directory, create a copy of the `.env.sample` file and name it `.env`.
14-
15-
2. To set up credentials for the MongoDB database, update `MONGO_INITDB_ROOT_USERNAME`, `MONGO_INITDB_ROOT_PASSWORD` of the `.env` file.
16-
17-
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.
18-
19-
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.
20-
2111
## Setting-up cloud MongoDB (in production)
2212

2313
> 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)
@@ -97,6 +87,8 @@
9787

9888
## Setting-up Firebase
9989

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

10294
2. Create a project and choose a project name. Navigate to `Storage` and click on it to activate it.

backend/matching-service/.env.sample

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
NODE_ENV=development
2-
PORT=3002
2+
SERVICE_PORT=3002
33

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

backend/matching-service/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ io.on("connection", (socket) => {
1717
handleWebsocketMatchEvents(socket);
1818
});
1919

20-
const PORT = process.env.PORT || 3002;
20+
const PORT = process.env.SERVICE_PORT || 3002;
2121

2222
if (process.env.NODE_ENV !== "test") {
2323
connectRabbitMq()
Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
NODE_ENV=development
2-
PORT=3000
3-
4-
MONGO_CLOUD_URI=<MONGO_CLOUD_URI>
5-
MONGO_LOCAL_URI=<MONGO_LOCAL_URI>
2+
SERVICE_PORT=3000
63

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

1613
MONGO_URI_TEST=mongodb://mongo:mongo@test-mongo:27017/
14+
15+
# if using cloud MongoDB, replace with actual URI (run service separately)
16+
MONGO_CLOUD_URI=<MONGO_CLOUD_URI>
17+
18+
# if using local MongoDB (run service with docker-compose)
19+
## MongoDB credentials
20+
MONGO_INITDB_ROOT_USERNAME=root
21+
MONGO_INITDB_ROOT_PASSWORD=example
22+
23+
## Mongo Express credentials
24+
ME_CONFIG_BASICAUTH_USERNAME=admin
25+
ME_CONFIG_BASICAUTH_PASSWORD=password
26+
27+
## Do not change anything below this line
28+
ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_INITDB_ROOT_USERNAME}
29+
ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_INITDB_ROOT_PASSWORD}
30+
ME_CONFIG_MONGODB_URL=mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@question-service-mongo:27017/
31+
32+
MONGO_LOCAL_URI=mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@question-service-mongo:27017/

backend/question-service/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88

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

11-
3. Update `MONGO_CLOUD_URI`, `MONGO_LOCAL_URI`, `FIREBASE_PROJECT_ID`, `FIREBASE_PRIVATE_KEY`, `FIREBASE_CLIENT_EMAIL`, `FIREBASE_STORAGE_BUCKET`.
11+
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.
12+
13+
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.
1214

1315
## Running Question Service without Docker
1416

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

1721
2. Run the command: `npm install`. This will install all the necessary dependencies.

backend/question-service/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import app from "./app.ts";
22
import connectDB from "./config/db.ts";
33

4-
const PORT = process.env.PORT || 3000;
4+
const PORT = process.env.SERVICE_PORT || 3000;
55

66
if (process.env.NODE_ENV !== "test") {
77
connectDB()

backend/user-service/.env.sample

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
NODE_ENV=development
2-
PORT=3001
3-
4-
MONGO_CLOUD_URI=<MONGO_CLOUD_URI>
5-
MONGO_LOCAL_URI=<MONGO_LOCAL_URI>
2+
SERVICE_PORT=3001
63

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

3532
# Test
3633
MONGO_URI_TEST=mongodb://mongo:mongo@test-mongo:27017/
37-
REDIS_URI_TEST=redis://test-redis:6379
34+
REDIS_URI_TEST=redis://test-redis:6379
35+
36+
# if using cloud MongoDB, replace with actual URI (run service separately)
37+
MONGO_CLOUD_URI=<MONGO_CLOUD_URI>
38+
39+
# if using local MongoDB (run service with docker-compose)
40+
## MongoDB credentials
41+
MONGO_INITDB_ROOT_USERNAME=root
42+
MONGO_INITDB_ROOT_PASSWORD=example
43+
44+
## Mongo Express credentials
45+
ME_CONFIG_BASICAUTH_USERNAME=admin
46+
ME_CONFIG_BASICAUTH_PASSWORD=password
47+
48+
## Do not change anything below this line
49+
ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_INITDB_ROOT_USERNAME}
50+
ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_INITDB_ROOT_PASSWORD}
51+
ME_CONFIG_MONGODB_URL=mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@user-service-mongo:27017/
52+
53+
MONGO_LOCAL_URI=mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@user-service-mongo:27017/

backend/user-service/README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
- `MONGO_CLOUD_URI`
1414

15-
- `MONGO_LOCAL_URI`
16-
1715
- `FIREBASE_PROJECT_ID`
1816

1917
- `FIREBASE_PRIVATE_KEY`
@@ -32,9 +30,13 @@
3230

3331
- `REDIS_URI`
3432

35-
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.
33+
You can also update `MONGO_INITDB_ROOT_USERNAME`, `MONGO_INITDB_ROOT_PASSWORD` to change your MongoDB credentials if necessary.
34+
35+
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.
3636

37-
5. To view the contents stored in Redis,
37+
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.
38+
39+
6. To view the contents stored in Redis,
3840

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

@@ -44,6 +46,8 @@
4446

4547
## Running User Service Individually
4648

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

4953
2. Open Command Line/Terminal and navigate into the `user-service` directory.

backend/user-service/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { connectRedis } from "./config/redis.ts";
77

88
dotenv.config();
99

10-
const port = process.env.PORT || 3001;
10+
const port = process.env.SERVICE_PORT || 3001;
1111

1212
const server = http.createServer(index);
1313

0 commit comments

Comments
 (0)