2020-03-25 17:04:52 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
|
|
|
* This file is part of Requestor which is released under BSD-3-Clause.
|
|
|
|
* See file 'LICENSE' for full license details.
|
|
|
|
* Created by Timo Hocker <timo@scode.ovh>, March 2020
|
|
|
|
*/
|
|
|
|
|
2020-01-03 19:13:21 +01:00
|
|
|
/* eslint-disable no-console */
|
|
|
|
/* eslint-disable no-sync */
|
2020-01-30 10:51:05 +01:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2020-01-03 19:13:21 +01:00
|
|
|
const fs = require ('fs');
|
|
|
|
const path = require ('path');
|
2019-12-10 15:59:12 +01:00
|
|
|
|
2019-12-11 13:07:48 +01:00
|
|
|
/**
|
2020-01-03 19:13:21 +01:00
|
|
|
* @typedef {object} options
|
2019-12-11 13:07:48 +01:00
|
|
|
* @property {any} [opts] object to pass to the handlers
|
|
|
|
* @property {string} [subdir] subdirectory for all requests
|
|
|
|
* @property {boolean} [verbose] enable verbose logging
|
2020-01-09 08:33:28 +01:00
|
|
|
* @property {boolean} [rethrow] rethrow errors (default: true)
|
2019-12-11 13:07:48 +01:00
|
|
|
*/
|
|
|
|
|
2020-01-09 20:56:04 +01:00
|
|
|
/**
|
|
|
|
* @typedef {object} handler_description
|
|
|
|
* @property {string} module_folder folder the module file is in
|
|
|
|
* @property {string} file name of the module
|
|
|
|
* @property {any} opts optional arguments
|
|
|
|
* @property {boolean} rethrow should errors be rethrown
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* wrap a requestor handler to be compatible with express
|
|
|
|
*
|
|
|
|
* @param {handler_description} data handler data
|
|
|
|
* @returns {Function} requestor handler
|
|
|
|
*/
|
|
|
|
function get_handler ({ module_folder, file, opts, rethrow }) {
|
|
|
|
// eslint-disable-next-line global-require
|
|
|
|
const handler = require (path.join (process.cwd (), module_folder, file));
|
|
|
|
|
2020-03-26 13:31:07 +01:00
|
|
|
return (req, res, next) => new Promise (
|
|
|
|
(resolve) => resolve (handler (req, res, next, opts))
|
|
|
|
)
|
|
|
|
.catch ((e) => {
|
|
|
|
if (rethrow)
|
|
|
|
throw e;
|
|
|
|
});
|
2020-01-09 20:56:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* register a handler to the given app
|
|
|
|
*
|
|
|
|
* @param {any} app express app
|
|
|
|
* @param {handler_description} handler_description data for the used handler
|
|
|
|
* @param {string} method method to respond to
|
|
|
|
* @param {string} url url to respond to
|
|
|
|
* @param {boolean} verbose should verbose logging be enabled
|
|
|
|
*/
|
|
|
|
function register_handler (
|
|
|
|
app,
|
|
|
|
handler_description,
|
|
|
|
method,
|
|
|
|
url,
|
|
|
|
verbose
|
|
|
|
) {
|
|
|
|
const handler = get_handler (handler_description);
|
|
|
|
|
2020-02-23 18:52:34 +01:00
|
|
|
if (verbose) {
|
2020-01-09 20:56:04 +01:00
|
|
|
console.log (
|
|
|
|
`[requestor info] redirecting ${url} to ${handler_description.file}`
|
|
|
|
);
|
2020-02-23 18:52:34 +01:00
|
|
|
}
|
2020-01-09 20:56:04 +01:00
|
|
|
|
|
|
|
switch (method) {
|
|
|
|
case 'post':
|
|
|
|
app.post (url, handler);
|
|
|
|
break;
|
|
|
|
case 'get':
|
|
|
|
app.get (url, handler);
|
|
|
|
break;
|
|
|
|
case 'put':
|
|
|
|
app.put (url, handler);
|
|
|
|
break;
|
|
|
|
case 'delete':
|
|
|
|
app.delete (url, handler);
|
|
|
|
break;
|
|
|
|
case 'all':
|
|
|
|
app.all (url, handler);
|
|
|
|
break;
|
|
|
|
default:
|
2020-02-23 18:52:34 +01:00
|
|
|
if (verbose) {
|
2020-01-09 20:56:04 +01:00
|
|
|
console.warn (
|
|
|
|
`'${method}' did not match any request method, ignoring`
|
|
|
|
);
|
2020-02-23 18:52:34 +01:00
|
|
|
}
|
2020-01-09 20:56:04 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 15:59:12 +01:00
|
|
|
/**
|
|
|
|
* Load all request handlers in the given folder
|
|
|
|
*
|
2019-12-11 09:32:40 +01:00
|
|
|
* @param {any} app express app
|
2020-01-09 20:56:04 +01:00
|
|
|
* @param {string} module_folder folder that contains all modules
|
2020-01-03 19:13:21 +01:00
|
|
|
* @param {options} options additional options
|
2019-12-10 15:59:12 +01:00
|
|
|
*/
|
2020-01-03 19:13:21 +01:00
|
|
|
module.exports = function main (
|
2019-12-11 12:57:05 +01:00
|
|
|
app,
|
2020-01-09 20:56:04 +01:00
|
|
|
module_folder,
|
2020-01-09 08:33:28 +01:00
|
|
|
options = { opts: null, subdir: '', verbose: false, rethrow: true }
|
2019-12-11 12:57:05 +01:00
|
|
|
) {
|
2020-01-09 08:33:28 +01:00
|
|
|
const { opts, subdir, verbose, rethrow } = options;
|
2019-12-11 12:55:30 +01:00
|
|
|
|
2020-01-09 20:56:04 +01:00
|
|
|
for (const file of fs.readdirSync (module_folder)) {
|
2020-01-03 19:13:21 +01:00
|
|
|
const regex = /(?<method>.*?)-(?<url>.*?)\.js/u;
|
2020-01-09 20:56:04 +01:00
|
|
|
const { groups } = regex.exec (file);
|
2020-01-08 08:47:35 +01:00
|
|
|
|
2020-01-09 20:34:14 +01:00
|
|
|
if (typeof subdir === 'undefined')
|
2020-01-08 08:47:35 +01:00
|
|
|
groups.url = `/${groups.url}/`;
|
|
|
|
else
|
2020-01-08 08:42:55 +01:00
|
|
|
groups.url = `/${subdir}/${groups.url}/`;
|
2020-01-08 08:47:35 +01:00
|
|
|
|
2020-01-03 19:13:21 +01:00
|
|
|
groups.url = groups.url
|
|
|
|
.replace (/^\/[^/]*\/root/iu, '/')
|
|
|
|
.replace (/\./gu, '/')
|
|
|
|
.replace (/\/+/gu, '/');
|
2019-12-10 15:59:12 +01:00
|
|
|
|
2020-01-09 20:56:04 +01:00
|
|
|
register_handler (
|
|
|
|
app,
|
|
|
|
{
|
|
|
|
file,
|
|
|
|
module_folder,
|
|
|
|
opts,
|
|
|
|
rethrow
|
|
|
|
},
|
|
|
|
groups.method,
|
|
|
|
groups.url,
|
|
|
|
verbose
|
|
|
|
);
|
2019-12-10 15:59:12 +01:00
|
|
|
}
|
2019-12-11 09:32:40 +01:00
|
|
|
};
|