Skip to content

Commit

Permalink
Merge pull request #50 from guanquann/mongodb-docker
Browse files Browse the repository at this point in the history
Add mongodb to Docker + Fix bugs
  • Loading branch information
ruiqi7 authored Oct 6, 2024
2 parents 5adfc36 + fe5134e commit cbaf0ad
Show file tree
Hide file tree
Showing 41 changed files with 511 additions and 172 deletions.
7 changes: 7 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cd ./frontend && npm run lint && npm run test
cd ..

cd ./backend/user-service && npm run lint && npm run test
cd ../..

cd ./backend/question-service && npm run lint && npm run test
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/bzPrOe11)
# CS3219 Project (PeerPrep) - AY2425S1 Group 28

# CS3219 Project (PeerPrep) - AY2425S1
## Setting up

## Group: G28
We will be using Docker to set up PeerPrep. Install Docker [here](https://docs.docker.com/get-started/get-docker).

### Note:
Follow the instructions in the [backend directory](./backend/) first before proceeding.

- You can choose to develop individual microservices within separate folders within this repository **OR** use individual repositories (all public) for each microservice.
- In the latter scenario, you should enable sub-modules on this GitHub classroom repository to manage the development/deployment **AND** add your mentor to the individual repositories as a collaborator.
- The teaching team should be given access to the repositories as we may require viewing the history of the repository in case of any disputes or disagreements.
Run:
```
docker-compose up --build
```

## Useful links

- User Service: http://localhost:3001
- Question Service: http://localhost:3000
- Frontend: http://localhost:5173
16 changes: 16 additions & 0 deletions backend/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Credentials for MongoDB and Mongo Express.
# Create a copy of this file and name it `.env`. Change the values accordingly.

# 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}@mongo:27017/
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
88 changes: 88 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# PeerPrep Backend

> Set-up either a local or cloud MongoDB first, before proceeding to each microservice for more instructions.
## 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` file in the various microservices later on.

5. 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)
1. Visit the MongoDB Atlas Site [https://www.mongodb.com/atlas](https://www.mongodb.com/atlas) and click on "Try Free".

2. Sign Up/Sign In with your preferred method.

3. You will be greeted with welcome screens. Feel free to skip them till you reach the Dashboard page.

4. Create a Database Deployment by clicking on the green `+ Create` Button:

![alt text](./GuideAssets/Creation.png)

5. Make selections as followings:

- Select Shared Cluster
- Select `aws` as Provider

![alt text](./GuideAssets/Selection1.png)

- Select `Singapore` for Region

![alt text](./GuideAssets/Selection2.png)

- Select `M0 Sandbox` Cluster (Free Forever - No Card Required)

> Ensure to select M0 Sandbox, else you may be prompted to enter card details and may be charged!
![alt text](./GuideAssets/Selection3.png)

- Leave `Additional Settings` as it is

- Provide a suitable name to the Cluster

![alt text](./GuideAssets/Selection4.png)


![alt text](./GuideAssets/Security.png)

7. Next, click on `Add my Current IP Address`. This will whitelist your IP address and allow you to connect to the MongoDB Database.

![alt text](./GuideAssets/Network.png)

8. Click `Finish and Close` and the MongoDB Instance should be up and running.

9. [Optional] Whitelisting All IP's

1. Select `Network Access` from the left side pane on Dashboard.
![alt text](./GuideAssets/SidePane.png)

2. Click on the `Add IP Address` Button
![alt text](./GuideAssets/AddIPAddress.png)

3. Select the `ALLOW ACCESS FROM ANYWHERE` Button and Click `Confirm`
![alt text](./GuideAssets/IPWhitelisting.png)

4. Now, any IP Address can access this Database.

10. After setting up, go to the Database Deployment Page. You would see a list of the Databases you have set up. Select `Connect` on the cluster you just created earlier.

![alt text](GuideAssets/ConnectCluster.png)

11. Select the `Drivers` option.

![alt text](GuideAssets/DriverSelection.png)

12. Select `Node.js` in the `Driver` pull-down menu, and copy the connection string.

Notice, you may see `<password>` in this connection string. We will be replacing this with the admin account password that we created earlier on when setting up the Shared Cluster.

![alt text](GuideAssets/ConnectionString.png)

13. Your cloud Mongo URI will be the string you copied earlier. Take note of it as we will be using in the `.env` file in the various microservices later on.
15 changes: 9 additions & 6 deletions backend/question-service/.env.sample
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
MONGO_URI=MONGO_URI
NODE_ENV=development

FIREBASE_PROJECT_ID=FIREBASE_PROJECT_ID
FIREBASE_PRIVATE_KEY=FIREBASE_PRIVATE_KEY
FIREBASE_CLIENT_EMAIL=FIREBASE_CLIENT_EMAIL
FIREBASE_STORAGE_BUCKET=FIREBASE_STORAGE_BUCKET
MONGO_CLOUD_URI=<MONGO_CLOUD_URI>
MONGO_LOCAL_URI=mongodb://root:example@mongo:27017/

FIREBASE_PROJECT_ID=<FIREBASE_PROJECT_ID>
FIREBASE_PRIVATE_KEY=<FIREBASE_PRIVATE_KEY>
FIREBASE_CLIENT_EMAIL=<FIREBASE_CLIENT_EMAIL>
FIREBASE_STORAGE_BUCKET=>FIREBASE_STORAGE_BUCKET>

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

USER_SERVICE_URL=USER_SERVICE_URL
USER_SERVICE_URL=http://user-service:3001/api
62 changes: 29 additions & 33 deletions backend/question-service/README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,6 @@
# Question Service

> 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)
## Setting-up MongoDB

