Commit 511b9508 authored by Michael Ochmann's avatar Michael Ochmann
Browse files

added settings management

parent 36d16709
{}
\ No newline at end of file
const Server = require("./Server");
const Std = require("./Std");
const Server = require("./Server");
const Std = require("./Std");
const Settings = require("./SettingsHandler");
class Application {
constructor() {
Std.Log(`[Application] starting beacon monitoring system`);
this.server = new Server();
this.server.listen();
this.server.listen(Settings.get("server_port", 9080));
}
}
......
const fs = require("fs");
const path = require("path");
const Std = require("./Std");
/**
* Handles dealing with app-settings
*
* @SINGLETON
* @since 0.0.1
* @author Michael Ochmann <ochmannm@hochschule-trier.de>
*/
class SettingsHandler {
constructor() {
if (!fs.existsSync(SettingsHandler.FILENAME))
fs.writeFileSync(SettingsHandler.FILENAME, "{}", {flag : "w+"});
try {
this.settings = require(SettingsHandler.FILENAME);
} catch (exception) {
Std.Log(`[SettingsHandler] ERROR parsing logfile:\n\t${exception}`, Std.LogLevel.ERROR);
}
}
get(key, defaultValue = null) {
if (key in this.settings)
return this.settings[key];
return defaultValue;
}
set(key, value) {
this.settings[key] = value;
this.save();
}
save() {
fs.writeFileSync(SettingsHandler.FILENAME, JSON.stringify(this.settings), {flag : "w"});
}
static Instance() {
if (SettingsHandler._Instance === null)
SettingsHandler._Instance = new SettingsHandler();
return SettingsHandler._Instance;
}
}
SettingsHandler.FILENAME = path.join(__dirname, "..", "assets", "settings.json");
SettingsHandler._Instance = null;
module.exports = SettingsHandler.Instance();
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment