Skip to content

Commit

Permalink
Merge branch 'master' into quizGame
Browse files Browse the repository at this point in the history
  • Loading branch information
uo287998 authored Apr 2, 2024
2 parents cb5f9c6 + 461e31e commit 91ef62a
Show file tree
Hide file tree
Showing 31 changed files with 11,513 additions and 1,101 deletions.
28 changes: 22 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ services:
environment:
MONGODB_URI: mongodb://mongodb:27017/userdb

statisticsservice:
container_name: statisticsservice-${teamname:-defaultASW}
image: ghcr.io/arquisoft/wiq_es3a/statisticsservice:latest
profiles: ["dev", "prod"]
build: ./statistics/statisticsservice
depends_on:
- mongodb
ports:
- "8006:8006"
networks:
- mynetwork
environment:
MONGODB_URI: mongodb://mongodb:27017/userdb

gatewayservice:
container_name: gatewayservice-${teamname:-defaultASW}
image: ghcr.io/arquisoft/wiq_es3a/gatewayservice:latest
Expand All @@ -73,6 +87,7 @@ services:
- userservice
- authservice
- generatorservice
- statisticsservice
- questionservice
ports:
- "8000:8000"
Expand All @@ -81,9 +96,11 @@ services:
environment:
AUTH_SERVICE_URL: http://authservice:8002
USER_SERVICE_URL: http://userservice:8001
STATS_SERVICE_URL: http://statisticsservice:8006
GENERATOR_SERVICE_URL: http://generatorservice:8003
QUESTION_SERVICE_URL: http://questionservice:8004


webapp:
container_name: webapp-${teamname:-defaultASW}
image: ghcr.io/arquisoft/wiq_es3a/webapp:latest
Expand All @@ -93,6 +110,10 @@ services:
- gatewayservice
ports:
- "3000:3000"
environment:
GATEWAY_SERVICE_URL: http://gatewayservice:8000



prometheus:
image: prom/prometheus
Expand Down Expand Up @@ -130,14 +151,9 @@ services:

volumes:
mongodb_data:
driver: local
driver_opts:
type: none
o: bind
device: /path/to/your/local/mongodb_data
prometheus_data:
grafana_data:

networks:
mynetwork:
driver: bridge
driver: bridge
Binary file modified docs/images/diagramaDespliegue.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 43 additions & 1 deletion gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const port = 8000;

const authServiceUrl = process.env.AUTH_SERVICE_URL || 'http://localhost:8002';
const userServiceUrl = process.env.USER_SERVICE_URL || 'http://localhost:8001';
const statisticssServiceUrl = process.env.STATS_SERVICE_URL || 'http://localhost:8006';
const generatorServiceUrl = process.env.GENERATOR_SERVICE_URL || 'http://localhost:8003';
const questionServiceUrl = process.env.QUESTION_SERVICE_URL || 'http://localhost:8004';

