Skip to content

Commit

Permalink
Homework #1 is done
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyampo committed Dec 26, 2023
1 parent 850c07f commit 1a768ae
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 20 deletions.
5 changes: 3 additions & 2 deletions JavaScript/9-logger/api/user.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict';
const {db, common} = require('../api.di.container');
const {db, common, console} = require('../api.di.container');

module.exports = {
async read(id) {
return await db('users').read(id, ['id', 'login']);
},

async create({login, password}) {
console.log({login});
const passwordHash = await common.hash(password);
return await db('users').create({login, password: passwordHash});
},
Expand All @@ -24,4 +25,4 @@ module.exports = {
const sql = 'SELECT login from users where login like $1';
return await db('users').query(sql, [mask]);
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ class Logger {
}
}

module.exports = new Logger('./log');
module.exports = new Logger('./log');
2 changes: 1 addition & 1 deletion JavaScript/9-logger/logger.di.container.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = Object.freeze(require('./logger'));
module.exports = Object.freeze(require('./pino-logger'));
13 changes: 2 additions & 11 deletions JavaScript/9-logger/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,7 @@ const config = require('./config.js');
const server = require(`./${config.apiServer.transport}.js`);
const staticServer = require('./static.js');
const load = require('./load.js');
const db = require('./db.js');
const hash = require('./hash.js');
const logger = require('./logger.js');


const sandbox = {
console: Object.freeze(logger),
db: Object.freeze(db),
common: {hash},
};
const apiPath = path.join(process.cwd(), './api');
const routing = {};

Expand All @@ -25,9 +16,9 @@ const routing = {};
if (!fileName.endsWith('.js')) continue;
const filePath = path.join(apiPath, fileName);
const serviceName = path.basename(fileName, '.js');
routing[serviceName] = await load(filePath, sandbox);
routing[serviceName] = require(filePath);
}

staticServer('./static', config.staticServer.port);
server(routing, config.apiServer.port);
})();
})();
49 changes: 49 additions & 0 deletions JavaScript/9-logger/pino-logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

const pino = require('pino');
const util = require('node:util');
const path = require('node:path');
const fs = require("node:fs");

class Logger {
constructor(logPath) {
const date = new Date().toISOString().substring(0, 10);
const filePath = path.join(logPath, `${date}.log`);
this.logger = pino.pino(pino.destination(filePath));
}

close() {
}

log(...args) {
const msg = util.format(...args);
this.logger.info(msg);
}

dir(...args) {
const msg = util.inspect(...args);
this.logger.info(msg);
}

debug(...args) {
const msg = util.format(...args);
this.logger.debug(msg);
}

error(...args) {
const msg = util.format(...args).replace(/[\n\r]{2,}/g, '\n');
this.logger.error('error', msg.replace(this.regexp, ''));
}

system(...args) {
const msg = util.format(...args);
this.logger.fatal(msg);
}

access(...args) {
const msg = util.format(...args);
this.logger.fatal(msg);
}
}

module.exports = new Logger('./log');
10 changes: 5 additions & 5 deletions JavaScript/9-logger/static/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const httpScaffold = (url, structure) => {
return api;
};

const wssScaffold = (url, structure) => {
const wsScaffold = (url, structure) => {
const api = {};
const services = Object.keys(structure);
for (const serviceName of services) {
Expand All @@ -71,11 +71,11 @@ const wssScaffold = (url, structure) => {
const scaffold = (url, structure) => {
const protocol = new URL(url).protocol;
if (protocol === 'http:') return httpScaffold(url, structure);
if (protocol === 'ws:') return wssScaffold(url, structure);
if (protocol === 'ws:') return wsScaffold(url, structure);
throw new Error(`Unknown protocol ${protocol}`);
};

const api = scaffold('ws://localhost:3000', {
const api = scaffold('http://localhost:3000', {
user: {
create: ['record'],
read: ['id'],
Expand All @@ -91,7 +91,7 @@ const api = scaffold('ws://localhost:3000', {
});

(async () => {
const [{id}] = await api.user.create({login: 'rand1', password: 'test'});
const [{id}] = await api.user.create({login: (Math.random() + 1).toString().substring(1), password: 'test'});
const [user] = await api.user.read(id);
console.log(user);
})();
})();

0 comments on commit 1a768ae

Please sign in to comment.