Skip to content

Commit

Permalink
Assignment 4
Browse files Browse the repository at this point in the history
  • Loading branch information
loyhongshenggg committed Oct 30, 2023
1 parent 71957f2 commit 4f52a4d
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 13 deletions.
5 changes: 2 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# Setting up PeerPrep

1. Install docker desktop
2. Use readme in /user-service to setup user service
3. Use readme in /questionapi to setup question service
4. Use readme in /client to setup client
2. Run docker compose up -d in project root directory
5. Once the container is built, the application should be running on localhost:3002


# Help
* Give the containers sometime to start up
* Delete the docker containers and images and rebuild the containers
Expand Down
8 changes: 8 additions & 0 deletions client/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM node:16-alpine as builder
WORKDIR './client'
COPY ./package.json ./
RUN npm install
COPY . .
# RUN npm run build // only use for deployment
EXPOSE 3002
CMD ["npm", "start"]
4 changes: 2 additions & 2 deletions client/src/pages/Authentication/Register.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ export default function Register() {
const handleInputChange = (event) => {
const newValue = event.target.value;


// Define a regular expression pattern to match your criteria
const pattern = /^[a-z0-9._-]{1,30}$/;


// Test if the input value matches the pattern
if (!pattern.test(newValue)) {
setErrorUsername("Use only lowercase a-z, 0-9, ., _ or -");

Expand Down
56 changes: 56 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
services:
user-service:
build:
context: ./user-service
dockerfile: Dockerfile.userservice-backend
ports:
- "3000:3000"
env_file:
- ./user-service/.env
depends_on:
user-service-db:
condition: service_healthy
restart: on-failure

user-service-db:
build:
context: ./user-service
dockerfile: Dockerfile.postgres-db
env_file:
- ./user-service/.env
healthcheck:
test: [ "CMD-SHELL", "pg_isready -U postgres" ]
interval: 5s
timeout: 3s
retries: 3
restart: on-failure

question-service:
build: ./question-service
ports:
- "3001:3001"
depends_on:
- user-service
env_file:
- ./question-service/.env
restart: on-failure

client:
build: ./client
ports:
- "3002:3002"
depends_on:
- user-service
- question-service
restart: on-failure
# nginx-api-gateway:
# build: ./nginx
# ports:
# - "80:80"
# depends_on:
# - user-service
# - question-service
# - matching-service
# - collaboration-service
# - client
# restart: on-failure
11 changes: 11 additions & 0 deletions question-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM alpine:latest
RUN apk update && \
apk add nodejs npm
RUN npm install -g nodemon
WORKDIR '/question-service'
COPY package*.json ./
RUN npm install
# RUN npm ci --omit=dev
COPY . .
EXPOSE 3001
CMD [ "nodemon", "index.js" ]
14 changes: 11 additions & 3 deletions question-service/readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# Starting sev (Dev mode)
# MongoDB question-service setup using docker

1. Run `npm i` => install all dependencies from package.json
2. Run `nodemon index.js` => hot reloads the event loop
1. Install docker desktop
2. In the root dir question-service/ run `docker build . -t <docker-username>/question-api`
3. Amend container name and port as needed and run the command
`docker run -p 3001:3001 -d <docker-username>/question-api`

Note: To access docker container:
Use the following link: `http://localhost:3001/`
You should see the following display: `You are on the question api service!`

# Starting sev (Dev mode)

1. Run `npm i` => install all dependencies from package.json
2. Run `nodemon index.js` => hot reloads the event loop
4 changes: 2 additions & 2 deletions user-service/.env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
DB_USER=postgres
POSTGRES_PASSWORD=<fill in>
DB_ADDR=localhost
DB_PORT=1111
DB_ADDR=user-service-db
DB_PORT=5432
DB_NAME=userservice
JWT_SECRET=<fill in>
ADMIN_PASSWORD=<admin password to login to db fill in>
11 changes: 11 additions & 0 deletions user-service/Dockerfile.userservice-backend
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM node:18
WORKDIR /userservice
COPY package*.json ./
RUN npm i
COPY app.ts ./
COPY tsconfig.json ./
WORKDIR /userservice/src
COPY src ./
EXPOSE 3000
RUN npm run build
CMD ["npm", "run", "start:prod"]
14 changes: 11 additions & 3 deletions user-service/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@

1. Install docker desktop
2. In the root dir user-service/ run `docker build -f Dockerfile.postgres-db -t user-service-psql .`
3. Amend the password, container name and port as needed and run the command
`docker run -p 1111:5432 -e POSTGRES_PASSWORD=<POSTGRES_PASSWORD> --name user-service-db -d user-service-psql`
3. Create user-service container
`docker build -f Dockerfile.userservice-backend -t user-service-backend .`
4. Create network for user-service
`docker network create user-service`
5. Start DB container from image - replace fields
`docker run --env-file=.env --name user-service-db -d --network=user-service user-service-psql`
6. Get DB container IP - copy this value into DB_ADDR in .env
`docker inspect user-service-db | grep IPAddress`
7. Start user-service container from image
`docker run -p 3000:3000 --env-file=.env --name user-service-backend -d --network=user-service user-service-backend`

# Setup

Expand All @@ -25,4 +33,4 @@
### Admin account

Username: [email protected]
password: Set by ADMIN_PASSWORD field
password: Set by ADMIN_PASSWORD field

0 comments on commit 4f52a4d

Please sign in to comment.