forked from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
questiongenservice & questionaskservice v0
- Loading branch information
1 parent
f027edd
commit 00e3f47
Showing
19 changed files
with
5,770 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
Oops, something went wrong.