+ Configuration file is fully loaded now

This commit is contained in:
Daniel Legt 2021-08-21 16:11:09 +03:00
parent 36b1e37370
commit 54f12a164c
4 changed files with 81 additions and 7 deletions

View File

@ -29,6 +29,7 @@
"hostname": "localhost", "hostname": "localhost",
"username": "root", "username": "root",
"password": "", "password": "",
"port" : 3306,
"databases": { "databases": {
"blacklist": null, "blacklist": null,
"whitelist": null "whitelist": null

View File

@ -3,5 +3,19 @@
"@types/nodemailer": "^6.4.4", "@types/nodemailer": "^6.4.4",
"mysql": "^2.18.1", "mysql": "^2.18.1",
"nodemailer": "^6.6.3" "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"
} }

View File

@ -1,13 +1,25 @@
// Import the configuration file
import { getConfig } from "./helper/config";
// Import SystemControl // Import SystemControl
import { SystemControl } from "./models/SystemController"; import { SystemControl } from "./models/SystemController";
// Global system control singleton // Global system control singleton
(global as any).logger = new SystemControl(); (global as any).logger = new SystemControl();
(global as any).config = null;
/** /**
* The main function to run the program with * The main function to run the program with
*/ */
async function main() { async function main() {
// Get the configuration file
(global as any).config = getConfig();
} console.log(
(global as any).config
);
}
// Run the main program
main();

View File

@ -1,3 +1,5 @@
import * as fs from "fs";
/** /**
* A Collection of settings * A Collection of settings
*/ */
@ -116,6 +118,10 @@ export interface NodeConnection {
* The password to connect to the database with * The password to connect to the database with
*/ */
password : string, password : string,
/**
* The port for the database connection
*/
port : number,
/** /**
* The database filter, this can contain a blacklist or a whitelist. * The database filter, this can contain a blacklist or a whitelist.
*/ */
@ -141,13 +147,54 @@ export interface ConfigObject {
nodes: Array<NodeConnection> nodes: Array<NodeConnection>
} }
/**
* 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 * Read the config file from the files of the project
* @returns The configuration object of the project * @returns The configuration object of the project
*/ */
export function getConfig() : ConfigObject | null { export function getConfig() : ConfigObject {
// Check for the location of the config file
const fileName : string = findConfigFile();
return null;
// 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.");
} }