diff --git a/index.js b/index.js index 81fe3de..2d3f734 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,5 @@ -const { readJSONSync } = require('fs-extra'); -const config = readJSONSync('./config.json'); const Formidable = require('./src/Formidable.js'); -const host = process.env.HOST ?? config.host ?? '0.0.0.0'; -const port = process.env.PORT ?? config.port; - new Formidable() .load() - .listen(host, port); \ No newline at end of file + .listen(); \ No newline at end of file diff --git a/src/Config.js b/src/Config.js index e69de29..4db2cbd 100644 --- a/src/Config.js +++ b/src/Config.js @@ -0,0 +1,18 @@ +const port = !isNaN(Number(process.env.PORT)) ? Number(process.env.PORT) : 1024; +const distance = !isNaN(Number(process.env.DISTANCE)) ? Number(process.env.DISTANCE) : 0.5; +const threads = !isNaN(Number(process.env.THREADS)) ? Number(process.env.THREADS) : 'auto'; +const maxQueue = !isNaN(Number(process.env.MAX_QUEUE)) ? Number(process.env.MAX_QUEUE) : 'auto'; +const maxResults = !isNaN(Number(process.env.MAX_RESULTS)) ? Number(process.env.MAX_RESULTS) : 10; +const autoUpdateInterval = !isNaN(Number(process.env.AUTO_UPDATE_INTERVAL)) ? Number(process.env.AUTO_UPDATE_INTERVAL) : 10; + +module.exports = { + level: process.env.LEVEL ?? 'info', + host: process.env.HOST ?? '0.0.0.0', + auth: process.env.AUTH, + port, + distance, + threads, + maxQueue, + maxResults, + autoUpdateInterval +}; \ No newline at end of file diff --git a/src/Formidable.js b/src/Formidable.js index 805c254..06d0f91 100644 --- a/src/Formidable.js +++ b/src/Formidable.js @@ -1,34 +1,37 @@ +const { readdirSync } = require('fs-extra'); + +const Os = require('os'); const Server = require('fastify'); + const Static = require('@fastify/static'); const Cors = require('@fastify/cors'); const Compress = require('@fastify/compress'); + const Pino = require('pino'); const Piscina = require('piscina'); const Path = require('path'); + const Limiter = require('./struct/Limiter.js'); -const { readdirSync, readJSONSync } = require('fs-extra'); -const { workers, maxQueue, abortTimeout } = require('./struct/Utils.js'); -const { level } = readJSONSync('./config.json'); +const Config = require('./Config.js'); -const config = readJSONSync('./config.json'); -const authorization = process.env.AUTHORIZATION ?? config.auth; +const { abortTimeout } = require('./struct/Utils.js'); class Formidable { constructor() { this.logger = new Pino( - { name: `Formidable [${process.pid}]`, level: level || 'info' }, + { name: `Formidable [${process.pid}]`, level: Config.level }, Pino.destination({ sync: false }) ); this.pool = new Piscina({ filename: `${__dirname}/FormidableWorker.js`, idleTimeout: 30000, minThreads: 0, - maxThreads: workers, - maxQueue + maxThreads: Config.threads === 'auto' ? Os.cpus().length : Config.threads, + maxQueue: Config.maxQueue }); this.endpoints = []; this.pool.on('error', error => this.logger.error(error)); - this.logger.info(`[Server] Using ${workers} workers`); + this.logger.info(`[Server] Using ${this.pool.maxThreads} workers`); this.server = Server(); this.server.addContentTypeParser('*', (_, body, done) => done(null, body)); this.server @@ -40,10 +43,10 @@ class Formidable { async handle(command, endpoint, request, reply) { try { let body = request.query; - if (!authorization && command.locked) { + if (!Config.auth && command.locked) { this.logger.warn(`[Server] ${endpoint} requires an auth, but user didn't set "auth" on config or AUTH in process enviroment variables`); } - if (authorization && command.locked && authorization !== request.headers?.authorization) { + if (Config.auth && command.locked && Config.auth !== request.headers?.authorization) { reply.code(401); return 'Unauthorized'; } @@ -183,10 +186,10 @@ class Formidable { return this; } - async listen(host, port) { - if (!port) this.logger.warn('[Server] User didn\'t set a port, will use a random open port'); - if (!authorization) this.logger.warn('[Server] User didn\'t set an auth, locked endpoints will execute without authorization'); - const address = await this.server.listen({ host, port }); + async listen() { + if (!Config.port) this.logger.warn('[Server] User didn\'t set a port, will use a random open port'); + if (!Config.auth) this.logger.warn('[Server] User didn\'t set an auth, locked endpoints will execute without authorization'); + const address = await this.server.listen({ host: Config.host, port: Config.port }); this.logger.info(`[Server] Server Loaded, Listening at ${address}`); } } diff --git a/src/struct/Cache.js b/src/struct/Cache.js index 21e600f..8c48736 100644 --- a/src/struct/Cache.js +++ b/src/struct/Cache.js @@ -1,7 +1,8 @@ const { watchFile } = require('fs'); const { readJSONSync } = require('fs-extra'); const { FILES, SEARCH, DIRECTORY, Create } = require('./Updater.js'); -const { distance } = readJSONSync('./config.json'); + +const Config = require('../Config.js'); const Fuse = require('fuse.js'); @@ -28,7 +29,7 @@ class Cache { this.data[key.toLowerCase()] = data; const keys = SEARCH[key]; if (!keys) return; - const dist = distance || 0.5; + const dist = Config.distance; this.fuse[key.toLowerCase()] = new Fuse(this.data[key.toLowerCase()], { keys, distance: dist }); } diff --git a/src/struct/Utils.js b/src/struct/Utils.js index 73068f0..7b592e3 100644 --- a/src/struct/Utils.js +++ b/src/struct/Utils.js @@ -1,11 +1,6 @@ const Https = require('https'); -const { cpus } = require('os'); -const { readJSONSync } = require('fs-extra'); -const { threads, maxQueue } = readJSONSync('./config.json'); module.exports = { - workers: isNaN(threads) ? Math.floor(50 * cpus().length / 100) : Number(threads), - maxQueue: maxQueue || 'auto', abortTimeout: 10000, fetch: (url, options) => new Promise((resolve, reject) => { const request = Https.request(url, options, response => {