Skip to content

Read app path from applications file #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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": "latest"
},
"globals": {
"BigInt": true
Expand Down
22 changes: 15 additions & 7 deletions main.js
Original file line number Diff line number Diff line change
@@ -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] });
})();
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