diff --git a/common/config.js b/common/config.js index c5e252397d3448792b2679d5ef26a194caef955a..f4f04f55d91740a2e9bfb5dc425533a944f676cb 100644 --- a/common/config.js +++ b/common/config.js @@ -2,18 +2,36 @@ const ini = require('ini'), os = require('os'), fs = require('fs'); -const __configFile = `${os.homedir()}/.amadeus.ini`; +const __configDir = `${os.homedir()}/.config/lektor`; +const __configFile = `${__configDir}/amadeus.ini`; + +fs.mkdirSync(__configDir, { recursive: true }); +__dir = fs.opendirSync(__configDir); +__dir.close(err => { + if (err) throw err; +}); var config = { - database: '/home/kara/kara.db', - reload: () => { - var fd = fs.openSync(__configFile, 'w'); + database: null, + host: null, + port: null, + reload() { + var fd = fs.openSync(__configFile, 'a'); fs.close(fd, err => {}); var __config = ini.parse(fs.readFileSync(__configFile, 'utf-8')); - __config.database_path = __config.database_path || '/home/kara/kara.db'; + __config.database ??= {}; + __config.lektord ??= {}; + + __config.database.path ??= '/home/kara/kara.db'; + __config.lektord.host ??= 'localhost'; + __config.lektord.port ??= '6600'; + + this.database = __config.database.path; + this.host = __config.lektord.host; + this.port = __config.lektord.port; - fs.writeFileSync(__configFile, ini.stringify(__config)) + fs.writeFileSync(__configFile, ini.stringify(__config)); }, }; diff --git a/common/db.js b/common/db.js index 6d8b1a3636579b76e369804a2aaf99557a22489a..ddfc7c508cd47cbc322864ee8b63fd93f9290bcd 100644 --- a/common/db.js +++ b/common/db.js @@ -1,4 +1,5 @@ const logger = require.main.require('./common/logger.js'); +const config = require.main.require('./common/config.js'); var sqlite3 = require.main.require('sqlite3').verbose(); /* The kara table is defined as follows: @@ -43,10 +44,10 @@ class KaraDatabase { * Private members * - m_karaPath: String * - m_db: sqlite3 database */ - constructor(karaPath) { - this.m_karaPath = karaPath; + constructor() { + this.m_karaPath = config.database; this.m_db = new sqlite3.Database(this.m_karaPath); - logger.info('db', 'Create database from file ' + karaPath); + logger.info('db', 'Create database from file ' + config.database); } /* Call this as a destructor */ @@ -169,4 +170,5 @@ class KaraDatabase { } } -module.exports = KaraDatabase; +var db = new KaraDatabase(); +module.exports = db; diff --git a/common/lkt.js b/common/lkt.js index 229c1d696cef56f1f70521c7c1ad4e15237805ea..238f0cdb572b6e95aca0d7fbc6ca57df5817d3cc 100644 --- a/common/lkt.js +++ b/common/lkt.js @@ -1,7 +1,8 @@ -const logger = require.main.require('./common/logger.js'); -const net = require('net'); -const { finished } = require('stream'); -const { cli } = require('winston/lib/winston/config'); +const logger = require.main.require('./common/logger.js'), + net = require('net'), + { finished } = require('stream'), + { cli } = require('winston/lib/winston/config'); +var config = require.main.require('./common/config.js'); class LktClient { /*************************************** @@ -18,14 +19,13 @@ class LktClient { * Node: You should not use that directly, prefere the use of static * methods */ - constructor(port = 6600) { + constructor() { this.m_online = false; - this.m_port = port; this.m_socket = new net.Socket(); this.m_closed = true; const sockopt = { - port: this.m_port, - host: 'localhost', + port: config.port, + host: config.host, readable: true, writable: true, }; @@ -36,7 +36,7 @@ class LktClient { this.m_socket.on('timeout', () => { logger.error( 'lkt', - `Got timeout while connecting to localhost:${this.m_port}` + `Got timeout while connecting to localhost:${config.port}` ); this.m_socket.end(); }); @@ -44,7 +44,7 @@ class LktClient { this.m_socket.on('ready', () => { logger.debug( 'lkt', - `Ready to use socker with localhost:${this.m_port}` + `Ready to use socker with localhost:${config.port}` ); this.m_online = true; }); @@ -52,7 +52,7 @@ class LktClient { this.m_socket.on('end', () => { logger.info( 'lkt', - `Disconnected from server localhost:${this.m_port}` + `Disconnected from server localhost:${config.port}` ); this.m_online = false; }); @@ -66,12 +66,12 @@ class LktClient { }); this.m_socket.on('close', () => { - logger.info('lkt', `Socket localhost:${this.m_port} closed`); + logger.info('lkt', `Socket localhost:${config.port} closed`); this.m_online = false; }); this.m_socket.connect(sockopt, () => { - logger.info('lkt', `Socket connected to localhost:${this.m_port}`); + logger.info('lkt', `Socket connected to localhost:${config.port}`); this.m_online = true; }); diff --git a/main.js b/main.js index 90901746f045d8081e727b6a108c0978d5d03fdc..ba0ddb5f63b2493dd98ee1b1410a1edb980dce7d 100644 --- a/main.js +++ b/main.js @@ -3,10 +3,10 @@ const logger = require.main.require('./common/logger.js'), { app, BrowserWindow, globalShortcut, ipcMain } = require('electron'), { fork, spawn } = require('child_process'), fs = require('fs'), - db = require.main.require('./common/db.js'), lkt = require.main.require('./common/lkt.js'); var config = require('./common/config.js'); +var db = require.main.require('./common/db.js'); var tail = require('tail').Tail; var client; /* Sub process for the express server */ var lektor; /* Sub process, the lektord player */ @@ -24,17 +24,6 @@ tail.on('line', function (data) { console.log(data); }); -/********************* - * Create the config * - *********************/ - -function createConfig() { - return { - database: new db(config.database), - }; -} -const cfg = createConfig(); - /*************************************************************** * Creates the main window and process for the admin interface * ***************************************************************/ @@ -108,7 +97,7 @@ app.on('ready', () => { } logger.info('main', 'Reloading DB'); let contents = win.webContents; - cfg.database.all().then(karas => { + db.all().then(karas => { contents.send('reload-db-responce', karas); }); }); @@ -207,16 +196,16 @@ ipcMain.on('reload-db-request', (event, arg) => { }; if (arg) { logger.debug('main', `Reload DB with search '${arg}'`); - cfg.database.search(arg, 0, 15).then(callback); + db.search(arg, 0, 15).then(callback); } else { - cfg.database.all(0, 15).then(callback); + db.all(0, 15).then(callback); } }); /* Send the queue to the webpage when asked to */ ipcMain.on('reload-queue-request', (event, arg) => { logger.info('main', 'Reloading next karas in queue'); - cfg.database.queue(0, 100).then(karas => { + db.queue(0, 100).then(karas => { event.reply('reload-queue-responce', karas); }); }); @@ -224,7 +213,7 @@ ipcMain.on('reload-queue-request', (event, arg) => { ipcMain.on('verify-queue-reloaded-request', (event, arg) => { if (lkt.isQueueUpdated()) { lkt.setQueueUpdated(false); - cfg.database.queue(0, 100).then(karas => { + db.queue(0, 100).then(karas => { event.reply('reload-queue-responce', karas); }); }