forked from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from Arquisoft/game_historic_service
User data interface and other changes
- Loading branch information
Showing
28 changed files
with
6,088 additions
and
94 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
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,17 @@ | ||
# Use an official Node.js runtime as a parent image | ||
FROM node:20 | ||
|
||
# Set the working directory in the container | ||
WORKDIR /usr/src/apisgatewayservice | ||
|
||
# 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 . . | ||
|
||
# Define the command to run your app | ||
CMD ["node", "apis-gateway-service.js"] |
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,57 @@ | ||
const express = require('express'); | ||
const axios = require('axios'); | ||
const cors = require('cors'); | ||
const promBundle = require('express-prom-bundle'); | ||
|
||
const app = express(); | ||
const port = 8100; | ||
|
||
const storeQuestionsServiceUrl = process.env.STORE_QUESTION_SERVICE_URL || 'http://localhost:8004' | ||
const userStatsServiceUrl = process.env.USER_STATS_SERVICE_URL || 'http://localhost:8003'; | ||
const userServiceUrl = process.env.USER_SERVICE_URL || 'http://localhost:8001'; | ||
|
||
app.use(cors()); | ||
app.use(express.json()); | ||
|
||
//Prometheus configuration | ||
const metricsMiddleware = promBundle({includeMethod: true}); | ||
|
||
function catchAction(error, res) { | ||
if ('response' in error && 'status' in error.response && 'data' in error.response && 'error' in error.response.data) | ||
res.status(error.response.status).json({ error: error.response.data.error }); | ||
else if('response' in error && 'status' in error.response){ | ||
res.status(error.response.status).json({ error: 'Unknown error' }); | ||
} else { | ||
console.log("Unknown error: " + error); | ||
} | ||
// } else { | ||
// res.status(500).json({ error: 'Internal server error' }); | ||
// } | ||
} | ||
|
||
app.use(metricsMiddleware); | ||
|
||
app.get('/users', async (req, res) => { | ||
try { | ||
const response = await axios.get(userServiceUrl+`/users`); | ||
res.json(response.data); | ||
} catch (error) { | ||
catchAction(error, res) | ||
} | ||
}) | ||
|
||
app.get('/history/questions', async (req, res) => { | ||
try { | ||
const response = await axios.get(storeQuestionsServiceUrl+'/history/questions'); | ||
res.json(response.data); | ||
} catch (error) { | ||
catchAction(error, res) | ||
} | ||
}) | ||
|
||
// Start the gateway service | ||
const server = app.listen(port, () => { | ||
console.log(`Gateway Service listening at http://localhost:${port}`); | ||
}); | ||
|
||
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,64 @@ | ||
const request = require('supertest'); | ||
const axios = require('axios'); | ||
const app = require('./apis-gateway-service'); | ||
|
||
afterAll(async () => { | ||
app.close(); | ||
}); | ||
|
||
jest.mock('axios'); | ||
|
||
describe('User Service', () => { | ||
describe('/users', () => { | ||
it('should return user information', async () => { | ||
const mockUsers = [ | ||
{ username: 'user1', createdAt: new Date() }, | ||
{ username: 'user2', createdAt: new Date() } | ||
]; | ||
User.find = jest.fn().mockResolvedValue(mockUsers); | ||
|
||
const res = await request(app).get('/users'); | ||
|
||
expect(res.statusCode).toEqual(200); | ||
|
||
expect(res.body).toEqual(mockUsers); | ||
}); | ||
|
||
it('should handle errors', async () => { | ||
|
||
User.find = jest.fn().mockRejectedValue(new Error('Database error')); | ||
|
||
const res = await request(app).get('/users'); | ||
|
||
expect(res.statusCode).toEqual(500); | ||
|
||
expect(res.body).toEqual({ error: 'Database error' }); | ||
}); | ||
}); | ||
}); | ||
|
||
|
||
describe('Gateway Service', () => { | ||
describe('/history/questions', () => { | ||
it('should return all questions', async () => { | ||
const mockData = [{ | ||
pregunta: '¿Cuál es la capital de la comunidad autónoma de Castilla y León?', | ||
respuesta_correcta: 'Ninguna', | ||
respuestas_incorrectas: ['Segovia','León','Valladolid'] | ||
}]; | ||
axios.get.mockResolvedValue({ data: mockData }); | ||
|
||
const res = await request(app).get('/history/questions'); | ||
expect(res.statusCode).toEqual(200); | ||
expect(res.body).toEqual(mockData); | ||
}); | ||
|
||
it('should handle errors', async () => { | ||
axios.get.mockRejectedValue(new Error('Error')); | ||
|
||
const res = await request(app).get('/history/questions'); | ||
expect(res.statusCode).toEqual(500); | ||
expect(res.body).toEqual({ error: 'Error' }); | ||
}); | ||
}); | ||
}); |
11 changes: 11 additions & 0 deletions
11
apisgatewayservice/monitoring/grafana/provisioning/dashboards/dashboard.yml
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,11 @@ | ||
apiVersion: 1 | ||
|
||
providers: | ||
- name: 'Prometheus' | ||
orgId: 1 | ||
folder: '' | ||
type: file | ||
disableDeletion: false | ||
editable: true | ||
options: | ||
path: /etc/grafana/provisioning/dashboards |
Oops, something went wrong.