From f6275b099833cd1bfdfd74326101313e51e9bce0 Mon Sep 17 00:00:00 2001 From: Nikiroy78 <35032449+Nikiroy78@users.noreply.github.com> Date: Tue, 3 Oct 2023 02:50:16 +0300 Subject: [PATCH] . --- config.json | 2 +- database.js | 138 +++++++++++++++++++++++++++++++--------------------- logger.js | 66 ++++++++++++------------- server.js | 20 ++++---- 4 files changed, 125 insertions(+), 101 deletions(-) diff --git a/config.json b/config.json index 0eaa747..4ea7ddd 100644 --- a/config.json +++ b/config.json @@ -8,7 +8,7 @@ }, "database" : { "port" : 5433, - "address" : "localhost", + "address" : "localhost", "username" : "postgres", "password" : "root", "database" : "kodex-db" diff --git a/database.js b/database.js index 448d1fb..2d56b7b 100644 --- a/database.js +++ b/database.js @@ -1,6 +1,7 @@ const { Sequelize, DataTypes } = require("sequelize"); +const config = require('./config-handler'); -class Table { +/*class Table { constructor(model) { this.model = model; } @@ -58,7 +59,7 @@ class TablePromise extends Table { async edit(data, condition) { return await this.model.update(data, condition); } -} +}*/ class Database { constructor(address, port, username, password, database) { @@ -67,62 +68,83 @@ class Database { ); this.sequelize.authenticate().then( () => { - this.authors = new Table( - this.sequelize.define( - "authors", - { - id: { - type: DataTypes.INTEGER, - primaryKey: true, - autoIncrement: true, - allowNull: false, - }, - name: { - type: DataTypes.TEXT, - allowNull: false, - }, - time: { - type: DataTypes.INTEGER, - allowNull: false, - }, + // this.authors = new Table( + // this.sequelize.define( + // "authors", + // { + // id: { + // type: DataTypes.INTEGER, + // primaryKey: true, + // autoIncrement: true, + // allowNull: false, + // }, + // name: { + // type: DataTypes.TEXT, + // allowNull: false, + // }, + // time: { + // type: DataTypes.INTEGER, + // allowNull: false, + // }, + // }, + // { + // freezeTableName: true, + // timestamps: false, + // }, + // ), + // ); + this.authors = this.sequelize.define( + "authors", + { + id: { + type: DataTypes.INTEGER, + primaryKey: true, + autoIncrement: true, + allowNull: false, }, - { - freezeTableName: true, - timestamps: false, + name: { + type: DataTypes.TEXT, + allowNull: false, }, - ), - ); - this.muzic = new Table( - this.sequelize.define( - "muzic", - { - id: { - type: DataTypes.INTEGER, - primaryKey: true, - autoIncrement: true, - allowNull: false, - }, - name: { - type: DataTypes.TEXT, - allowNull: false, - }, - author_id: { - type: DataTypes.INTEGER, - allowNull: false, - }, - data: { - type: DataTypes.BLOB("long"), - }, - time: { - type: DataTypes.INTEGER, - allowNull: false, - }, + time: { + type: DataTypes.INTEGER, + allowNull: false, }, - { - freezeTableName: true, - timestamps: false, + }, + { + freezeTableName: true, + timestamps: false, + }, + ) + this.music = this.sequelize.define( + "music", + { + id: { + type: DataTypes.INTEGER, + primaryKey: true, + autoIncrement: true, + allowNull: false, }, - ), + name: { + type: DataTypes.TEXT, + allowNull: false, + }, + author_id: { + type: DataTypes.INTEGER, + allowNull: false, + }, + data: { + type: DataTypes.BLOB("long"), + }, + time: { + type: DataTypes.INTEGER, + allowNull: false, + }, + }, + { + freezeTableName: true, + timestamps: false, + }, ); console.log("Database successful connected!"); }, @@ -133,4 +155,10 @@ class Database { } } -module.exports = { Database }; +module.exports = new Database( + config().database.address, + config().database.port, + config().database.username, + config().database.password, + config().database.database +); diff --git a/logger.js b/logger.js index b92da74..1863f0b 100644 --- a/logger.js +++ b/logger.js @@ -1,40 +1,36 @@ const config = require("./config-handler"); const fs = require("fs"); -function log(date, req, ip, res) { - const requestBody = !req.byteBody.toString("utf-8") - ? "" - : ` -~~~~~~~~~~~~~~ -[REQUEST BODY] -~~~~~~~~~~~~~~ -${req.byteBody.toString("utf-8")}`; - let action = `HTTP ${req.httpVersion} ${req.method} ${req.originalUrl} -~~~~~~~~~ -[HEADERS] -~~~~~~~~~ -${Object.entries(req.headers) - .map(([header, value]) => header + ": " + value) - .join("\n")}${requestBody}`; - let response = !res.isError - ? "" - : `\n----------------\nError raised:\n----------------\n${JSON.stringify( - res.responseData, - )}`; - //console.log(`================================\nREPORT\n================================\n\nIP: ${ip}\n----------------\nACTION:\n----------------\n${action}${response}`); - let loggerFolder = config().logger_folder; - loggerFolder = !["/", "\\"].includes(loggerFolder.at(-1)) - ? loggerFolder + "/" - : loggerFolder; - - fs.writeFileSync( - `${loggerFolder}${date.getTime()}.log`, - `================================\nREPORT\n================================\n\nIP: ${ip}\n----------------\nACTION:\n----------------\n${action}${response}`, - ); +class Logger { + constructor () { + this.logId = `${(new Date()).getTime()}`; + let loggerFolder = config().logger_folder; + loggerFolder = !["/", "\\"].includes(loggerFolder.at(-1)) + ? loggerFolder + "/" + : loggerFolder; + fs.writeFile( + `${loggerFolder}${this.logId}.log`, '', + { + encoding: "utf8" + } + (err) => { + throw err; + } + ); + } + + log (date, ip, action, isError = false) { + fs.writeFile( + `${loggerFolder}${this.logId}.log`, !isError ? `IP: ${ip}\nДата запроса: ${date}` : `\nПроизошла ошибка: ${action}`, + { + encoding : "utf8", + flag : "a" + } + (err) => { + throw err; + } + ); + } } -module.exports = async (req, res, next) => { - // console.log('ip', req.ip); - log(new Date(), req, req.ip, res); - next(); -}; \ No newline at end of file +module.exports = async (req, res, next) => { Logger }; \ No newline at end of file diff --git a/server.js b/server.js index 4033b70..fc26bca 100644 --- a/server.js +++ b/server.js @@ -3,20 +3,20 @@ const express = require("express"); const bodyHand = require("body"); const config = require("./config-handler"); -const http = require("http"); +// const http = require("http"); const { Database } = require("./database"); const app = express(); -global.database = new Database( - config().database.address, - config().database.port, - config().database.username, - config().database.password, - config().database.database, -); +// global.database = new Database( + // config().database.address, + // config().database.port, + // config().database.username, + // config().database.password, + // config().database.database +// ); -http.ServerResponse.prototype.errorModeOn = function () { +/*http.ServerResponse.prototype.errorModeOn = function () { this.isError = true; }; @@ -62,7 +62,7 @@ app.use((req, res, next) => { next(); }, ); -}); +});*/ app.use("/", require("./page-view")); app.use("/api", require("./api"));