Skip to content

Commit

Permalink
Improve README and docker compose files (#43)
Browse files Browse the repository at this point in the history
* Split compose file into two

* Split `docker-compose.yml` into `compose.yml` and `compose.dev.yml`
* Ensure all containers restart
* Unmount volumes for base `compose.yml`
* Publish backend service ports only for `compose.dev.yml`
* Configure backend services to have both "start" and "dev" scripts

* Standardise port numbers

* Ensure correct dev command

* Skip mongo startup messages

* README: Improve clarity

* Fix typo

* Compose: Fix ports

* Ensure backend services can communicate
* Remove useless networks
* Due to the lack of API gateway, all backend services currently need to be exposed directly for now.
  • Loading branch information
samuelim01 authored Sep 29, 2024
1 parent e011fba commit f906bf1
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 37 deletions.
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.

**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.
21 changes: 21 additions & 0 deletions compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
services:
frontend:
volumes:
- /app/node_modules
- ./frontend:/app

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

question-db:
ports:
- 27017:27017

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

question:
container_name: question
Expand All @@ -27,13 +22,9 @@ services:
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 +37,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 @@ -55,26 +47,17 @@ services:
context: services/user
dockerfile: Dockerfile
ports:
- 3001
- 8082:8082
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

volumes:
question-db:

networks:
question-network:
driver: bridge
question-db-network:
driver: bridge
user-network:
driver: bridge
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

0 comments on commit f906bf1

Please sign in to comment.