Gum-Joe / 2Keys

A easy to setup second keyboard, designed for everyone.
GNU General Public License v3.0
11 stars 4 forks source link

Rate limiting of routes to prevent excessive FS ac... #127

Open github-actions[bot] opened 4 years ago

github-actions[bot] commented 4 years ago

Rate limiting of routes to prevent excessive FS access

https://github.com/Gum-Joe/2Keys/blob/c941b12a0011413a0e441a0ec6354ce808c9b792/packages/@twokeys/server/src/routes/api.ts#L48

 * Needed so we can use async/await
 */
// TODO: Rate limiting of routes to prevent excessive FS access
// TODO: Remove deprecated routes
export default async function getAPI(projectConfig: ProjectConfig, projectDir: string): Promise<Router> {
    logger.info("Preparing server...");

    // 1: Load config
    // TODO: Watch for changes to project and reload everything
    logger.info("Loading root config...");
    const mainConfig = await loadMainConfig(TWOKEYS_MAIN_CONFIG_DEFAULT_PATH);
    logger.info("Loading add-ons...");
    const registry = new AddOnsRegistry(mainConfig.registry_root);

    // Load add-ons & run startup functions for detectors
    const detectors = await loadDetectors(projectConfig, projectDir, registry);
    const executors = await loadExecutors(registry);

    /**
     * Returns to config for the 2Keys project
     */
    router.get("/get/config/project", (req, res) => {
        logger.info("Sending a config copy as JSON...");
        // We can rely on hot reload to ensure it is accurate
        res.statusCode = 200;
        res.json(mainConfig);
    });

    /**
     * Returns config for a detector
     */
    router.get("/get/config/detectors/:detector", (req, res) => {
        const detectorToGet = req.params.detector;
        logger.info(`Requested config for detector ${detectorToGet}`);
        if (detectors.has(detectorToGet)) {
            res.statusCode = 200;
            res.json(detectors.get(detectorToGet));
        } else {
            logger.info(`${detectorToGet} not found!`);
            res.statusCode = 404;
            res.json({
                message: "Not Found"
            });
        }
    });

    /**
     * Returns the config for the 2Keys project
        * @deprecated
     */
    router.get("/get/config", (req, res, next) => {
        logger.debug("Sending a config copy as JSON...");
        logger.warn("/get/config is deprecated.");
        readFile(join(process.cwd(), "config.yml"), (err, data) => {
            if (err) {
                return next(err);
ew file mode 100644
ndex 0000000..b55931d
++ b/packages/@twokeys/server/src/routes/loadDetectors.ts

746d9777d189893122d31def2897d3e7200cf7bf