-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from guanquann/mongodb-docker
Add mongodb to Docker + Fix bugs
- Loading branch information
Showing
41 changed files
with
511 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
Oops, something went wrong.