Skip to content

Commit

Permalink
questiongenservice & questionaskservice v0
Browse files Browse the repository at this point in the history
  • Loading branch information
angelalvaigle committed Mar 8, 2024
1 parent f027edd commit 00e3f47
Show file tree
Hide file tree
Showing 19 changed files with 5,770 additions and 138 deletions.
30 changes: 23 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,33 @@ services:
environment:
MONGODB_URI: mongodb://mongodb:27017/userdb

answerservice:
container_name: answerservice-${teamname:-defaultASW}
image: ghcr.io/arquisoft/wiq_es6a/answerservice:latest
questiongenservice:
container_name: questiongenservice-${teamname:-defaultASW}
image: ghcr.io/arquisoft/wiq_es6a/questiongenservice:latest
profiles: ["dev", "prod"]
build: ./questions/answerservice
build: ./questions/questiongenservice
depends_on:
- mongodb
ports:
- "8003:8003"
networks:
- mynetwork
environment:
MONGODB_URI: mongodb://mongodb:27017/questiondb

questionaskservice:
container_name: questionaskservice-${teamname:-defaultASW}
image: ghcr.io/arquisoft/wiq_es6a/questionaskservice:latest
profiles: ["dev", "prod"]
build: ./questions/questionaskservice
depends_on:
- mongodb
ports:
- "8004:8004"
networks:
- mynetwork
environment:
MONGODB_URI: mongodb://mongodb:27017/answerdb
MONGODB_URI: mongodb://mongodb:27017/questiondb

gatewayservice:
container_name: gatewayservice-${teamname:-defaultASW}
Expand All @@ -62,15 +76,17 @@ services:
- mongodb
- userservice
- authservice
- answerservice
- questiongenservice
- questionaskservice
ports:
- "8000:8000"
networks:
- mynetwork
environment:
USER_SERVICE_URL: http://userservice:8001
AUTH_SERVICE_URL: http://authservice:8002
ANSWER_SERVICE_URL: http://answerservice:8004
QUESTION_GEN_SERVICE_URL: http://questiongenservice:8003
QUESTION_ASK_SERVICE_URL: http://questionaskservice:8004

webapp:
container_name: webapp-${teamname:-defaultASW}
Expand Down
20 changes: 10 additions & 10 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const port = 8000;

const userServiceUrl = process.env.USER_SERVICE_URL || 'http://localhost:8001';
const authServiceUrl = process.env.AUTH_SERVICE_URL || 'http://localhost:8002';

const answerServiceUrl = process.env.ANSWER_SERVICE_URL || 'http://localhost:8004';
const questionGenServiceUrl = process.env.QUESTION_GEN_SERVICE_URL || 'http://localhost:8003';
const questionAskServiceUrl = process.env.QUESTION_ASK_SERVICE_URL || 'http://localhost:8004';

app.use(cors());
app.use(express.json());
Expand Down Expand Up @@ -43,21 +43,21 @@ app.post('/adduser', async (req, res) => {
}
});

app.post('/addanswer', async (req, res) => {
app.post('/addquestion', async (req, res) => {
try {
// Forward the add answer request to the answer service
const answerResponse = await axios.post(answerServiceUrl+'/addanswer', req.body);
res.json(answerResponse.data);
// Forward the add question request to the question generation service
const questionGenResponse = await axios.post(questionGenServiceUrl+'/addquestion', req.body);
res.json(questionGenResponse.data);
} catch (error) {
res.status(error.response.status).json({ error: error.response.data.error });
}
});

app.post('/getanswer', async (req, res) => {
app.post('/getquestion', async (req, res) => {
try {
// Forward the add answer request to the answer service
const answerResponse = await axios.post(answerServiceUrl+'/getanswer', req.body);
res.json(answerResponse.data);
// Forward the get question request to the question asking service
const questionAskResponse = await axios.post(questionAskServiceUrl+'/getquestion', req.body);
res.json(questionAskResponse.data);
} catch (error) {
res.status(error.response.status).json({ error: error.response.data.error });
}
Expand Down
82 changes: 0 additions & 82 deletions questions/answerservice/answer-service.js

This file was deleted.

30 changes: 0 additions & 30 deletions questions/answerservice/answer-service.test.js

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
FROM node:20

# Set the working directory in the container
WORKDIR /usr/src/answerservice
WORKDIR /usr/src/questionaskservice

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./
Expand All @@ -17,4 +17,4 @@ COPY . .
EXPOSE 8004

# Define the command to run your app
CMD ["node", "answer-service.js"]
CMD ["node", "questionask-service.js"]
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const mongoose = require('mongoose');

const answerSchema = new mongoose.Schema({
const questionSchema = new mongoose.Schema({
type: {
type: String,
required: true,
Expand All @@ -27,6 +27,6 @@ const answerSchema = new mongoose.Schema({
},
});

const Answer = mongoose.model('Answer', answerSchema);
const Question = mongoose.model('Question', questionSchema);

module.exports = Answer
module.exports = Question
59 changes: 59 additions & 0 deletions questions/questionaskservice/questionask-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// questionask-service.js
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const Question = require('./questionask-model')

const app = express();
const port = 8004;

// Middleware to parse JSON in request body
app.use(bodyParser.json());

// Connect to MongoDB
const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/questiondb';
mongoose.connect(mongoUri);

// Function to validate required fields in the request body
function validateRequiredFields(req, requiredFields) {
for (const field of requiredFields) {
if (!(field in req.body)) {
throw new Error(`Missing required field: ${field}`);
}
}
}

app.post('/getquestion', async (req, res) => {
try {
// Check if required fields are present in the request body
validateRequiredFields(req, ['type', 'attribute']);

const { type, attribute } = req.body;

// Find the question in the database
const question = await Question.findOne({ attribute });

// Check if the question exists
if (question) {
// Respond with the question information
res.json({ type: question.type, attribute: question.attribute, right: question.right,
wrong1: question.wrong1, wrong2: question.wrong2, wrong3: question.wrong3, });
} else {
res.status(401).json({ error: 'Question not found' });
}
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' });
}
});

const server = app.listen(port, () => {
console.log(`Question Asking Service listening at http://localhost:${port}`);
});

// Listen for the 'close' event on the Express.js server
server.on('close', () => {
// Close the Mongoose connection
mongoose.connection.close();
});

module.exports = server
2 changes: 2 additions & 0 deletions questions/questiongenservice/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
coverage
20 changes: 20 additions & 0 deletions questions/questiongenservice/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use an official Node.js runtime as a parent image
FROM node:20

# Set the working directory in the container
WORKDIR /usr/src/questionagenservice

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./

# Install app dependencies
RUN npm install

# Copy the app source code to the working directory
COPY . .

# Expose the port the app runs on
EXPOSE 8003

# Define the command to run your app
CMD ["node", "questiongen-service.js"]
Loading

0 comments on commit 00e3f47

Please sign in to comment.