Skip to content

Commit f906bf1

Browse files
authored
Improve README and docker compose files (#43)
* 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.
1 parent e011fba commit f906bf1

File tree

10 files changed

+103
-37
lines changed

10 files changed

+103
-37
lines changed

README.md

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,64 @@
55
### Note:
66
- You can choose to develop individual microservices within separate folders within this repository **OR** use individual repositories (all public) for each microservice.
77
- 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.
8-
- 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.
8+
- 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.
9+
10+
## Pre-requisites
11+
12+
1. Install [Docker Desktop](https://www.docker.com/products/docker-desktop/)
13+
2. Clone the GitHub repository
14+
```
15+
git clone https://github.com/CS3219-AY2425S1/cs3219-ay2425s1-project-g03.git
16+
```
17+
18+
## Getting Started
19+
20+
**Step 1: Copy Environment Configuration File**
21+
22+
To get started, copy the contents of `.env.sample` into a new `.env` file located at the root level of the project.
23+
24+
**Step 2: Build the Docker containers**
25+
26+
Next, run the following command to build the Docker containers.
27+
28+
```bash
29+
docker compose -f compose.yml build --no-cache
30+
```
31+
32+
**Step 3: Start the Docker containers**
33+
34+
Once the build is complete, you can start the Docker containers.
35+
36+
```bash
37+
docker compose -f compose.yml up -d
38+
```
39+
40+
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`.
41+
42+
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.
43+
44+
```bash
45+
docker compose -f compose.yml -f compose.dev.yml up -d
46+
```
47+
48+
| Service | Port |
49+
|-----------------------|------|
50+
| Frontend | 4200 |
51+
| API Gateway | 8080 |
52+
| Question Service | 8081 |
53+
| User Service | 8082 |
54+
| Match Service | 8083 |
55+
| Collaboration Service | 8084 |
56+
| Chat Service | 8085 |
57+
| History Service | 8086 |
58+
59+
60+
**Step 4: Stop the Docker containers**
61+
62+
Once you are done, stop and remove the containers using:
63+
64+
```bash
65+
docker compose down -v
66+
```
67+
68+
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.

compose.dev.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
services:
2+
frontend:
3+
volumes:
4+
- /app/node_modules
5+
- ./frontend:/app
6+
7+
question:
8+
command: npm run dev
9+
volumes:
10+
- /app/node_modules
11+
- ./services/question:/app
12+
13+
question-db:
14+
ports:
15+
- 27017:27017
16+
17+
user:
18+
command: npm run dev
19+
volumes:
20+
- /app/node_modules
21+
- ./services/user:/app

docker-compose.yml renamed to compose.yml

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@ services:
77
dockerfile: Dockerfile
88
ports:
99
- 4200:4200
10-
volumes:
11-
- /app/node_modules
12-
- ./frontend:/app
13-
networks:
14-
- question-network
15-
- user-network
10+
restart: always
1611

1712
question:
1813
container_name: question
@@ -27,13 +22,9 @@ services:
2722
DB_LOCAL_URI: ${QUESTION_DB_LOCAL_URI}
2823
DB_USERNAME: ${QUESTION_DB_USERNAME}
2924
DB_PASSWORD: ${QUESTION_DB_PASSWORD}
30-
volumes:
31-
- /app/node_modules
32-
- ./services/question:/app
3325
networks:
34-
- question-network
3526
- question-db-network
36-
- user-network
27+
restart: always
3728

3829
question-db:
3930
container_name: question-db
@@ -46,6 +37,7 @@ services:
4637
- ./services/question/init-mongo/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js
4738
networks:
4839
- question-db-network
40+
command: --quiet
4941
restart: always
5042

5143
user:
@@ -55,26 +47,17 @@ services:
5547
context: services/user
5648
dockerfile: Dockerfile
5749
ports:
58-
- 3001
50+
- 8082:8082
5951
environment:
6052
USER_SERVICE_CLOUD_URI: ${USER_SERVICE_CLOUD_URI}
6153
USER_SERVICE_LOCAL_URI: ${USER_SERVICE_LOCAL_URI}
6254
ENV: ${ENV}
6355
JWT_SECRET: ${JWT_SECRET}
64-
volumes:
65-
- /app/node_modules
66-
- ./services/user:/app
67-
networks:
68-
- user-network
6956
restart: always
7057

7158
volumes:
7259
question-db:
7360

7461
networks:
75-
question-network:
76-
driver: bridge
7762
question-db-network:
78-
driver: bridge
79-
user-network:
8063
driver: bridge

services/question/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"main": "index.js",
55
"scripts": {
66
"build": "npx tsc",
7-
"start": "nodemon src/index.ts",
7+
"start": "npm run build && node dist/index.js",
8+
"dev": "nodemon src/index.ts",
89
"lint": "npx eslint .",
910
"lint:fix": "npx eslint . --fix"
1011
},

services/question/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"compilerOptions": {
44
"target": "es2016",
55
"module": "CommonJS",
6+
"rootDir": "./src",
67
"outDir": "./dist",
78
"strict": true,
89
"esModuleInterop": true,

services/user/.env.sample

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# User Service
22
USER_SERVICE_CLOUD_URI=<cloud_uri>
33
USER_SERVICE_LOCAL_URI=mongodb://127.0.0.1:27017/peerprepUserServiceDB
4-
PORT=3001
4+
PORT=8082
55

66
# Will use cloud MongoDB Atlas database
77
ENV=PROD

services/user/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ WORKDIR /app
44
COPY package.json package-lock.json ./
55
RUN npm install
66
COPY . .
7-
EXPOSE 3001
7+
EXPOSE 8082
88

99
CMD ["npm", "start"]

services/user/README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
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.
3434

35-
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.
35+
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.
3636

3737
## User Service API Guide
3838

@@ -42,7 +42,7 @@
4242

4343
- HTTP Method: `POST`
4444

45-
- Endpoint: http://localhost:3001/users
45+
- Endpoint: http://localhost:8082/users
4646

4747
- Body
4848
- Required: `username` (string), `email` (string), `password` (string)
@@ -72,11 +72,11 @@
7272

7373
- HTTP Method: `GET`
7474

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

7777
- Parameters
7878
- Required: `userId` path parameter
79-
- Example: `http://localhost:3001/users/60c72b2f9b1d4c3a2e5f8b4c`
79+
- Example: `http://localhost:8082/users/60c72b2f9b1d4c3a2e5f8b4c`
8080

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

@@ -104,7 +104,7 @@
104104

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

129129
- HTTP Method: `PATCH`
130130

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

133133
- Parameters
134134
- Required: `userId` path parameter
@@ -170,7 +170,7 @@
170170

171171
- HTTP Method: `PATCH`
172172

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

175175
- Parameters
176176
- Required: `userId` path parameter
@@ -208,7 +208,7 @@
208208

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

214214
- Required: `userId` path parameter
@@ -235,7 +235,7 @@
235235

236236
- 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).
237237
- HTTP Method: `POST`
238-
- Endpoint: http://localhost:3001/auth/login
238+
- Endpoint: http://localhost:8082/auth/login
239239
- Body
240240
- Required: `username` (string), `password` (string)
241241

@@ -259,7 +259,7 @@
259259

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

services/user/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"main": "./src/index.js",
66
"type": "module",
77
"scripts": {
8-
"dev": "nodemon ./src/server.js",
98
"start": "node ./src/server.js",
9+
"dev": "nodemon ./src/server.js",
1010
"test": "mocha --require ts-node/register tests/**/*.spec.ts --exit"
1111
},
1212
"keywords": [],

services/user/src/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import index from "./index.js";
33
import "dotenv/config";
44
import { connectToDB } from "./model/repository.js";
55

6-
const port = process.env.PORT || 3001;
6+
const port = process.env.PORT || 8082;
77

88
const server = http.createServer(index);
99

0 commit comments

Comments
 (0)