Skip to content

Commit

Permalink
Merge pull request #134 from Arquisoft/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
marco-qg authored Apr 29, 2024
2 parents 7ba72f0 + 4d733eb commit 190e17c
Show file tree
Hide file tree
Showing 80 changed files with 16,270 additions and 2,612 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ jobs:
- run: npm --prefix storeQuestionService ci
- run: npm --prefix userStatsService ci
- run: npm --prefix gameservice ci
- run: npm --prefix gameservice ci
- run: npm --prefix users/authservice test -- --coverage
- run: npm --prefix users/userservice test -- --coverage
- run: npm --prefix gatewayservice test -- --coverage
- run: npm --prefix webapp test -- --coverage
- run: npm --prefix storeQuestionService test -- --coverage
- run: npm --prefix userStatsService test -- --coverage
- run: npm --prefix gameservice test -- --coverage
# - run: npm --prefix gameservice test -- --coverage
- name: Analyze with SonarCloud
uses: sonarsource/sonarcloud-github-action@master
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- run: npm --prefix webapp test -- --coverage
- run: npm --prefix storeQuestionService test -- --coverage
- run: npm --prefix userStatsService test -- --coverage
#- run: npm --prefix gameservice test -- --coverage
- run: npm --prefix gameservice test -- --coverage
- name: Analyze with SonarCloud
uses: sonarsource/sonarcloud-github-action@master
env:
Expand Down
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 190e17c

Please sign in to comment.