mirror of
https://github.com/kmitresse/Compo-Service-Log-Project.git
synced 2026-05-13 17:11:49 +00:00
feat: Use Db with TypeORM to save logs
This commit is contained in:
@@ -4,8 +4,6 @@ services:
|
|||||||
mariadb:
|
mariadb:
|
||||||
image: mariadb:latest
|
image: mariadb:latest
|
||||||
restart: "always"
|
restart: "always"
|
||||||
# volumes:
|
|
||||||
# - ./mariaDB/init:/docker-entrypoint-initdb.d
|
|
||||||
environment:
|
environment:
|
||||||
MYSQL_ROOT_PASSWORD: root
|
MYSQL_ROOT_PASSWORD: root
|
||||||
MYSQL_DATABASE: db
|
MYSQL_DATABASE: db
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
CREATE DATABASE IF NOT EXISTS db_prod;
|
|
||||||
USE db_prod;
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS `web-log` (
|
|
||||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
|
||||||
`date` datetime NOT NULL,
|
|
||||||
`request` text NOT NULL,
|
|
||||||
`ip_user` varchar(16) NOT NULL,
|
|
||||||
PRIMARY KEY (`id`)
|
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS `nudgerData` (
|
|
||||||
`code` text NOT NULL UNIQUE,
|
|
||||||
`country` text
|
|
||||||
PRIMARY KEY (`code`)
|
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS `openfoodfactData` (
|
|
||||||
`code` varchar(20) NOT NULL UNIQUE,
|
|
||||||
`country` varchar(10) NOT NULL,
|
|
||||||
PRIMARY KEY (`code`)
|
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS `databasesCustom` (
|
|
||||||
`input` varchar(255) NOT NULL,
|
|
||||||
`output` varchar(255) NOT NULL,
|
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
|
||||||
Generated
+1223
-20
File diff suppressed because it is too large
Load Diff
@@ -21,7 +21,10 @@
|
|||||||
"dotenv": "^16.4.5",
|
"dotenv": "^16.4.5",
|
||||||
"express": "^4.21.0",
|
"express": "^4.21.0",
|
||||||
"fast-csv": "^5.0.1",
|
"fast-csv": "^5.0.1",
|
||||||
|
"mysql": "^2.18.1",
|
||||||
|
"reflect-metadata": "^0.2.2",
|
||||||
"tar-stream": "^3.1.7",
|
"tar-stream": "^3.1.7",
|
||||||
|
"typeorm": "^0.3.20",
|
||||||
"unzipper": "^0.12.3"
|
"unzipper": "^0.12.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import "reflect-metadata";
|
||||||
|
import { DataSource } from "typeorm";
|
||||||
|
import { Log } from "./entity/Log";
|
||||||
|
|
||||||
|
export const AppDataSource = new DataSource({
|
||||||
|
type: "mariadb",
|
||||||
|
host: "localhost",
|
||||||
|
port: 3306,
|
||||||
|
username: "root",
|
||||||
|
password: "root",
|
||||||
|
database: "db",
|
||||||
|
synchronize: true,
|
||||||
|
logging: true,
|
||||||
|
entities: [Log],
|
||||||
|
subscribers: [],
|
||||||
|
migrations: [],
|
||||||
|
});
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
|
||||||
|
|
||||||
|
@Entity()
|
||||||
|
export class Log {
|
||||||
|
@PrimaryGeneratedColumn()
|
||||||
|
id: number = 0;
|
||||||
|
|
||||||
|
@Column("timestamp")
|
||||||
|
timestamp: Date = new Date();
|
||||||
|
|
||||||
|
@Column("text")
|
||||||
|
url: string;
|
||||||
|
|
||||||
|
@Column("text")
|
||||||
|
method: "GET" | "POST" | "PUT" | "DELETE" = "GET";
|
||||||
|
|
||||||
|
@Column("blob")
|
||||||
|
body: string = "";
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
url: string,
|
||||||
|
method: "GET" | "POST" | "PUT" | "DELETE",
|
||||||
|
body: string
|
||||||
|
) {
|
||||||
|
this.url = url;
|
||||||
|
this.method = method;
|
||||||
|
this.body = body;
|
||||||
|
}
|
||||||
|
}
|
||||||
+3
-1
@@ -1,10 +1,12 @@
|
|||||||
import dotenv from "dotenv";
|
import dotenv from "dotenv";
|
||||||
import Server from "./Server";
|
import Server from "./Server";
|
||||||
import { DatasetCollection } from "./services/dataset";
|
import { DatasetCollection } from "./services/dataset";
|
||||||
|
import { AppDataSource } from "./AppDataSource";
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
DatasetCollection.loadAll()
|
AppDataSource.initialize()
|
||||||
|
.then(() => DatasetCollection.loadAll())
|
||||||
.then(() => console.log("All datasets are loaded"))
|
.then(() => console.log("All datasets are loaded"))
|
||||||
.then(() => new Server().start())
|
.then(() => new Server().start())
|
||||||
.catch(console.error);
|
.catch(console.error);
|
||||||
|
|||||||
@@ -1,10 +1,20 @@
|
|||||||
import { NextFunction, Request, Response } from "express";
|
import { NextFunction, Request, Response } from "express";
|
||||||
|
import { Log } from "../entity/Log";
|
||||||
|
import { AppDataSource } from "../AppDataSource";
|
||||||
|
|
||||||
export default function logger(
|
export default async function logger(
|
||||||
req: Request,
|
req: Request,
|
||||||
res: Response,
|
res: Response,
|
||||||
next: NextFunction
|
next: NextFunction
|
||||||
) {
|
) {
|
||||||
console.info(`[${req.method}] ${req.url}`);
|
console.info(`[${req.method}] ${req.url}`);
|
||||||
|
// Put the log into the database
|
||||||
|
const log: Log = new Log(
|
||||||
|
req.url,
|
||||||
|
req.method as any,
|
||||||
|
JSON.stringify(req.body)
|
||||||
|
);
|
||||||
|
await AppDataSource.manager.save(log);
|
||||||
|
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-1
@@ -18,6 +18,9 @@
|
|||||||
"noImplicitAny": true,
|
"noImplicitAny": true,
|
||||||
/* Completeness */
|
/* Completeness */
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"typeRoots": ["./src/types", "./node_modules/@types"]
|
"typeRoots": ["./src/types", "./node_modules/@types"],
|
||||||
|
/* Decorators */
|
||||||
|
"emitDecoratorMetadata": true,
|
||||||
|
"experimentalDecorators": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user