Expand Down Expand Up @@ -50,15 +51,35 @@ app.post('/adduser', async (req, res) => {
app.get('/generate-question', async (req, res) => {
try {
// Forward the generate question request to the question service
const bearerHeader = req.headers['authorization'];
if(! bearerHeader ){
return res.sendStatus(403);
}
const questionResponse = await axios.get(generatorServiceUrl+'/generate-question');
res.json(questionResponse.data);
} catch (error) {
res.status(error.response.status).json({ error: error.response.data.error });
}
});


app.get('/statistics', async (req, res) => {
try {
const questionResponse = await axios.get(statisticssServiceUrl+'/statistics', {
params: req.query,
});
res.json(questionResponse.data);
} catch (error) {
res.status(error.response.status).json({ error: error.response.data.error });
}
});

app.get('/questions', async (req, res) => {
try {
const bearerHeader = req.headers['authorization'];
if(! bearerHeader ){
return res.sendStatus(403);
}
// Forward the get questions request to the question service
const questionResponse = await axios.get(questionServiceUrl+'/questions');
res.json(questionResponse.data);
Expand All @@ -68,6 +89,26 @@ app.get('/questions', async (req, res) => {
});


app.post('/addStatistic', async (req, res) => {
try {
const questionResponse = await axios.post(statisticssServiceUrl+'/addStatistic', req.body );
res.json(questionResponse.data);
} catch (error) {
res.status(error.response.status).json({ error: error.response.data.error });
}
});

app.get('/users', async (req, res) => {
try {
// Forward the get questions request to the question service
const usersResponse = await axios.get(userServiceUrl+'/users');
res.json(usersResponse.data);
} catch (error) {
res.status(error.response.status).json({ error: error.response.data.error });
}
});


// Read the OpenAPI YAML file synchronously
openapiPath='./openapi.yaml'
if (fs.existsSync(openapiPath)) {
Expand All @@ -84,9 +125,10 @@ if (fs.existsSync(openapiPath)) {
console.log("Not configuring OpenAPI. Configuration file not present.")
}


// Start the gateway service
const server = app.listen(port, () => {
console.log(`Gateway Service listening at http://localhost:${port}`);
});

module.exports = server
module.exports = server;
18 changes: 18 additions & 0 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ describe('Gateway Service', () => {
return Promise.resolve({ data: { question: 'mockedQuestion' } });
}else if (url.endsWith('/questions')) {
return Promise.resolve({ data: { question: 'mockedQuestion' } });
}else if (url.endsWith('/statistics')) {
return Promise.resolve({ data: { gamesPlayed: 'mockedGamesPlayed' ,
rigthAnswers: 'mockedRigthAnswers',
wrongAnswers:'mockedWrongAnswers' }});
}
});



// Test /login endpoint
it('should forward login request to auth service', async () => {
const response = await request(app)
Expand Down Expand Up @@ -61,4 +67,16 @@ describe('Gateway Service', () => {
expect(response.statusCode).toBe(200);
expect(response.body.question).toBe('mockedQuestion');
});

// Test /statistics endpoint
it('should forward get statistics request to statistics service', async () => {
const response = await request(app)
.get('/statistics');

expect(response.statusCode).toBe(200);
expect(response.body.gamesPlayed).toBe('mockedGamesPlayed');
expect(response.body.rigthAnswers).toBe('mockedRigthAnswers');
expect(response.body.wrongAnswers).toBe('mockedWrongAnswers');
});

});
48 changes: 47 additions & 1 deletion gatewayservice/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,50 @@ paths:
error:
type: string
description: Error information.
example: Internal Server Error
example: Internal Server Error
/users:
get:
summary: Get all users.
operationId: getUsers
responses:
'200':
description: Users retrieved successfully.
content:
application/json:
schema:
type: array
items:
type: object
properties:
_id:
type: string
description: user ID.
example: 65f756db3fa22d227a4b7c7d
username:
type: string
description: User.
example: carmen
gamesPlayed:
type: integer
description: Games played.
example: 2
rigthAnswers:
type: integer
description: Right answers.
example: 5
wrongAnswers:
type: integer
description: Wrong answers.
example: 5

'500':
description: Internal server error.
content:
application/json:
schema:
type: object
properties:
error:
type: string
description: Error information.
example: Internal Server Error
6 changes: 6 additions & 0 deletions gatewayservice/package-lock.json

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

63 changes: 32 additions & 31 deletions gatewayservice/package.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
{
"name": "gatewayservice",
"version": "1.0.0",
"description": "Gateway service, in charge distributing the requests",
"main": "service.js",
"scripts": {
"start": "node gateway-service.js",
"test": "jest"
},
"repository": {
"type": "git",
"url": "git+https://github.com/arquisoft/wiq_es3a.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/arquisoft/wiq_es3a/issues"
},
"homepage": "https://github.com/arquisoft/wiq_es3a#readme",
"dependencies": {
"axios": "^1.6.5",
"cors": "^2.8.5",
"express": "^4.18.2",
"express-openapi": "^12.1.3",
"express-prom-bundle": "^7.0.0",
"swagger-ui-express": "^5.0.0",
"yaml": "^2.4.1"
},
"devDependencies": {
"jest": "^29.7.0",
"supertest": "^6.3.4"
}
"name": "gatewayservice",
"version": "1.0.0",
"description": "Gateway service, in charge distributing the requests",
"main": "service.js",
"scripts": {
"start": "node gateway-service.js",
"test": "jest"
},
"repository": {
"type": "git",
"url": "git+https://github.com/arquisoft/wiq_es3a.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/arquisoft/wiq_es3a/issues"
},
"homepage": "https://github.com/arquisoft/wiq_es3a#readme",
"dependencies": {
"axios": "^1.6.5",
"cors": "^2.8.5",
"express": "^4.18.2",
"express-openapi": "^12.1.3",
"express-prom-bundle": "^7.0.0",
"fs": "^0.0.1-security",
"swagger-ui-express": "^5.0.0",
"yaml": "^2.4.1"
},
"devDependencies": {
"jest": "^29.7.0",
"supertest": "^6.3.4"
}
}
Loading

0 comments on commit 91ef62a

Please sign in to comment.