Skip to content

Commit

Permalink
Merge branch 'master' into e2e_laura
Browse files Browse the repository at this point in the history
  • Loading branch information
lauratbg authored Apr 24, 2024
2 parents 9ea0ea5 + c32009e commit 6d6c743
Show file tree
Hide file tree
Showing 63 changed files with 2,539 additions and 386 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ jobs:
- uses: actions/checkout@v4
- name: Publish to Registry
uses: elgohr/Publish-Docker-Github-Action@v5
env:
JWT_KEY: ${{secrets.JWT_KEY}}
with:
name: arquisoft/wiq_en1b/authservice
username: ${{ github.actor }}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ docker compose --profile dev up --build
```

### Deployed in Cloud
In order to view the application deploy in the cloud click [here](http://172.203.216.60:3000)
In order to view the application deploy in the cloud click [here](http://wiqen1b.serveminecraft.net:3000)
### Members

- Lucía Ruiz Núñez [email protected]
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
prometheus:
image: prom/prometheus
container_name: prometheus-${teamname:-defaultASW}
profiles: ["dev"]
profiles: ["dev", "prod"]
networks:
- mynetwork
volumes:
Expand All @@ -127,7 +127,7 @@
grafana:
image: grafana/grafana
container_name: grafana-${teamname:-defaultASW}
profiles: ["dev"]
profiles: ["dev", "prod"]
networks:
- mynetwork
volumes:
Expand Down
156 changes: 139 additions & 17 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const promBundle = require('express-prom-bundle');
const swaggerUi = require('swagger-ui-express');
const fs = require("fs")
const YAML = require('yaml')

const jwt = require('jsonwebtoken');
const app = express();
const port = 8000;

Expand All @@ -33,7 +33,7 @@ app.post('/login', async (req, res) => {
const authResponse = await axios.post(authServiceUrl+'/login', req.body);
res.json(authResponse.data);
} catch (error) {
res.status(error.response.status).json({ error: error.response.data.error });
manageError(error)
}
});

Expand All @@ -43,52 +43,130 @@ app.post('/adduser', async (req, res) => {
const userResponse = await axios.post(userServiceUrl+'/adduser', req.body);
res.json(userResponse.data);
} catch (error) {
res.status(error.response.status).json({ error: error.response.data.error });
manageError(error);

}
});

app.get('/questions', async (req, res) => {
app.get('/questions', verifyToken, async (req, res) => {
try {

// Forward the question request to the quetion service
const questionResponse = await axios.get(questionServiceUrl+'/questions');
res.json(questionResponse.data);
} catch (error) {
res.status(error.response.status).json({ error: error.response.data.error });
manageError(error)
}
});

app.get('/questions/:lang', async (req, res) => {


app.get('/questions/:lang/:amount/:type', verifyToken, async (req, res) => {
try {
const lang = req.params.lang;
// Forward the question request to the quetion service
const questionResponse = await axios.get(questionServiceUrl+'/questions/' + lang);
if(!validateLang(req.params.lang.toString()) ||
!validateAmount(req.params.amount.toString()) ||
!validateType(req.params.type.toString()))
res.status(400).json({ error: 'Wrong values given' });
else {
const lang = encodeURIComponent(req.params.lang.toString());
const amount = encodeURIComponent(req.params.amount.toString());
const type = encodeURIComponent(req.params.type.toString());
// Forward the question request to the quetion service
const questionResponse = await axios.get(questionServiceUrl+'/questions/' + lang + '/' + amount + '/' + type);

res.json(questionResponse.data);
}
} catch (error) {

res.json(questionResponse.data);
manageError(error)
}
});


app.get('/questions/:lang/:amount', verifyToken, async (req, res) => {
try {
if(!validateLang(req.params.lang.toString()) ||
!validateAmount(req.params.amount.toString()))
res.status(400).json({ error: 'Wrong values given' });
else{
const lang = encodeURIComponent(req.params.lang.toString());
const amount = encodeURIComponent(req.params.amount.toString());
// Forward the question request to the quetion service
const questionResponse = await axios.get(questionServiceUrl+'/questions/' + lang + '/' + amount);

res.json(questionResponse.data);
}
} catch (error) {
manageError(error)
}
});

res.status(error.response.status).json({ error: error.response.data.error });
app.get('/questions/:lang', verifyToken, async (req, res) => {
try {
if(!validateLang(req.params.lang.toString()))
res.status(400).json({ error: 'Wrong values given' });
else{
const lang = encodeURIComponent(req.params.lang.toString());
// Forward the question request to the quetion service
const questionResponse = await axios.get(questionServiceUrl+'/questions/' + lang.toString());

res.json(questionResponse.data);
}

} catch (error) {

manageError(error)
}
});

app.post('/record', async(req, res) => {
app.post('/record', verifyToken, async(req, res) => {

try {
// Forward the record request to the record service
const recordResponse = await axios.post(recordServiceUrl+'/record', req.body);
res.json(recordResponse.data);
} catch (error) {
res.send(error);
manageError(error)
}
});

app.get('/record/:user', async(req, res)=>{
app.get('/record/ranking/top10', verifyToken, async(req, res)=>{
try {
const user = req.params.user;
// Forward the record request to the record service
const recordResponse = await axios.get(recordServiceUrl + '/record/' + user);
const recordResponse = await axios.get(recordServiceUrl + '/record/ranking/top10');
res.json(recordResponse.data);
} catch (error) {
res.send(error);
manageError(error)
}
});

app.get('/record/ranking/:user', verifyToken, async(req, res)=>{
try {
if(!validateUser(req.params.user.toString()))
res.status(400).json({ error: 'Wrong values given' });
else{
const user = encodeURIComponent(req.params.user.toString());
// Forward the record request to the record service
const recordResponse = await axios.get(recordServiceUrl + '/record/ranking/' + user);
res.json(recordResponse.data);
}
} catch (error) {
manageError(error)
}
});

app.get('/record/:user', verifyToken, async(req, res)=>{
try {
if(!validateUser(req.params.user.toString()))
res.status(400).json({ error: 'Wrong values given' });
else{
const user = encodeURIComponent(req.params.user.toString());
// Forward the record request to the record service
const recordResponse = await axios.get(recordServiceUrl + '/record/' + user);
res.json(recordResponse.data);
}
} catch (error) {
manageError(error)
}
});

Expand All @@ -108,4 +186,48 @@ const server = app.listen(port, () => {
console.log(`Gateway Service listening at http://localhost:${port}`);
});

function verifyToken(req, res, next) {
// Get the token from the request headers
const token = req.headers['token'] || req.body.token || req.query.token;

// Verify if the token is valid
jwt.verify(token, (process.env.JWT_KEY??'my-key'), (err, decoded) => {
if (err) {
// Token is not valid
res.status(403).json({authorized: false,
error: 'Invalid token or outdated'});
} else {
// Token is valid
req.decodedToken = decoded;
// Call next() to proceed to the next middleware or route handler
next();
}
});
}

function validateLang(lang){
return ['en', 'es', 'tk'].includes(lang);
}

function validateAmount(amount) {
const parsed = parseInt(amount, 10);
// We only accept integers and positive ones
return !isNaN(parsed) && parsed > 0;
}

function validateType(type){
return ['POPULATION', 'CAPITAL', 'LANGUAGE', 'SIZE'].includes(type);
}

function validateUser(user){
return !(/\s/.test(user)) //True if there are no spaces
}

function manageError(error){
if(error.response) //Some microservice responded with an error
res.status(error.response.status).json({ error: error.response.data.error });
else //Some other error
res.status(500).json({error : "Interanl server error"})
}

module.exports = server
Loading

0 comments on commit 6d6c743

Please sign in to comment.