diff --git a/.eslintrc.json b/.eslintrc.json index 69eb1e4..d164636 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -6,7 +6,7 @@ }, "extends": "eslint:recommended", "parserOptions": { - "ecmaVersion": 2020 + "ecmaVersion": "latest" }, "globals": { "BigInt": true diff --git a/main.js b/main.js index b194a82..4308a24 100644 --- a/main.js +++ b/main.js @@ -1,25 +1,33 @@ 'use strict'; -const fastify = require('fastify'); +const fsp = require('node:fs/promises'); -const path = require('node:path'); +const fastify = require('fastify'); const { Logger, StreamForLogger } = require('./src/logger.js'); const http = require('./src/http.js'); const ws = require('./src/ws.js'); const { loadApplication } = require('./src/loader.js'); -const APPLICATION_PATH = path.join(process.cwd(), '../NodeJS-Application'); const LOG_FOLDER_PATH = './log'; +const getAppPaths = async () => { + const apps = await fsp.readFile('.applications', 'utf8'); + return apps.split(/[\r\n\s]+/).filter((s) => s.length !== 0); +}; + (async () => { - const streamForLogger = new StreamForLogger(LOG_FOLDER_PATH); - const server = fastify({ logger: { level: 'info', stream: streamForLogger } }); + const stream = new StreamForLogger(LOG_FOLDER_PATH); + const server = fastify({ + logger: { level: 'info', stream }, + }); const logger = new Logger(server.log); - const app = await loadApplication(APPLICATION_PATH, logger); + + const appPath = (await getAppPaths())[0]; + const app = await loadApplication(appPath, logger); http.init(server, app.api); - http.initStatic(server, APPLICATION_PATH); + http.initStatic(server, appPath); ws.init(server, app.api); http.start(server, { port: app.config.server.ports[0] }); })(); diff --git a/src/http.js b/src/http.js index b9dc4f1..99bb4b0 100644 --- a/src/http.js +++ b/src/http.js @@ -2,7 +2,7 @@ const path = require('node:path'); -const fastigyStatic = require('@fastify/static'); +const fastifyStatic = require('@fastify/static'); function init(server, routes) { /* TODO: session support */ @@ -24,7 +24,7 @@ function init(server, routes) { function initStatic(server, appPath) { const staticPath = path.join(appPath, 'static'); - server.register(fastigyStatic, { + server.register(fastifyStatic, { root: staticPath, wildcard: true, }); diff --git a/src/logger.js b/src/logger.js index 1fd761b..09922eb 100644 --- a/src/logger.js +++ b/src/logger.js @@ -47,7 +47,7 @@ class StreamForLogger { constructor(folderPath) { this.folderPath = folderPath; - this.date = new Date().toISOString().substring(0, 10); + this.date = new Date().toISOString().substring(0, 10); this.#createFileStream(); } @@ -69,7 +69,7 @@ class StreamForLogger { this.#logFileStream = fs.createWriteStream(filePath, { flags: 'a' }); return this.#logFileStream; } -}; +} module.exports = { Logger,