This commit is contained in:
Nikiroy78 2023-10-03 02:50:16 +03:00
parent 725f91ea8c
commit f6275b0998
4 changed files with 125 additions and 101 deletions

View File

@ -1,6 +1,7 @@
const { Sequelize, DataTypes } = require("sequelize"); const { Sequelize, DataTypes } = require("sequelize");
const config = require('./config-handler');
class Table { /*class Table {
constructor(model) { constructor(model) {
this.model = model; this.model = model;
} }
@ -58,7 +59,7 @@ class TablePromise extends Table {
async edit(data, condition) { async edit(data, condition) {
return await this.model.update(data, condition); return await this.model.update(data, condition);
} }
} }*/
class Database { class Database {
constructor(address, port, username, password, database) { constructor(address, port, username, password, database) {
@ -67,8 +68,32 @@ class Database {
); );
this.sequelize.authenticate().then( this.sequelize.authenticate().then(
() => { () => {
this.authors = new Table( // this.authors = new Table(
this.sequelize.define( // 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", "authors",
{ {
id: { id: {
@ -90,11 +115,9 @@ class Database {
freezeTableName: true, freezeTableName: true,
timestamps: false, timestamps: false,
}, },
), )
); this.music = this.sequelize.define(
this.muzic = new Table( "music",
this.sequelize.define(
"muzic",
{ {
id: { id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
@ -122,7 +145,6 @@ class Database {
freezeTableName: true, freezeTableName: true,
timestamps: false, timestamps: false,
}, },
),
); );
console.log("Database successful connected!"); 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
);

View File

@ -1,40 +1,36 @@
const config = require("./config-handler"); const config = require("./config-handler");
const fs = require("fs"); const fs = require("fs");
function log(date, req, ip, res) { class Logger {
const requestBody = !req.byteBody.toString("utf-8") constructor () {
? "" this.logId = `${(new Date()).getTime()}`;
: `
~~~~~~~~~~~~~~
[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; let loggerFolder = config().logger_folder;
loggerFolder = !["/", "\\"].includes(loggerFolder.at(-1)) loggerFolder = !["/", "\\"].includes(loggerFolder.at(-1))
? loggerFolder + "/" ? loggerFolder + "/"
: loggerFolder; : loggerFolder;
fs.writeFile(
fs.writeFileSync( `${loggerFolder}${this.logId}.log`, '',
`${loggerFolder}${date.getTime()}.log`, {
`================================\nREPORT\n================================\n\nIP: ${ip}\n----------------\nACTION:\n----------------\n${action}${response}`, encoding: "utf8"
}
(err) => {
throw err;
}
); );
} }
module.exports = async (req, res, next) => { log (date, ip, action, isError = false) {
// console.log('ip', req.ip); fs.writeFile(
log(new Date(), req, req.ip, res); `${loggerFolder}${this.logId}.log`, !isError ? `IP: ${ip}\nДата запроса: ${date}` : `\nПроизошла ошибка: ${action}`,
next(); {
}; encoding : "utf8",
flag : "a"
}
(err) => {
throw err;
}
);
}
}
module.exports = async (req, res, next) => { Logger };

View File

@ -3,20 +3,20 @@
const express = require("express"); const express = require("express");
const bodyHand = require("body"); const bodyHand = require("body");
const config = require("./config-handler"); const config = require("./config-handler");
const http = require("http"); // const http = require("http");
const { Database } = require("./database"); const { Database } = require("./database");
const app = express(); const app = express();
global.database = new Database( // global.database = new Database(
config().database.address, // config().database.address,
config().database.port, // config().database.port,
config().database.username, // config().database.username,
config().database.password, // config().database.password,
config().database.database, // config().database.database
); // );
http.ServerResponse.prototype.errorModeOn = function () { /*http.ServerResponse.prototype.errorModeOn = function () {
this.isError = true; this.isError = true;
}; };
@ -62,7 +62,7 @@ app.use((req, res, next) => {
next(); next();
}, },
); );
}); });*/
app.use("/", require("./page-view")); app.use("/", require("./page-view"));
app.use("/api", require("./api")); app.use("/api", require("./api"));