Skip to content

Commit

Permalink
Merge pull request #136 from Arquisoft/Develop
Browse files Browse the repository at this point in the history
Develop to UO289097
  • Loading branch information
uo289097 authored Apr 2, 2024
2 parents 4a2fc23 + 121f232 commit 109c2b7
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,4 @@ jobs:
wget https://raw.githubusercontent.com/arquisoft/wiq_es3b/master/docker-compose.yml -O docker-compose.yml
wget https://raw.githubusercontent.com/arquisoft/wiq_es3b/master/.env -O .env
docker compose --profile prod down
docker compose --profile prod up -d
docker-compose --profile prod up -d --pull always
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ services:
- "27017:27017"
networks:
- mynetwork
restart: always

authservice:
container_name: authservice-${teamname:-defaultASW}
Expand All @@ -24,6 +25,7 @@ services:
- mynetwork
environment:
MONGODB_URI: mongodb://mongodb:27017/userdb
restart: always

userservice:
container_name: userservice-${teamname:-defaultASW}
Expand All @@ -38,6 +40,7 @@ services:
- mynetwork
environment:
MONGODB_URI: mongodb://mongodb:27017/userdb
restart: always

questiongenerationservice:
container_name: questiongenerationservice-${teamname:-defaultASW}
Expand All @@ -50,6 +53,8 @@ services:
- mynetwork
environment:
QUESTIONS_SERVICE_URL: http://questionservice:8004
AUTH_SERVICE_URL: http://authservice:8002
restart: always

questionservice:
container_name: questionservice-${teamname:-defaultASW}
Expand All @@ -64,6 +69,7 @@ services:
- mynetwork
environment:
MONGODB_URI: mongodb://mongodb:27017/questionsdb
restart: always

gameservice:
container_name: gameservice-${teamname:-defaultASW}
Expand All @@ -78,6 +84,7 @@ services:
- mynetwork
environment:
MONGODB_URI: mongodb://mongodb:27017/gamesdb
restart: always

gatewayservice:
container_name: gatewayservice-${teamname:-defaultASW}
Expand All @@ -101,6 +108,7 @@ services:
QUESTION_GENERATION_SERVICE_URL: http://questiongenerationservice:8003
QUESTIONS_SERVICE_URL: http://questionservice:8004
GAME_SERVICE_URL: http://gameservice:8005
restart: always

webapp:
container_name: webapp-${teamname:-defaultASW}
Expand All @@ -113,6 +121,7 @@ services:
- "3000:3000"
environment:
GATEWAY_SERVICE_URL: http://gatewayservice:8000
restart: always
#volumes:
#- ./webapp:/app

