Skip to content

Commit b7fee12

Browse files
Merge pull request #44 from CS3219-AY2425S1/frontend-websocket
Frontend websocket with docker
2 parents 443b7de + d3caf57 commit b7fee12

File tree

5 files changed

+80
-6
lines changed

5 files changed

+80
-6
lines changed

apps/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,21 @@ In the `./apps` directory:
1616
```plaintext
1717
.
1818
├── docker-compose.yml # Docker Compose configuration
19+
├── README.md # Project documentation (for docker compose)
1920
├── .env # Global environment variables (optional)
2021
├── frontend
2122
│ ├── Dockerfile # Dockerfile for frontend
2223
│ └── ... (other frontend files)
24+
├── matching-service
25+
│ ├── Dockerfile # Dockerfile for matching-service
26+
│ └── ... (other matching-service files)
2327
├── question-service
2428
│ ├── Dockerfile # Dockerfile for question-service
2529
│ └── ... (other question-service files)
2630
├── user-service
2731
│ ├── Dockerfile # Dockerfile for user-service
2832
│ └── ... (other user-service files)
29-
└── README.md # Project documentation (for docker compose)
33+
3034
```
3135

3236
## Docker Compose Setup
@@ -53,6 +57,8 @@ Once running, you can access:
5357
- The **frontend** at http://localhost:3000
5458
- The **user service** at http://localhost:3001
5559
- The **question service** at http://localhost:8080
60+
- The **matching service** at http://localhost:8081
61+
- The **redis service** at http://localhost:6379
5662

5763
3. Stopping Services
5864

apps/docker-compose.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,28 @@ services:
3838
volumes:
3939
- ./question-service:/question-service
4040

41+
matching-service:
42+
build:
43+
context: ./matching-service
44+
dockerfile: Dockerfile
45+
ports:
46+
- 8081:8081
47+
env_file:
48+
- ./matching-service/.env
49+
networks:
50+
- apps_network
51+
volumes:
52+
- ./matching-service:/matching-service
53+
depends_on:
54+
- redis
55+
56+
redis:
57+
image: redis:latest
58+
networks:
59+
- apps_network
60+
ports:
61+
- 6379:6379
62+
container_name: redis-container
63+
4164
networks:
4265
apps_network:

apps/matching-service/.env.example

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
PORT=8081
2-
MATCH_TIMEOUT=10
2+
MATCH_TIMEOUT=30
33
JWT_SECRET=you-can-replace-this-with-your-own-secret
4-
REDIS_URL=localhost:6379
4+
5+
# if you are NOT USING docker, use the below url
6+
REDIS_URL=localhost:6379
7+
8+
# if you are USING docker, use the below url
9+
# REDIS_URL=redis-container:6379

apps/matching-service/Dockerfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM golang:1.23
2+
3+
WORKDIR /usr/src/app
4+
5+
# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
6+
COPY go.mod go.sum ./
7+
8+
RUN go mod tidy && go mod download && go mod verify
9+
10+
COPY .env /usr/src/app/.env
11+
12+
COPY . .
13+
14+
RUN go build -v -o /usr/local/bin/app ./main.go
15+
16+
EXPOSE 8081
17+
18+
CMD ["app"]

apps/matching-service/README.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ go mod tidy
2727
- `PORT`: Specifies the port for the WebSocket server. Default is `8081`.
2828
- `JWT_SECRET`: The secret key used to verify JWT tokens.
2929
- `MATCH_TIMEOUT`: The time in seconds to wait for a match before timing out.
30-
- `REDIS_URL`: The URL for the Redis server. Default is `localhost:6379`.
30+
- `REDIS_URL`: The URL for the Redis server. Default is `localhost:6379`. If you are using docker, use `redis-container:6379`
3131

3232
4. Start a local Redis server:
3333

@@ -121,6 +121,28 @@ Make sure to open the HTML file in a web browser while the WebSocket server is r
121121

122122
You can open one instance of the HTML file in multiple tabs to simulate multiple clients connecting to the server. (In the future: ensure that only one connection is allowed per user)
123123

124-
## Docker Support
124+
## Running the Application via Docker
125125

126-
TODO: Add section for Docker setup and usage instructions.
126+
Before running the following commands, ensure that the URL for the Redis server in `.env` file has been changed to `REDIS_URL=redis-container:6379`
127+
128+
To run the application via Docker, run the following command:
129+
130+
1. Set up the Go Docker container for the matching service
131+
```bash
132+
docker build -f Dockerfile -t match-go-app .
133+
```
134+
135+
2. Create the Docker network for Redis and Go
136+
```bash
137+
docker network create redis-go-network
138+
```
139+
140+
3. Start a new Redis container in detached mode using the Redis image from Docker Hub
141+
```bash
142+
docker run -d --name redis-container --network redis-go-network redis
143+
```
144+
145+
4. Run the Go Docker container for the matching-service
146+
```bash
147+
docker run -d -p 8081:8081 --name go-app-container --network redis-go-network match-go-app
148+
```

0 commit comments

Comments
 (0)