Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 2020
"ecmaVersion": 2022
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"ecmaVersion": 2022
"ecmaVersion": "latest"

},
"globals": {
"BigInt": true
Expand Down
25 changes: 19 additions & 6 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
'use strict';

const fastify = require('fastify');
const { resolve } = require('node:path');
const { readFile } = require('node:fs/promises');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to have compound identifiers like path.resolve in codebase below


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 APPLICATIONS_FILE_PATH = '.applications';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need no constant, string content is easy to understand

const LOG_FOLDER_PATH = './log';

const getAppPaths = async (appFilePath) => {
const appsFile = await readFile(resolve(appFilePath), {
encoding: 'utf8',
});

return appsFile.split('\n').filter((path) => path.trim() !== '');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const appsFile = await readFile(resolve(appFilePath), {
encoding: 'utf8',
});
return appsFile.split('\n').filter((path) => path.trim() !== '');
const appsFile = path.resolve('.applications')
const apps = await readFile(appsFile, 'utf8');
return apps.split('[\r\n\s]+').filter((s) => s.length !== 0);

};

(async () => {
const streamForLogger = new StreamForLogger(LOG_FOLDER_PATH);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const streamForLogger = new StreamForLogger(LOG_FOLDER_PATH);
const stream = new StreamForLogger(LOG_FOLDER_PATH);

const server = fastify({ logger: { level: 'info', stream: streamForLogger } });
const server = fastify({
logger: { level: 'info', stream: streamForLogger },
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const server = fastify({
logger: { level: 'info', stream: streamForLogger },
});
const server = fastify({ logger: { level: 'info', stream } });

const logger = new Logger(server.log);
const app = await loadApplication(APPLICATION_PATH, logger);

const appPath = (await getAppPaths(APPLICATIONS_FILE_PATH))[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] });
})();
4 changes: 2 additions & 2 deletions src/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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,
});
Expand Down
4 changes: 2 additions & 2 deletions src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -69,7 +69,7 @@ class StreamForLogger {
this.#logFileStream = fs.createWriteStream(filePath, { flags: 'a' });
return this.#logFileStream;
}
};
}

module.exports = {
Logger,
Expand Down