Expand Down
14 changes: 9 additions & 5 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,16 @@ app.post('/adduser', async (req, res) => {

app.get('/api/questions/create', async (req, res) => {
try {
let apiUrl=questionGenerationServiceUrl+'/api/questions/create';
if(Object.keys(req.query).length > 0) {
apiUrl+='?'+new URLSearchParams(req.query);
}
const queryParams = new URLSearchParams(req.query);
const apiUrl = new URL('/api/questions/create', questionGenerationServiceUrl);
apiUrl.search = queryParams.toString();

// Forward the add user request to the user service
const userResponse = await axios.get(apiUrl);
const userResponse = await axios.get(apiUrl.toString(), {
headers: {
Authorization: req.headers.authorization
}
});
res.json(userResponse.data);
} catch (error) {
res.status(error.response.status).json({ error: error.response.data.error });
Expand Down
2 changes: 1 addition & 1 deletion question_generator/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"Who has more followers? (Tennis)": "Who has more followers? (Tennis)",
"Who has more looses? (Tennis)": "Who has more looses? (Tennis)",
"Who has more wins? (Tennis)": "Who has more wins? (Tennis)",
"Which tennis player is from?" : "Which tennis player is from %s?"
"Which tennis player is from?" : "Which tennis player is from %s?",

"Which team plays in?": "Which team plays in %s?",
"Which team trains?": "Which team trains %s?",
Expand Down
2 changes: 1 addition & 1 deletion question_generator/locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"Who has more followers?": "¿Quién tiene más seguidores? (Tenis)",
"Who has more looses?": "¿Quién tiene más derrotas? (Tenis)",
"Who has more wins?": "¿Quién tiene más victorias? (Tenis)",
"Which tennis player is from?" : "¿Qué jugador es de %s?"
"Which tennis player is from?" : "¿Qué jugador es de %s?",

"Which football team was founded in?": "¿Qué equipo de fútbol fue fundado en %s?",
"Which team plays in?": "¿Qué equipo juega en %s?",
Expand Down
12 changes: 10 additions & 2 deletions question_generator/questionGenerationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const sportTemplate=require('./sports/sportTemplate');
const generalTemplate=require('./questionTemplate');
const axios = require('axios');
const questionServiceUrl = process.env.QUESTIONS_SERVICE_URL || 'http://localhost:8004';
const authServiceUrl = process.env.AUTH_SERVICE_URL || 'http://localhost:8002';

const app = express();
const port = 8003;
Expand Down Expand Up @@ -36,8 +37,15 @@ app.use(
app.get('/api/questions/create', async (req, res) => {
try {
let category = req.query.category;
//User is null because we are not using authentication yet
let user=null;
if (req.headers.authorization) {
const response = await axios.get(`${authServiceUrl}/verify`, {
headers: {
Authorization: req.headers.authorization
}
});
user = response.data.username;
}
let randomQuestion;

switch (category) {
Expand All @@ -58,7 +66,7 @@ app.get('/api/questions/create', async (req, res) => {
const saveQuestion = async (question) => {
const url = questionServiceUrl+'/addquestion';
try {
const response = await axios.post(url, question);
await axios.post(url, question);
} catch (error) {
console.error(error);
}
Expand Down
15 changes: 15 additions & 0 deletions users/authservice/auth-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ app.post('/login', async (req, res) => {
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.get('/verify', async (req, res) => {
try {
const token = req.headers.authorization.split(' ')[1];
const decodedToken = jwt.verify(token, 'your-secret-key');
const userId = decodedToken.userId;
const user = await User.findById(userId);
if (user) {
res.json({ username: user.username, createdAt: user.createdAt });
} else {
res.status(404).json({ error: 'User not found' });
}
} catch (error) {
res.status(401).json({ error: 'Invalid token' });
}
});

// Start the server
const server = app.listen(port, () => {
Expand Down
5 changes: 2 additions & 3 deletions webapp/src/components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ const Login = ({ goTo }) => {
const response = await axios.post(`${apiEndpoint}/login`, { username, password });

// Extract data from the response
const { createdAt: userCreatedAt, username: loggedInUsername, profileImage: profileImage } = response.data;
const { createdAt: userCreatedAt, username: loggedInUsername, token:token, profileImage: profileImage } = response.data;

setTimeStart(Date.now());
setCreatedAt(userCreatedAt);
setLoginSuccess(true);
saveSessionData({ username: loggedInUsername, createdAt: userCreatedAt, profileImage: profileImage });
console.log(profileImage+'\n\n\n');
saveSessionData({ username: loggedInUsername, createdAt: userCreatedAt, token: token, profileImage: profileImage });
setOpenSnackbar(true);
} catch (error) {
setError(error.response.data.error);
Expand Down
8 changes: 6 additions & 2 deletions webapp/src/components/Question.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export const handleGameFinish = (nQuestion, numberCorrect, segundos, MAX_TIME, s
const Question = ({ goTo, setGameFinished }) => {
localStorage.setItem("pAcertadas", 0);

useContext(SessionContext);
const {sessionData}=useContext(SessionContext);
const userToken = sessionData ? sessionData.token : '';

const [question, setQuestion] = useState('');
const [options, setOptions] = useState([]);
Expand Down Expand Up @@ -77,7 +78,10 @@ const Question = ({ goTo, setGameFinished }) => {
const fetchQuestion = async () => {
try {
const response = await fetch(`${gatewayUrl}/api/questions/create`, {
method: 'GET'
method: 'GET',
headers: {
'Authorization': `Bearer ${userToken}`
}
});
const data = await response.json();

Expand Down

0 comments on commit 109c2b7

Please sign in to comment.