Skip to content

Commit

Permalink
Merge pull request #123 from Arquisoft/game_historic_service
Browse files Browse the repository at this point in the history
User data interface and other changes
  • Loading branch information
AbelMH1 authored Apr 26, 2024
2 parents fb7d392 + 416eaee commit 3356971
Show file tree
Hide file tree
Showing 28 changed files with 6,088 additions and 94 deletions.
2 changes: 2 additions & 0 deletions apisgatewayservice/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
coverage
17 changes: 17 additions & 0 deletions apisgatewayservice/Dockerfile
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"]
57 changes: 57 additions & 0 deletions apisgatewayservice/apis-gateway-service.js
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
64 changes: 64 additions & 0 deletions apisgatewayservice/apis-gateway-service.test.js
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' });
});
});
});
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
Loading

0 comments on commit 3356971

Please sign in to comment.