From 54f12a164c5a2bfe1b50bcbb127433bd1b95d005 Mon Sep 17 00:00:00 2001 From: Kato Twofold Date: Sat, 21 Aug 2021 16:11:09 +0300 Subject: [PATCH] + Configuration file is fully loaded now --- config.example.json | 1 + package.json | 16 ++++++++++++- src/app.ts | 16 +++++++++++-- src/helper/config.ts | 55 ++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 81 insertions(+), 7 deletions(-) diff --git a/config.example.json b/config.example.json index 9ace8a1..2989d2b 100644 --- a/config.example.json +++ b/config.example.json @@ -29,6 +29,7 @@ "hostname": "localhost", "username": "root", "password": "", + "port" : 3306, "databases": { "blacklist": null, "whitelist": null diff --git a/package.json b/package.json index 2efd93a..652efc8 100644 --- a/package.json +++ b/package.json @@ -3,5 +3,19 @@ "@types/nodemailer": "^6.4.4", "mysql": "^2.18.1", "nodemailer": "^6.6.3" - } + }, + "name": "datahoard", + "description": "Make sure your database backups run and keep track of performance", + "version": "0.1.0", + "main": "dist/app.js", + "devDependencies": {}, + "scripts": { + "start": "tsc && node dist/app.js" + }, + "repository": { + "type": "git", + "url": "https://tea.chunkbyte.com/kato/DataHoard.git" + }, + "author": "Kato Twofold", + "license": "SEE LICENSE IN LICENSE" } diff --git a/src/app.ts b/src/app.ts index 399ffb8..a592abf 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,13 +1,25 @@ +// Import the configuration file +import { getConfig } from "./helper/config"; // Import SystemControl import { SystemControl } from "./models/SystemController"; // Global system control singleton (global as any).logger = new SystemControl(); +(global as any).config = null; /** * The main function to run the program with */ async function main() { - + // Get the configuration file + (global as any).config = getConfig(); -} \ No newline at end of file + console.log( + (global as any).config + ); + +} + + +// Run the main program +main(); \ No newline at end of file diff --git a/src/helper/config.ts b/src/helper/config.ts index a6ac455..545cb43 100644 --- a/src/helper/config.ts +++ b/src/helper/config.ts @@ -1,3 +1,5 @@ +import * as fs from "fs"; + /** * A Collection of settings */ @@ -116,6 +118,10 @@ export interface NodeConnection { * The password to connect to the database with */ password : string, + /** + * The port for the database connection + */ + port : number, /** * The database filter, this can contain a blacklist or a whitelist. */ @@ -141,13 +147,54 @@ export interface ConfigObject { nodes: Array } +/** + * Get the name of the config file + */ +export function getConfigFileName() : string { + return "config.json"; +} + +/** + * The config file can really be in 2 places, in the project or above it, so we're going to take a peak in both cases. + */ +function findConfigFile() : string { + const fileName : string = getConfigFileName(); + + // Look in the same folder + if ( fs.existsSync(fileName) ) { + return fileName; + } + + // Check outside + if ( fs.existsSync(fileName) ) { + return `../${fileName}`; + } + + // Throw an error because the file does not exist. + throw new Error("Failed to find the config file, please create a `config.json` file and complete it similar to the provided example file `config.example.json`"); +} + /** * Read the config file from the files of the project * @returns The configuration object of the project */ -export function getConfig() : ConfigObject | null { +export function getConfig() : ConfigObject { - - - return null; + // Check for the location of the config file + const fileName : string = findConfigFile(); + + // Initialize the config data object + let configData : ConfigObject; + + // Load in the configuration data from the json file + const rawFileData : string = fs.readFileSync(fileName, `utf-8`); + // Parse the data + configData = JSON.parse(rawFileData); + + if ( !!configData ) { + return configData; + } + + // Throw an error because it has been a failure + throw new Error("Failed to load the configuration file."); } \ No newline at end of file