> :notebook: If you are familiar to MongoDB and wish to use a local instance, please feel free to do so. This guide utilizes MongoDB Cloud Services.
1. Set up a MongoDB Shared Cluster by following the steps in this [Guide](../user-service/MongoDBSetup.md).

2. After setting up, go to the Database Deployment Page. You would see a list of the Databases you have set up. Select `Connect` on the cluster you just created earlier.

![alt text](../user-service/GuideAssets/ConnectCluster.png)

3. Select the `Drivers` option, as we have to link to a Node.js App (Question Service).

![alt text](../user-service/GuideAssets/DriverSelection.png)

4. Select `Node.js` in the `Driver` pull-down menu, and copy the connection string.

Notice, you may see `<password>` in this connection string. We will be replacing this with the admin account password that we created earlier on when setting up the Shared Cluster.

![alt text](../user-service/GuideAssets/ConnectionString.png)

5. In the `question-service` directory, create a copy of the `.env.sample` file and name it `.env`.

6. Update the `MONGO_URI` of the `.env` file, and paste the string we copied earlier in step 4.
> If you have not set-up either a local or cloud MongoDB, go [here](../) first before proceding.
## Setting-up Firebase

Expand Down Expand Up @@ -52,13 +28,17 @@

5. Go to `Settings`, `Project settings`, `Service accounts` and click `Generate new private key`. This will download a `.json` file, which will contain your credentials.

6. In `.env` of question service, replace:
- `FIREBASE_PROJECT_ID` with `project_id` found in the downloaded json file.
- `FIREBASE_PRIVATE_KEY` with `private_key` found in the downloaded json file.
- `FIREBASE_CLIENT_EMAIL` with `client_email` found in the downloaded json file.
- `FIREBASE_STORAGE_BUCKET` with the folder path of the Storage. It should look something like `gs://<appname>.appspot.com`.
## Setting-up Question Service

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

## Running Question Service
2. Update `MONGO_CLOUD_URI`, `MONGO_LOCAL_URI`, `FIREBASE_PROJECT_ID`, `FIREBASE_PRIVATE_KEY`, `FIREBASE_CLIENT_EMAIL`, `FIREBASE_STORAGE_BUCKET`.
- `FIREBASE_PROJECT_ID` is the value of `project_id` found in the downloaded json file.
- `FIREBASE_PRIVATE_KEY` is the value of `private_key` found in the downloaded json file.
- `FIREBASE_CLIENT_EMAIL` is the value of `client_email` found in the downloaded json file.
- `FIREBASE_STORAGE_BUCKET` is the folder path of the Storage. It should look something like `gs://<appname>.appspot.com`.

