Skip to content

Commit 7664713

Browse files
authored
Merge pull request #79 from Arquisoft/develop
Entregable 3
2 parents f14a0da + 5917c61 commit 7664713

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+45701
-15323
lines changed

.env

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ teamname="wiq_es1a"
22
AUTH_SERVICE_URL=http://localhost:8002
33
USER_SERVICE_URL=http://localhost:8001
44
QUESTION_SERVICE_URL=http://localhost:8003
5-
AUTH_SERVICE_URL=http://localhost:8004
5+
STATS_SERVICE_URL=http://localhost:8004
66
GATEWAY_SERVICE_URL=http://gatewayservice:8000
7-
MONGODB_URI=mongodb://localhost:27017/userdb
7+
MONGODB_URI=mongodb://localhost:27017/userdb
8+
MONGODB_STATS_URI=mongodb://localhost:27017/statsdb

.github/workflows/release.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ jobs:
139139
needs: [e2e-tests]
140140
steps:
141141
- uses: actions/checkout@v4
142+
- name: Update OpenAPI configuration
143+
run: |
144+
DEPLOY_HOST=${{ secrets.DEPLOY_HOST }}
145+
sed -i "s/SOMEIP/${DEPLOY_HOST}/g" gatewayservice/openapi.yaml
142146
- name: Publish to Registry
143147
uses: elgohr/Publish-Docker-Github-Action@v5
144148
with:
@@ -170,4 +174,4 @@ jobs:
170174
wget https://raw.githubusercontent.com/arquisoft/wiq_es1a/master/docker-compose.yml -O docker-compose.yml
171175
wget https://raw.githubusercontent.com/arquisoft/wiq_es1a/master/.env -O .env
172176
docker compose --profile prod down
173-
docker compose --profile prod up -d
177+
docker compose --profile prod up -d --pull always

docker-compose.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,12 @@ services:
2828
build: ./statsservice
2929
depends_on:
3030
- mongodb
31-
- userservice
3231
ports:
3332
- "8004:8004"
3433
networks:
3534
- mynetwork
3635
environment:
37-
MONGODB_URI: mongodb://mongodb:27017/userdb
38-
USER_SERVICE_URL: http://userservice:8001
36+
MONGODB_STATS_URI: mongodb://mongodb:27017/statsdb
3937

4038
authservice:
4139
container_name: authservice-${teamname:-defaultASW}

gatewayservice/gateway-service.js

+65-19
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ const express = require("express");
22
const axios = require("axios");
33
const cors = require("cors");
44
const promBundle = require("express-prom-bundle");
5-
5+
const YAML = require('yaml');
6+
const fs = require("fs");
7+
const swaggerUi = require('swagger-ui-express');
68
const app = express();
79
const port = 8000;
810

911
const authServiceUrl = process.env.AUTH_SERVICE_URL || "http://localhost:8002";
1012
const userServiceUrl = process.env.USER_SERVICE_URL || "http://localhost:8001";
1113
const questionServiceUrl =
1214
process.env.QUESTION_SERVICE_URL || "http://localhost:8003";
13-
const statsServiceUrl = process.env.AUTH_SERVICE_URL || "http://localhost:8004";
15+
const statsServiceUrl = process.env.STATS_SERVICE_URL || "http://localhost:8004";
1416

1517
app.use(cors());
1618
app.use(express.json());
@@ -53,6 +55,36 @@ app.post("/adduser", async (req, res) => {
5355
}
5456
});
5557

