Skip to content

Commit 56ed5df

Browse files
committed
Add some documentation to the hug router
1 parent 49b0ba5 commit 56ed5df

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

hug.router.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
// Hug Router
2+
// This handles all calls to the API
3+
14
const router = require('express').Router();
25
const axios = require('axios').default;
36
const xmlParser = require('xml2json');
47

8+
// This is how we choose which GIF to return
59
function getRandomArbitrary(max) {
610
return Math.round(Math.random() * max);
711
}
@@ -10,16 +14,21 @@ router.route('/').get((req, res) => {
1014

1115
var options = {method: 'GET', url: 'http://hugcdn.nyc3.digitaloceanspaces.com/'};
1216

17+
// Make the API request
1318
axios.request(options).then(function (response) {
19+
// Convert the XML response to a JS array
1420
const fullParsed = JSON.parse(xmlParser.toJson(response.data))
1521

22+
// Pick which GIF to return based off of the array
1623
const hug = fullParsed.ListBucketResult.Contents[getRandomArbitrary(fullParsed.ListBucketResult.Contents.length)]
1724
const stagedResponse = {
1825
url: `https://hug.cdn.bigbrother.group/${hug.Key}`,
1926
filename: hug.Key,
2027
size: hug.Size
2128
}
29+
// Let us know that a hug has been sent out, which is very useful for knowning the activity level of the API.
2230
console.log(`A hug has been sent out to ${res.req.ip}`);
31+
// Send back the gif and related information
2332
res.status(200).json(stagedResponse);
2433

2534
}).catch(function (error) {

0 commit comments

Comments
 (0)