## Running Question Service without Docker

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

Expand All @@ -68,6 +48,22 @@

4. Run the command `npm start` to start the Question Service in production mode, or use `npm run dev` for development mode, which includes features like automatic server restart when you make code changes.

5. To view Question Service documentation, go to http://localhost:3000/docs.
## Seeding questions into MongoDB

1. With Docker

- Run `docker ps` to get a list of the Docker containers on your machine.

- Retrieve the `CONTAINER_ID` of `peerprep/question-service`.

- Run `docker exec -it <CONTAINER_ID> npm run seed`.

2. Without Docker

- Run `npm run seed`.

## After running

1. To view Question Service documentation, go to http://localhost:3000/docs.

6. Using applications like Postman, you can interact with the Question Service on port 3000. If you wish to change this, please update the `.env` file.
2. Using applications like Postman, you can interact with the Question Service on port 3000. If you wish to change this, please update the `.env` file.
5 changes: 0 additions & 5 deletions backend/question-service/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import yaml from "yaml";
import fs from "fs";
import cors from "cors";

import connectDB from "./config/db.ts";
import questionRoutes from "./src/routes/questionRoutes.ts";

dotenv.config();
Expand All @@ -19,10 +18,6 @@ const swaggerDocument = yaml.parse(file);

const app = express();

if (process.env.NODE_ENV !== "test") {
connectDB();
}

app.use(cors({ origin: allowedOrigins, credentials: true }));
app.options("*", cors({ origin: allowedOrigins, credentials: true }));

Expand Down
11 changes: 8 additions & 3 deletions backend/question-service/config/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ dotenv.config();

const connectDB = async () => {
try {
if (process.env.MONGO_URI == undefined) {
throw new Error("MONGO_URI is undefined");
const mongoDBUri: string | undefined =
process.env.NODE_ENV === "production"
? process.env.MONGO_CLOUD_URI
: process.env.MONGO_LOCAL_URI;

if (!mongoDBUri) {
throw new Error("MongoDB URI is not provided");
}

await mongoose.connect(process.env.MONGO_URI);
await mongoose.connect(mongoDBUri);
console.log("MongoDB connected");
} catch (error) {
console.error(error);
Expand Down
3 changes: 3 additions & 0 deletions backend/question-service/config/firebase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import admin from "firebase-admin";
import dotenv from "dotenv";

dotenv.config();

admin.initializeApp({
credential: admin.credential.cert({
Expand Down
19 changes: 19 additions & 0 deletions backend/question-service/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions backend/question-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
"main": "server.ts",
"type": "module",
"scripts": {
"seed": "tsx src/scripts/seed.ts",
"start": "tsx server.ts",
"dev": "tsx watch server.ts",
"test": "export NODE_ENV=test && jest",
"test:watch": "export NODE_ENV=test && jest --watch",
"test": "cross-env NODE_ENV=test && jest",
"test:watch": "cross-env NODE_ENV=test && jest --watch",
"lint": "eslint ."
},
"author": "",
Expand Down Expand Up @@ -38,6 +39,7 @@
"@types/supertest": "^6.0.2",
"@types/swagger-ui-express": "^4.1.6",
"@types/uuid": "^10.0.0",
"cross-env": "^7.0.3",
"eslint": "^9.11.1",
"globals": "^15.9.0",
"jest": "^29.7.0",
Expand Down
18 changes: 15 additions & 3 deletions backend/question-service/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import app from "./app.ts";
import connectDB from "./config/db.ts";

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

app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
if (process.env.NODE_ENV !== "test") {
connectDB()
.then(() => {
console.log("MongoDB connected");

app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
})
.catch((err) => {
console.error("Failed to connect to DB");
console.error(err);
});
}
Loading

0 comments on commit cbaf0ad

Please sign in to comment.