Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User data interface and other changes #123

Merged
merged 23 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c80b73c
Added openapi file for the api
MarcosBarrilVillaverde Apr 5, 2024
5867f64
Minor fixes realeted to speeling and indexing
MarcosBarrilVillaverde Apr 5, 2024
908969d
Added to the authentication and user registration services the openap…
MarcosBarrilVillaverde Apr 9, 2024
2014423
Added apis-gateway system
MarcosBarrilVillaverde Apr 11, 2024
ab8b6b1
Merge pull request #102 from Arquisoft/develop
marco-qg Apr 11, 2024
7d9236d
Merge branch 'develop'
AbelMH1 Apr 11, 2024
34f5702
Merge branch 'develop'
marco-qg Apr 12, 2024
7ba72f0
HOTFIX: Cambiado método para rellenar base de datos
AbelMH1 Apr 12, 2024
0fdfc4f
Merge branch 'master' into game_historic_service
MarcosBarrilVillaverde Apr 12, 2024
b8c9b80
Modify user-service and user-stats-service to get extra information n…
MarcosBarrilVillaverde Apr 12, 2024
0dc405b
Modifies API's gateway so it correctly send user information
MarcosBarrilVillaverde Apr 12, 2024
894b774
Small fix on services
MarcosBarrilVillaverde Apr 13, 2024
498f064
Changes in the interface so know it can check information about the user
MarcosBarrilVillaverde Apr 13, 2024
ecd3c5a
Fixed missing fields in the structure of user-stats-model
MarcosBarrilVillaverde Apr 13, 2024
af61ad6
Minor fixes on user-stat-model
MarcosBarrilVillaverde Apr 17, 2024
f0a0444
Updated openapi file for user-stat-service
MarcosBarrilVillaverde Apr 17, 2024
0f7025e
Fix on test as model related to games stored data is changed
MarcosBarrilVillaverde Apr 19, 2024
369dce0
Small fix
MarcosBarrilVillaverde Apr 19, 2024
74190f7
Small fixes to tests cases
MarcosBarrilVillaverde Apr 19, 2024
cb35bc8
New test case added
MarcosBarrilVillaverde Apr 19, 2024
f97fdef
Modified sonar properties
MarcosBarrilVillaverde Apr 19, 2024
0f0960a
Small security fix
MarcosBarrilVillaverde Apr 19, 2024
416eaee
Update apisgatewayservice/monitoring/prometheus/prometheus.yml
AbelMH1 Apr 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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