Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Węglarek committed Jan 29, 2021
0 parents commit 6fc3015
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.git
package-lock.json
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <https://unlicense.org>
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# RandomAPI
RandomAPI is a super simple express API generating random numbers
# Usage
### URL queries
1. `min` (required) - min random number (inclusive) [must be >= 0 & < max]
1. `max` (required) - max random number (inclusive) [must be > min]
1. `num` (optional) - amount of random number to generate (default: 1e5)<br /><br />
Feel free to edit `config/config.json` to change: server port, request path, `num` limit
### HTTP Headers
**Request**<br />
`GET /?`min`=1&`max`=10&`num`=100 HTTP/1.1`<br /><br />
**Response**<br />
`HTTP/1.1 200 OK`<br />
### Response body
*Randomly generated array of numbers:*
<br />
`[0,9,1,9,5,...0,9,4,4,6]`<br /><br />
The amout of numbers is defined in the `num` request header
# Changelog
29.01.2021. - Initial release
5 changes: 5 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"port": 80,
"path": "/",
"numLimit": 1e5
}
13 changes: 13 additions & 0 deletions functions/sendResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = (response, code) => {
switch (code) {
case 400:
response.status(400).end("Bad Request", "utf-8");
break;
case 416:
response.status(416).end("Range Not Satisfiable", "utf-8");
break;
case 500:
response.status(500).end("Internal Server Error", "utf-8");
break;
}
}
33 changes: 33 additions & 0 deletions handlers/request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const config = require("../config/config.json");
const sendResponse = require("../functions/sendResponse.js");
module.exports = (request, response) => {
response.set({
"Access-Control-Allow-Origin": "*"
});
if (!request.query.min) sendResponse(response, 400);
else if (!request.query.max) sendResponse(response, 400);
else if (isNaN(request.query.min)) sendResponse(response, 400);
else if (isNaN(request.query.max)) sendResponse(response, 400);
else if (request.query.min < 0) sendResponse(response, 416);
else if (request.query.min > request.query.max) sendResponse(response, 416);
else if (!request.query.num) {
const min = request.query.min;
const max = request.query.max;
const random = Math.floor(Math.random() * (max - min + 1) + min);
response.status(200).end(random.toString());
} else {
if (isNaN(request.query.num)) sendResponse(response, 400);
else if (request.query.num < 1) sendResponse(response, 416);
else if (request.query.num > config.numLimit) sendResponse(response, 416);
else {
const min = request.query.min;
const max = request.query.max;
const numbers = [];
for (let i = 0; i < request.query.num; i++) {
const random = Math.floor(Math.random() * (max - min + 1) + min);
numbers.push(random);
}
response.status(200).end(JSON.stringify(numbers));
}
}
}
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const express = require("express");
const handleRequest = require("./handlers/request.js");
const config = require("./config/config.json");
const app = express();
app.get(config.path, handleRequest);
app.listen(config.port, () => console.log(`The server has started listening on port: ${config.port}`));
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "randomapi",
"version": "1.0.0",
"description": "RandomAPI is a super simple express API generating random numbers",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Wengiel",
"license": "Unlicense",
"dependencies": {
"express": "^4.17.1",
"fs": "^0.0.1-security"
}
}

0 comments on commit 6fc3015

Please sign in to comment.