58+
app.get("/userInfo", async (req, res) => {
59+
try {
60+
// Forward the question request to the user service
61+
const userResponse = await axios.get(
62+
userServiceUrl + "/userInfo",
63+
{ params: req.query }
64+
);
65+
res.json(userResponse.data);
66+
} catch (error) {
67+
res
68+
.status(error.response.status)
69+
.json({ error: error.response.data.error });
70+
}
71+
});
72+
73+
app.post("/saveGameList", async (req, res) => {
74+
try {
75+
// Forward the save game request to the stats service
76+
const gameResponse = await axios.post(
77+
userServiceUrl + "/saveGameList",
78+
req.body
79+
);
80+
res.json(gameResponse.data);
81+
} catch (error) {
82+
res
83+
.status(error.response.status)
84+
.json({ error: error.response.data.error });
85+
}
86+
});
87+
5688
app.get("/questions", async (req, res) => {
5789
try {
5890
// Forward the question request to the question service
@@ -68,6 +100,21 @@ app.get("/questions", async (req, res) => {
68100
}
69101
});
70102

103+
app.post("/questions", async (req, res) => {
104+
try {
105+
// Forward the question request to the question service
106+
const questionResponse = await axios.post(
107+
questionServiceUrl + "/questions",
108+
{ body: req.body }
109+
);
110+
res.json(questionResponse.data);
111+
} catch (error) {
112+
res
113+
.status(error.response.status)
114+
.json({ error: error.response.data.error });
115+
}
116+
});
117+
71118
app.get("/stats", async (req, res) => {
72119
try {
73120
// Forward the stats request to the stats service
@@ -97,10 +144,9 @@ app.post("/saveGame", async (req, res) => {
97144
}
98145
});
99146

100-
app.get("/getstats", async (req, res) => {
147+
app.get("/ranking", async (req, res) => {
101148
try {
102-
// Forward the stats request to the stats service
103-
const statsResponse = await axios.get(userServiceUrl + "/getstats", {
149+
const statsResponse = await axios.get(statsServiceUrl + "/ranking", {
104150
params: req.query,
105151
});
106152
res.json(statsResponse.data);
@@ -111,20 +157,20 @@ app.get("/getstats", async (req, res) => {
111157
}
112158
});
113159

114-
app.post("/userSaveGame", async (req, res) => {
115-
try {
116-
// Forward the save game request to the stats service
117-
const gameResponse = await axios.post(
118-
userServiceUrl + "/userSaveGame",
119-
req.body
120-
);
121-
res.json(gameResponse.data);
122-
} catch (error) {
123-
res
124-
.status(error.response.status)
125-
.json({ error: error.response.data.error });
126-
}
127-
});
160+
openapiPath='./openapi.yaml'
161+
if (fs.existsSync(openapiPath)) {
162+
const file = fs.readFileSync(openapiPath, 'utf8');
163+
164+
// Parse the YAML content into a JavaScript object representing the Swagger document
165+
const swaggerDocument = YAML.parse(file);
166+
167+
// Serve the Swagger UI documentation at the '/api-doc' endpoint
168+
// This middleware serves the Swagger UI files and sets up the Swagger UI page
169+
// It takes the parsed Swagger document as input
170+
app.use('/api-doc', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
171+
} else {
172+
console.log("Not configuring OpenAPI. Configuration file not present.")
173+
}
128174

129175
// Start the gateway service
130176
const server = app.listen(port, () => {

gatewayservice/gateway-service.test.js

+32
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ describe('Gateway Service', () => {
3939
}
4040
],
4141
});
42+
} else if (url.endsWith('/stats')) {
43+
return Promise.resolve({ data: { stats: 'mockedStats' } });
44+
} else if (url.endsWith('/userInfo')) {
45+
return Promise.resolve({ data: { userInfo: 'mockedUserInfo' } });
46+
} else if (url.endsWith('/ranking')) {
47+
return Promise.resolve({ data: { ranking: 'mockedRanking' } });
4248
}
4349
});
4450

@@ -89,4 +95,30 @@ describe('Gateway Service', () => {
8995
}
9096
]);
9197
});
98+
99+
// Test /userInfo endpoint
100+
it('should forward userInfo request to user service', async () => {
101+
const response = await request(app)
102+
.get('/userInfo')
103+
.query({ user: 'testuser' });
104+
105+
expect(response.statusCode).toBe(200);
106+
expect(response.body).toEqual({ userInfo: 'mockedUserInfo' });
107+
});
108+
109+
// Test /stats endpoint
110+
it('should forward stats request to stats service', async () => {
111+
const response = await request(app).get("/stats").query({ user: 'testuser', gamemode: 'classic' });
112+
113+
expect(response.statusCode).toBe(200);
114+
expect(response.body).toEqual({ stats: 'mockedStats' });
115+
});
116+
117+
// Test /ranking endpoint
118+
it('should forward ranking request to stats service', async () => {
119+
const response = await request(app).get("/ranking").query({ gamemode: 'classic', filterBy: 'all' });
120+
121+
expect(response.statusCode).toBe(200);
122+
expect(response.body).toEqual({ ranking: 'mockedRanking' });
123+
});
92124
});

0 commit comments

Comments
 (0)