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

@ -8,7 +8,7 @@
},
"database" : {
"port" : 5433,
"address" : "localhost",
"address" : "localhost",
"username" : "postgres",
"password" : "root",
"database" : "kodex-db"

View File

@ -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
);

View File

@ -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();
};
module.exports = async (req, res, next) => { Logger };

View File

@ -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"));