1
0
Fork 0
auth.rxbn.de/bin/web/routes/api.js

42 lines
1.2 KiB
JavaScript

/*
* This file is part of the authRXBN single sign-on package.
*
* (c) Ruben Meyer <contact@rxbn.de>
*/
var path = require('path');
var fs = require('fs');
var express = require('express');
var route = express.Router();
asyncer = require('express-async-handler');
getRoutes = async () => {
// add all routes from files in api
let routesPath = path.join(global['__dirname'], '/bin/web/routes/api');
await addRoutesFromDirectory(routesPath);
return route;
};
addRoutesFromDirectory = async (filepath) => {
for await (const file of fs.readdirSync(filepath)) {
let joinedPath = path.join(filepath, file);
if(fs.statSync(joinedPath).isFile())
await addRoutesFromFile(joinedPath);
else if(fs.statSync(joinedPath).isDirectory())
await addRoutesFromDirectory(joinedPath);
}
};
addRoutesFromFile = async (filepath) => {
let file = require(filepath);
if(typeof file !== "object" || !file.hasOwnProperty("path")) return;
if(file.hasOwnProperty("all")) route.all(file.path, asyncer(file.all));
if(file.hasOwnProperty("get")) route.get(file.path, asyncer(file.get));
if(file.hasOwnProperty("post")) route.post(file.path, asyncer(file.post));
};
module.exports = {
getRoutes: getRoutes
};