Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve README and docker compose files #43

Merged
merged 7 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,64 @@
### Note:
- 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.
- 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.

## Pre-requisites

1. Install [Docker Desktop](https://www.docker.com/products/docker-desktop/)
2. Clone the GitHub repository
```
git clone https://github.com/CS3219-AY2425S1/cs3219-ay2425s1-project-g03.git
```

## Getting Started

**Step 1: Copy Environment Configuration File**

To get started, copy the contents of `.env.sample` into a new `.env.` file located at the root level of the project.
samuelim01 marked this conversation as resolved.
Show resolved Hide resolved

**Step 2: Build the Docker containers**

Next, run the following command to build the Docker containers.

```bash
docker compose -f compose.yml build --no-cache
```

**Step 3: Start the Docker containers**

Once the build is complete, you can start the Docker containers.

```bash
docker compose -f compose.yml up -d
```

After spinning up the services, you may access the frontend client at `127.0.0.1:4200`. Specifically, you can navigate to the Question SPA at `127.0.0.1:4200/questions` and the login page at `127.0.0.1/account`.

If you would like to spin up the services in development mode, you may use the following command. This enables hot reloading and exposes the ports for all microservices.

```bash
docker compose -f compose.yml -f compose.dev.yml up -d
```

| Service | Port |
|-----------------------|------|
| Frontend | 4200 |
| API Gateway | 8080 |
| Question Service | 8081 |
| User Service | 8082 |
| Match Service | 8083 |
| Collaboration Service | 8084 |
| Chat Service | 8085 |
| History Service | 8086 |


**Step 4: Stop the Docker containers**

Once you are done, stop and remove the containers using:

```bash
docker compose down -v
```

Note that this will clear any data stored in volumes associated with the containers. If you would like to keep your data, you can run the command without the `-v` flag, which will remove the containers but retain the data in the volumes for future use.
25 changes: 25 additions & 0 deletions compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
services:
frontend:
volumes:
- /app/node_modules
- ./frontend:/app

question:
command: npm run dev
ports:
- 8081:8081
volumes:
- /app/node_modules
- ./services/question:/app

question-db:
ports:
- 27017:27017

user:
command: npm run dev
ports:
- 8082:8082
volumes:
- /app/node_modules
- ./services/user:/app
16 changes: 3 additions & 13 deletions docker-compose.yml → compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,27 @@ services:
dockerfile: Dockerfile
ports:
- 4200:4200
volumes:
- /app/node_modules
- ./frontend:/app
networks:
- question-network
- user-network
restart: always

question:
container_name: question
image: question
build:
context: services/question
dockerfile: Dockerfile
ports:
- 8081:8081
environment:
DB_CLOUD_URI: ${QUESTION_DB_CLOUD_URI}
DB_LOCAL_URI: ${QUESTION_DB_LOCAL_URI}
DB_USERNAME: ${QUESTION_DB_USERNAME}
DB_PASSWORD: ${QUESTION_DB_PASSWORD}
volumes:
- /app/node_modules
- ./services/question:/app
networks:
- question-network
- question-db-network
- user-network
restart: always

question-db:
container_name: question-db
Expand All @@ -46,6 +40,7 @@ services:
- ./services/question/init-mongo/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js
networks:
- question-db-network
command: --quiet
restart: always

user:
Expand All @@ -54,16 +49,11 @@ services:
build:
context: services/user
dockerfile: Dockerfile
ports:
- 3001
environment:
USER_SERVICE_CLOUD_URI: ${USER_SERVICE_CLOUD_URI}
USER_SERVICE_LOCAL_URI: ${USER_SERVICE_LOCAL_URI}
ENV: ${ENV}
JWT_SECRET: ${JWT_SECRET}
volumes:
- /app/node_modules
- ./services/user:/app
networks:
- user-network
restart: always
Expand Down
3 changes: 2 additions & 1 deletion services/question/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"main": "index.js",
"scripts": {
"build": "npx tsc",
"start": "nodemon src/index.ts",
"start": "npm run build && node dist/index.js",
"dev": "nodemon src/index.ts",
"lint": "npx eslint .",
"lint:fix": "npx eslint . --fix"
},
Expand Down
1 change: 1 addition & 0 deletions services/question/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"compilerOptions": {
"target": "es2016",
"module": "CommonJS",
"rootDir": "./src",
"outDir": "./dist",
"strict": true,
"esModuleInterop": true,
Expand Down
2 changes: 1 addition & 1 deletion services/user/.env.sample
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# User Service
USER_SERVICE_CLOUD_URI=<cloud_uri>
USER_SERVICE_LOCAL_URI=mongodb://127.0.0.1:27017/peerprepUserServiceDB
PORT=3001
PORT=8082

# Will use cloud MongoDB Atlas database
ENV=PROD
Expand Down
2 changes: 1 addition & 1 deletion services/user/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install
COPY . .
EXPOSE 3001
EXPOSE 8082

CMD ["npm", "start"]
20 changes: 10 additions & 10 deletions services/user/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

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

4. Using applications like Postman, you can interact with the User Service on port 3001. If you wish to change this, please update the `.env` file.
4. Using applications like Postman, you can interact with the User Service on port 8082. If you wish to change this, please update the `.env` file.

## User Service API Guide

Expand All @@ -42,7 +42,7 @@

- HTTP Method: `POST`

- Endpoint: http://localhost:3001/users
- Endpoint: http://localhost:8082/users

- Body
- Required: `username` (string), `email` (string), `password` (string)
Expand Down Expand Up @@ -72,11 +72,11 @@

- HTTP Method: `GET`

- Endpoint: http://localhost:3001/users/{userId}
- Endpoint: http://localhost:8082/users/{userId}

- Parameters
- Required: `userId` path parameter
- Example: `http://localhost:3001/users/60c72b2f9b1d4c3a2e5f8b4c`
- Example: `http://localhost:8082/users/60c72b2f9b1d4c3a2e5f8b4c`

- <a name="auth-header">Headers</a>

Expand Down Expand Up @@ -104,7 +104,7 @@

- This endpoint allows retrieval of all users' data from the database.
- HTTP Method: `GET`
- Endpoint: http://localhost:3001/users
- Endpoint: http://localhost:8082/users
- Headers
- Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>`
- Auth Rules:
Expand All @@ -128,7 +128,7 @@

- HTTP Method: `PATCH`

- Endpoint: http://localhost:3001/users/{userId}
- Endpoint: http://localhost:8082/users/{userId}

- Parameters
- Required: `userId` path parameter
Expand Down Expand Up @@ -170,7 +170,7 @@

- HTTP Method: `PATCH`

- Endpoint: http://localhost:3001/users/{userId}
- Endpoint: http://localhost:8082/users/{userId}

- Parameters
- Required: `userId` path parameter
Expand Down Expand Up @@ -208,7 +208,7 @@

- This endpoint allows deletion of a user and their related data from the database using the user's ID.
- HTTP Method: `DELETE`
- Endpoint: http://localhost:3001/users/{userId}
- Endpoint: http://localhost:8082/users/{userId}
- Parameters

- Required: `userId` path parameter
Expand All @@ -235,7 +235,7 @@

- This endpoint allows a user to authenticate with an email and password and returns a JWT access token. The token is valid for 1 day and can be used subsequently to access protected resources. For example usage, refer to the [Authorization header section in the Get User endpoint](#auth-header).
- HTTP Method: `POST`
- Endpoint: http://localhost:3001/auth/login
- Endpoint: http://localhost:8082/auth/login
- Body
- Required: `username` (string), `password` (string)

Expand All @@ -259,7 +259,7 @@

- This endpoint allows one to verify a JWT access token to authenticate and retrieve the user's data associated with the token.
- HTTP Method: `GET`
- Endpoint: http://localhost:3001/auth/verify-token
- Endpoint: http://localhost:8082/auth/verify-token
- Headers
- Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>`

Expand Down
2 changes: 1 addition & 1 deletion services/user/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"main": "./src/index.js",
"type": "module",
"scripts": {
"dev": "nodemon ./src/server.js",
"start": "node ./src/server.js",
"dev": "nodemon ./src/server.js",
"test": "mocha --require ts-node/register tests/**/*.spec.ts --exit"
},
"keywords": [],
Expand Down
2 changes: 1 addition & 1 deletion services/user/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import index from "./index.js";
import "dotenv/config";
import { connectToDB } from "./model/repository.js";

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

const server = http.createServer(index);

Expand Down