Skip to content

Commit

Permalink
Merge pull request #126 from Arquisoft/123-update-api-doc-with-new-en…
Browse files Browse the repository at this point in the history
…dpoints

123 update api doc with new endpoints
  • Loading branch information
uo289267 authored Apr 25, 2024
2 parents c32009e + 2835435 commit 287aabf
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 431 deletions.
42 changes: 23 additions & 19 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
manageError(error)
manageError(res, error)
}
});

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

}
});
Expand All @@ -55,7 +55,7 @@ app.get('/questions', verifyToken, async (req, res) => {
const questionResponse = await axios.get(questionServiceUrl+'/questions');
res.json(questionResponse.data);
} catch (error) {
manageError(error)
manageError(res, error)
}
});

Expand All @@ -78,7 +78,7 @@ app.get('/questions/:lang/:amount/:type', verifyToken, async (req, res) => {
}
} catch (error) {

manageError(error)
manageError(res, error)
}
});

Expand All @@ -97,7 +97,7 @@ app.get('/questions/:lang/:amount', verifyToken, async (req, res) => {
res.json(questionResponse.data);
}
} catch (error) {
manageError(error)
manageError(res, error)
}
});

Expand All @@ -115,7 +115,7 @@ app.get('/questions/:lang', verifyToken, async (req, res) => {

} catch (error) {

manageError(error)
manageError(res, error)
}
});

Expand All @@ -126,7 +126,7 @@ app.post('/record', verifyToken, async(req, res) => {
const recordResponse = await axios.post(recordServiceUrl+'/record', req.body);
res.json(recordResponse.data);
} catch (error) {
manageError(error)
manageError(res, error)
}
});

Expand All @@ -136,7 +136,7 @@ app.get('/record/ranking/top10', verifyToken, async(req, res)=>{
const recordResponse = await axios.get(recordServiceUrl + '/record/ranking/top10');
res.json(recordResponse.data);
} catch (error) {
manageError(error)
manageError(res, error)
}
});

Expand All @@ -151,7 +151,7 @@ app.get('/record/ranking/:user', verifyToken, async(req, res)=>{
res.json(recordResponse.data);
}
} catch (error) {
manageError(error)
manageError(res, error)
}
});

Expand All @@ -166,20 +166,24 @@ app.get('/record/:user', verifyToken, async(req, res)=>{
res.json(recordResponse.data);
}
} catch (error) {
manageError(error)
manageError(res, error)
}
});

// Read the OpenAPI YAML file synchronously
const file = fs.readFileSync('./openapi.yaml', 'utf8');
const openapiPath = './openapi.yaml'
if(fs.existsSync(openapiPath)){
const file = fs.readFileSync(openapiPath, 'utf8');

// Parse the YAML content into a JavaScript object representing the Swagger document
const swaggerDocument = YAML.parse(file);
// Parse the YAML content into a JavaScript object representing the Swagger document
const swaggerDocument = YAML.parse(file);

// Serve the Swagger UI documentation at the '/api-doc' endpoint
// This middleware serves the Swagger UI files and sets up the Swagger UI page
// It takes the parsed Swagger document as input
app.use('/api-doc', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
}

// Serve the Swagger UI documentation at the '/api-doc' endpoint
// This middleware serves the Swagger UI files and sets up the Swagger UI page
// It takes the parsed Swagger document as input
app.use('/api-doc', swaggerUi.serve, swaggerUi.setup(swaggerDocument));

// Start the gateway service
const server = app.listen(port, () => {
Expand All @@ -206,7 +210,7 @@ function verifyToken(req, res, next) {
}

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

function validateAmount(amount) {
Expand All @@ -223,7 +227,7 @@ function validateUser(user){
return !(/\s/.test(user)) //True if there are no spaces
}

function manageError(error){
function manageError(res, 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
Expand Down
Loading

0 comments on commit 287aabf

Please sign in to comment.