Skip to content
Permalink
Browse files

💥 Re-factored the services response to just return an array, added a …

…couple new routes
  • Loading branch information
crock committed Dec 8, 2019
1 parent 6c4f397 commit 0aaaa6d777f6b7718ec0956cd80aa0c218b4e928
Showing with 1,744 additions and 4,426 deletions.
  1. +0 −29 APICHANGES.md
  2. +1 −1 Procfile
  3. +26 −24 app.js
  4. +36 −0 index.html
  5. +1,563 −0 package-lock.json
  6. +1 −5 package.json
  7. +117 −119 services.json
  8. +0 −1,950 yarn-error.log
  9. +0 −2,298 yarn.lock

This file was deleted.

@@ -1 +1 @@
web=npm start
web=npm run start
50 app.js
@@ -9,7 +9,6 @@ const bodyParser = require('body-parser');
const timeout = require('connect-timeout');
const apicache = require('apicache');
const redis = require('redis');
const fs = require("fs")

// Load services.json
const advanced = require('./services.json');
@@ -25,25 +24,20 @@ app.use(bodyParser.urlencoded({ extended: false }));
app.use(haltOnTimedout);
app.use(function (req, res, next) {
res.header('Content-Type', 'application/json');
res.header('x-powered-by', 'github.com/checker')
res.header('x-powered-by', 'checkerapi.com')
next();
});

const loadLibs = (callback) => {
// This isn't great, but it works.
advanced["services"].forEach(service => {
advanced.forEach(service => {
console.log(`[BOOTSTRAP] Bootstrapping ${service.name} (${service.slug})`)
modules[service.slug] = {"manifest": service, "instance": require(`./${service.libPath}`)}
})
console.log(`[BOOTSTRAP] Module start finished, loaded ${modules.length} modules.`)
callback()
}

function goToDocs(req, res) {
const targetUrl = "https://app.swaggerhub.com/apis-docs/CrocBuzz/penguin-api/";
res.redirect(targetUrl);
}

function checkAuthKey(req, res, next) {
var keys = require('./apikeys.json');
var appkey = req.get('og-apikey');
@@ -58,41 +52,49 @@ function checkAuthKey(req, res, next) {

let cacheWithRedis = apicache.options({ redisClient: redis.createClient(process.env.REDIS_URL) }).middleware;

/* GET root api endpoint */
router.get('/', goToDocs);
router.get('/', function(req, res) {
return res.sendFile(__dirname + '/index.html');
});

router.get('/gh', function(req, res) {
return res.redirect("https://github.com/checker/api/");
});

router.get('/docs', function(req, res) {
return res.redirect("https://app.swaggerhub.com/apis/checker/api/");
});

router.get('/check/services', function(req, res) {
// Note: see APICHANGES.md for possible changes
var simple = {"services":[]};
for (var key in advanced.services) {
simple.services.push(advanced.services[key].slug)
var simple = [];
for (var key in advanced) {
simple.push(advanced[key].slug)
}
res.json(simple);
});

router.get('/check/services/details', function(req, res) {
return res.json(advanced)
});

router.get('/check/:service', function(req, res) {
var service = req.params.service;
var json = {};
for (var key in advanced.services) {
if (service === advanced.services[key].slug) {
json = advanced.services[key];
for (var key in advanced) {
if (service === advanced[key].slug) {
json = advanced[key];
break;
}
}
res.json(json)
});

router.get('/check/services/details', function(req, res) {
var json = require('./services.json');
res.json(json)
return res.json(json)
});

router.get('/check/:service/:word', [cacheWithRedis('6 hours')], function(req, res) {
var service = req.params.service;
var word = req.params.word;
if (modules[service] == undefined) {
res.status(400)
res.json({"error": "INVALID_SERVICE", "message": "Invalid service selected, please see /check/services for a list."})
return res.json({"error": "INVALID_SERVICE", "message": "Invalid service selected, please see /check/services for a list."})
}else {
// Seemingly valid service, request data on 'word':
modules[service].instance(word, (status, timestamp, statusBreakdown) => {
@@ -104,7 +106,7 @@ router.get('/check/:service/:word', [cacheWithRedis('6 hours')], function(req, r
"timestamp": timestamp, // Return an MS epoch time
"statusBreakdown": statusBreakdown || undefined // Return the breakdown (or undefined)
}
res.json(json)
return res.json(json)
})
}
});
@@ -0,0 +1,36 @@
<html>
<head>
<title>gh: checker/api</title>
</head>
<style>
body {
margin: 0;
padding: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
font-size: 16px;
display: flex;
}
.wrapper {
width: 75%;
margin: 0 auto;
align-self: center;
text-align: center;
}
.links > a,span { display:block; }
</style>
<body>
<div class="wrapper">
<h1>Username Checker API</h1>
<div>(Base Route)</div>
<p>
This is the base route for the checker api, aka the Penguin API.
I don't intend on making this page pretty as I don't have a lot of time anymore.
However, if you want to contribute, the project is open-source on GitHub. Just fork it and submit a pull request.
</p>
<div class="links">
<a href="/gh">GitHub Repository</a>
<span><a href="/docs">API Documentation</a> via Swagger</span>
</div>
</div>
</body>
</html>

0 comments on commit 0aaaa6d

Please sign in to comment.
You can’t perform that action at this time.