From 0f4c72a32ff56061bfd90537ace2d4ca0dcbfc2a Mon Sep 17 00:00:00 2001 From: kato Date: Sat, 21 Aug 2021 02:26:59 +0300 Subject: [PATCH] + System Controller + Mail MOdel + Config Updates --- config.example.json | 3 +- src/app.ts | 3 ++ src/helper/config.ts | 19 ++++--- src/models/MailModel.ts | 21 ++++++-- src/models/SystemController.ts | 92 ++++++++++++++++++++++++++++++++++ 5 files changed, 125 insertions(+), 13 deletions(-) create mode 100644 src/models/SystemController.ts diff --git a/config.example.json b/config.example.json index bda8b62..9ace8a1 100644 --- a/config.example.json +++ b/config.example.json @@ -2,7 +2,8 @@ "options": { "parallel_nodes": 2, "parallel_node_tasks": 5, - "separate_into_tables": false + "separate_into_tables": false, + "mysql_dump_settings": "--no-comments" }, "smtp": { diff --git a/src/app.ts b/src/app.ts index eea0d27..020f4ab 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,4 +1,7 @@ +import { SystemControl } from "./models/SystemController"; + +(global as any).logger = new SystemControl(); /** * The main function to run the program with diff --git a/src/helper/config.ts b/src/helper/config.ts index 8cafdd1..c188a99 100644 --- a/src/helper/config.ts +++ b/src/helper/config.ts @@ -1,7 +1,7 @@ /** * A Collection of settings */ -interface ConfigOptions { +export interface ConfigOptions { /** * The number of nodes that will be ran in parallel, * if the number of nodes is smaller than the parallels, @@ -20,12 +20,17 @@ interface ConfigOptions { * to only export tables. */ separate_into_tables: boolean, + + /** + * A string of CLI options for the mysql dump command + */ + mysql_dump_settings: string, } /** * Basic configuration interface for the mailer */ -interface SMTPConfig { +export interface SMTPConfig { /** * The email address to globally use for sending * out email from. @@ -46,14 +51,14 @@ interface SMTPConfig { /** * The port to send to, common ports are 465 ( ssl ), 587 for TLS */ - port : string, + port : number, /** * Secure Connection */ ssl : boolean, } -interface NodeConnectionMail { +export interface NodeConnectionMail { /** * Enable or disable email sending */ @@ -68,7 +73,7 @@ interface NodeConnectionMail { email_cc: Array, } -interface DatabaseFilter { +export interface DatabaseFilter { /** * A filter where anything inside of it is going to be ignored */ @@ -82,7 +87,7 @@ interface DatabaseFilter { /** * A Node Connection for the database backup */ -interface NodeConnection { +export interface NodeConnection { /** * The name for the connection to be identified by */ @@ -117,7 +122,7 @@ interface NodeConnection { databases?: DatabaseFilter } -interface ConfigObject { +export interface ConfigObject { /** * A list of global settings for the behaviour * of the exporter diff --git a/src/models/MailModel.ts b/src/models/MailModel.ts index 54222cf..a7d84fd 100644 --- a/src/models/MailModel.ts +++ b/src/models/MailModel.ts @@ -1,4 +1,4 @@ -import {getConfig} from "../helper/config"; +import {getConfig, NodeConnectionMail} from "../helper/config"; import nodemailer from "nodemailer"; const config = getConfig(); @@ -12,6 +12,8 @@ function getTransporter() : nodemailer.Transporter { // Generate the transporter const transporter = nodemailer.createTransport({ host: config?.smtp.host, + port: config?.smtp.port, + secure: config?.smtp.ssl, auth: { user: config?.smtp.username, pass: config?.smtp.password, @@ -22,15 +24,24 @@ function getTransporter() : nodemailer.Transporter { return transporter; } -export async function sendMail(mailContent: string, toAddress: string, ccAddresses?: Array) { +/** + * Send out an email with the config details. + */ +export async function sendMail(mailContent: string, toAddress: string, nodeConfig: NodeConnectionMail, ccAddresses?: Array) { // Get the transporter const transporter = getTransporter(); + // Generate the mail options + const mailOptions : nodemailer.SendMailOptions = { + from: config?.smtp.email_from, + to: nodeConfig.email_to, + cc: nodeConfig.email_cc, + html: mailContent, + } + await new Promise((_r, _e) => { - transporter.sendMail({ - - }, (err, info) => { + transporter.sendMail(mailOptions, (err, info) => { }) }) diff --git a/src/models/SystemController.ts b/src/models/SystemController.ts new file mode 100644 index 0000000..856efd6 --- /dev/null +++ b/src/models/SystemController.ts @@ -0,0 +1,92 @@ +function formatDate(d : Date) : string { + + let day = (String(d.getDate() + 1)).padStart(2, "0") + let month = (String(d.getMonth())).padStart(2, "0"); + let year = (String(d.getFullYear())); + + let hour = (String(d.getHours())).padStart(2, "0"); + let minute = (String(d.getMinutes())).padStart(2, "0"); + + // Return the formatted datye + return `${day}/${month}/${year} ${hour}:${minute}`; +} + + +class logMessage { + + ts: Date; + origin: string; + message: string; + + constructor( message: string, origin: string = "Unknown", ts?: Date ) { + + // Initialize the date as the current date + if ( !ts ) ts = new Date(); + + // Initialize the log message's data + this.ts = ts; + this.origin = origin; + this.message = message; + } + + /** + * Get the log as a string + * @returns A string representation fo the log message + */ + toString() : string { + // Get the formatted date + let dateFormatted = formatDate(this.ts); + + // return the formatted message + return `[${dateFormatted}][${this.origin}]: ${this.message}`; + } + +} + +class Logger { + + // An array of logs to represent in an email + logData : Array; + + constructor() { + // Initialize an array of logs + this.logData = []; + } + + sendEmailLog() : void{ + + } + +} + +export class SystemControl { + // The main logger of the system + log: Logger; + + constructor() { + // Create an instance of the logger + this.log = new Logger(); + } + + + + /** + * Send out a panic message and end the process + * @param message The panic message + */ + async panic(message: string) : Promise { + // Send out the error message + console.error(message); + + // End the process + this.endProcess(); + } + + async endProcess(exitCode : number = 100): Promise { + + + // Exit the process + process.exit(exitCode); + } + +} \ No newline at end of file