From 6b97a2dd7387ab918b5c9ff165758721c4c0e952 Mon Sep 17 00:00:00 2001 From: nknguyenhc <87511888+nknguyenhc@users.noreply.github.com> Date: Tue, 1 Oct 2024 21:12:14 +0800 Subject: [PATCH] Add docker compose & adjust ports of services --- backend/question-service/.env.sample | 4 ++- backend/question-service/package-lock.json | 31 ++++++++++++++++++++++ backend/question-service/package.json | 2 ++ backend/question-service/src/server.ts | 6 ++++- backend/user-service/.env.sample | 3 +++ backend/user-service/index.js | 4 ++- docker-compose.yml | 10 +++++++ frontend/.env.sample | 4 ++- frontend/src/services/question.service.ts | 15 ++++++++--- 9 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 docker-compose.yml diff --git a/backend/question-service/.env.sample b/backend/question-service/.env.sample index be0283230e..09a5cd7950 100644 --- a/backend/question-service/.env.sample +++ b/backend/question-service/.env.sample @@ -1,6 +1,8 @@ DB_CLOUD_URI= DB_LOCAL_URI="mongodb://127.0.0.1:27017/peerprepQuestionServiceDB" -PORT=3000 +PORT=3002 # Will use cloud MongoDB Atlas database ENV=PROD + +FRONTEND_URL=http://localhost:3000 diff --git a/backend/question-service/package-lock.json b/backend/question-service/package-lock.json index 75dc8dc5db..9ced8a2544 100644 --- a/backend/question-service/package-lock.json +++ b/backend/question-service/package-lock.json @@ -10,11 +10,13 @@ "license": "ISC", "dependencies": { "body-parser": "^1.20.3", + "cors": "^2.8.5", "dotenv": "^16.4.5", "express": "^4.21.0", "mongoose": "^8.6.3" }, "devDependencies": { + "@types/cors": "^2.8.17", "@types/express": "^4.17.21", "@types/mongoose": "^5.11.97", "@types/node": "^22.5.5", @@ -111,6 +113,15 @@ "@types/node": "*" } }, + "node_modules/@types/cors": { + "version": "2.8.17", + "resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.17.tgz", + "integrity": "sha512-8CGDvrBj1zgo2qE+oS3pOCyYNqCPryMWY2bGfwA0dcfopWGgxs+78df0Rs3rc9THP4JkOhLsAa+15VdpAqkcUA==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/express": { "version": "4.17.21", "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.21.tgz", @@ -438,6 +449,18 @@ "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==" }, + "node_modules/cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "dependencies": { + "object-assign": "^4", + "vary": "^1" + }, + "engines": { + "node": ">= 0.10" + } + }, "node_modules/create-require": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz", @@ -1132,6 +1155,14 @@ "node": ">=0.10.0" } }, + "node_modules/object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/object-inspect": { "version": "1.13.2", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.2.tgz", diff --git a/backend/question-service/package.json b/backend/question-service/package.json index c4a18ba18a..a6cbb9b702 100644 --- a/backend/question-service/package.json +++ b/backend/question-service/package.json @@ -13,11 +13,13 @@ "license": "ISC", "dependencies": { "body-parser": "^1.20.3", + "cors": "^2.8.5", "dotenv": "^16.4.5", "express": "^4.21.0", "mongoose": "^8.6.3" }, "devDependencies": { + "@types/cors": "^2.8.17", "@types/express": "^4.17.21", "@types/mongoose": "^5.11.97", "@types/node": "^22.5.5", diff --git a/backend/question-service/src/server.ts b/backend/question-service/src/server.ts index db782c7284..a6001b7c7f 100644 --- a/backend/question-service/src/server.ts +++ b/backend/question-service/src/server.ts @@ -1,5 +1,6 @@ import express, { Application } from "express"; import bodyParser from "body-parser"; +import cors from "cors"; import questionRoutes from "./routes/questionRoutes"; import { connectToDB } from "./util/db"; import dotenv from "dotenv"; @@ -8,10 +9,13 @@ import dotenv from "dotenv"; const app: Application = express(); dotenv.config(); -const PORT = process.env.PORT || 3000; +const PORT = process.env.PORT || 3002; // Middleware app.use(bodyParser.json()); +app.use(cors({ + origin: process.env.FRONTEND_URL as string, +})); // Routes app.use("/api/questions", questionRoutes); diff --git a/backend/user-service/.env.sample b/backend/user-service/.env.sample index b3518e0224..9ee7a11730 100644 --- a/backend/user-service/.env.sample +++ b/backend/user-service/.env.sample @@ -7,3 +7,6 @@ ENV=PROD # Secret for creating JWT signature JWT_SECRET=you-can-replace-this-with-your-own-secret + +# Frontend URL +FRONTEND_URL=http://localhost:3000 diff --git a/backend/user-service/index.js b/backend/user-service/index.js index 24a5835874..cedc894749 100644 --- a/backend/user-service/index.js +++ b/backend/user-service/index.js @@ -8,7 +8,9 @@ const app = express(); app.use(express.urlencoded({ extended: true })); app.use(express.json()); -app.use(cors()); // config cors so that front-end can use +app.use(cors({ + origin: process.env.FRONTEND_URL, +})); app.options("*", cors()); // To handle CORS Errors diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000000..6df7fd0d3a --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,10 @@ +services: + question: + build: ./backend/question-service + ports: + - "3002:3002" + + user: + build: ./backend/user-service + ports: + - "3001:3001" diff --git a/frontend/.env.sample b/frontend/.env.sample index 861b536c54..982b9fb6e0 100644 --- a/frontend/.env.sample +++ b/frontend/.env.sample @@ -1 +1,3 @@ -port=3001 \ No newline at end of file +PORT=3000 +REACT_APP_QUESTION_SERVICE_URL=http://localhost:3002 +REACT_APP_USER_SERVICE_URL=http://localhost:3001 diff --git a/frontend/src/services/question.service.ts b/frontend/src/services/question.service.ts index c5b7ff4518..dee3b9f88b 100644 --- a/frontend/src/services/question.service.ts +++ b/frontend/src/services/question.service.ts @@ -3,8 +3,15 @@ import { Question, QuestionComplexity } from "../models/question.model"; import { verifyNewQuestion } from "../util/question.helper"; export default class QuestionService { + private static client = axios.create({ + baseURL: process.env.REACT_APP_QUESTION_SERVICE_URL as string, + headers: { + "Content-type": "application/json", + }, + }); + static async getQuestions(): Promise { - const response = await axios.get("/api/questions"); + const response = await QuestionService.client.get("/api/questions"); return response.data; } @@ -17,7 +24,7 @@ export default class QuestionService { link: string, ): Promise { const body = verifyNewQuestion(id, title, description, categoriesString, complexity, link); - const response = await axios.post("/api/questions", body); + const response = await QuestionService.client.post("/api/questions", body); return response.data; } @@ -30,12 +37,12 @@ export default class QuestionService { link: string, ): Promise { const body = verifyNewQuestion(id, title, description, categoriesString, complexity, link); - const response = await axios.put(`/api/questions/${id}`, body); + const response = await QuestionService.client.put(`/api/questions/${id}`, body); return response.data; } static async deleteQuestion(id: number): Promise { - const response = await axios.delete(`/api/questions/${id}`); + const response = await QuestionService.client.delete(`/api/questions/${id}`); return response.data; } }