Skip to content

Commit

Permalink
Add docker compose & adjust ports of services
Browse files Browse the repository at this point in the history
  • Loading branch information
nknguyenhc committed Oct 1, 2024
1 parent 1dd3f69 commit 6b97a2d
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 8 deletions.
4 changes: 3 additions & 1 deletion backend/question-service/.env.sample
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
DB_CLOUD_URI=<CONNECTION_STRING>
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
31 changes: 31 additions & 0 deletions backend/question-service/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions backend/question-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 5 additions & 1 deletion backend/question-service/src/server.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions backend/user-service/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion backend/user-service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
services:
question:
build: ./backend/question-service
ports:
- "3002:3002"

user:
build: ./backend/user-service
ports:
- "3001:3001"
4 changes: 3 additions & 1 deletion frontend/.env.sample
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
port=3001
PORT=3000
REACT_APP_QUESTION_SERVICE_URL=http://localhost:3002
REACT_APP_USER_SERVICE_URL=http://localhost:3001
15 changes: 11 additions & 4 deletions frontend/src/services/question.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Question[]> {
const response = await axios.get("/api/questions");
const response = await QuestionService.client.get("/api/questions");
return response.data;
}

Expand All @@ -17,7 +24,7 @@ export default class QuestionService {
link: string,
): Promise<any> {
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;
}

Expand All @@ -30,12 +37,12 @@ export default class QuestionService {
link: string,
): Promise<any> {
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<any> {
const response = await axios.delete(`/api/questions/${id}`);
const response = await QuestionService.client.delete(`/api/questions/${id}`);
return response.data;
}
}

0 comments on commit 6b97a2d

Please sign in to comment.