Skip to content
Extraits de code Groupes Projets
Vérifiée Valider a12dafbc rédigé par Kubat's avatar Kubat
Parcourir les fichiers

Make things construct from the config

parent a03d8378
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!9Config
...@@ -2,18 +2,36 @@ const ini = require('ini'), ...@@ -2,18 +2,36 @@ const ini = require('ini'),
os = require('os'), os = require('os'),
fs = require('fs'); 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 = { var config = {
database: '/home/kara/kara.db', database: null,
reload: () => { host: null,
var fd = fs.openSync(__configFile, 'w'); port: null,
reload() {
var fd = fs.openSync(__configFile, 'a');
fs.close(fd, err => {}); fs.close(fd, err => {});
var __config = ini.parse(fs.readFileSync(__configFile, 'utf-8')); 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));
}, },
}; };
......
const logger = require.main.require('./common/logger.js'); const logger = require.main.require('./common/logger.js');
const config = require.main.require('./common/config.js');
var sqlite3 = require.main.require('sqlite3').verbose(); var sqlite3 = require.main.require('sqlite3').verbose();
/* The kara table is defined as follows: /* The kara table is defined as follows:
...@@ -43,10 +44,10 @@ class KaraDatabase { ...@@ -43,10 +44,10 @@ class KaraDatabase {
* Private members * Private members
* - m_karaPath: String * - m_karaPath: String
* - m_db: sqlite3 database */ * - m_db: sqlite3 database */
constructor(karaPath) { constructor() {
this.m_karaPath = karaPath; this.m_karaPath = config.database;
this.m_db = new sqlite3.Database(this.m_karaPath); 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 */ /* Call this as a destructor */
...@@ -169,4 +170,5 @@ class KaraDatabase { ...@@ -169,4 +170,5 @@ class KaraDatabase {
} }
} }
module.exports = KaraDatabase; var db = new KaraDatabase();
module.exports = db;
const logger = require.main.require('./common/logger.js'); const logger = require.main.require('./common/logger.js'),
const net = require('net'); net = require('net'),
const { finished } = require('stream'); { finished } = require('stream'),
const { cli } = require('winston/lib/winston/config'); { cli } = require('winston/lib/winston/config');
var config = require.main.require('./common/config.js');
class LktClient { class LktClient {
/*************************************** /***************************************
...@@ -18,14 +19,13 @@ class LktClient { ...@@ -18,14 +19,13 @@ class LktClient {
* Node: You should not use that directly, prefere the use of static * Node: You should not use that directly, prefere the use of static
* methods */ * methods */
constructor(port = 6600) { constructor() {
this.m_online = false; this.m_online = false;
this.m_port = port;
this.m_socket = new net.Socket(); this.m_socket = new net.Socket();
this.m_closed = true; this.m_closed = true;
const sockopt = { const sockopt = {
port: this.m_port, port: config.port,
host: 'localhost', host: config.host,
readable: true, readable: true,
writable: true, writable: true,
}; };
...@@ -36,7 +36,7 @@ class LktClient { ...@@ -36,7 +36,7 @@ class LktClient {
this.m_socket.on('timeout', () => { this.m_socket.on('timeout', () => {
logger.error( logger.error(
'lkt', 'lkt',
`Got timeout while connecting to localhost:${this.m_port}` `Got timeout while connecting to localhost:${config.port}`
); );
this.m_socket.end(); this.m_socket.end();
}); });
...@@ -44,7 +44,7 @@ class LktClient { ...@@ -44,7 +44,7 @@ class LktClient {
this.m_socket.on('ready', () => { this.m_socket.on('ready', () => {
logger.debug( logger.debug(
'lkt', 'lkt',
`Ready to use socker with localhost:${this.m_port}` `Ready to use socker with localhost:${config.port}`
); );
this.m_online = true; this.m_online = true;
}); });
...@@ -52,7 +52,7 @@ class LktClient { ...@@ -52,7 +52,7 @@ class LktClient {
this.m_socket.on('end', () => { this.m_socket.on('end', () => {
logger.info( logger.info(
'lkt', 'lkt',
`Disconnected from server localhost:${this.m_port}` `Disconnected from server localhost:${config.port}`
); );
this.m_online = false; this.m_online = false;
}); });
...@@ -66,12 +66,12 @@ class LktClient { ...@@ -66,12 +66,12 @@ class LktClient {
}); });
this.m_socket.on('close', () => { 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_online = false;
}); });
this.m_socket.connect(sockopt, () => { 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; this.m_online = true;
}); });
......
...@@ -3,10 +3,10 @@ const logger = require.main.require('./common/logger.js'), ...@@ -3,10 +3,10 @@ const logger = require.main.require('./common/logger.js'),
{ app, BrowserWindow, globalShortcut, ipcMain } = require('electron'), { app, BrowserWindow, globalShortcut, ipcMain } = require('electron'),
{ fork, spawn } = require('child_process'), { fork, spawn } = require('child_process'),
fs = require('fs'), fs = require('fs'),
db = require.main.require('./common/db.js'),
lkt = require.main.require('./common/lkt.js'); lkt = require.main.require('./common/lkt.js');
var config = require('./common/config.js'); var config = require('./common/config.js');
var db = require.main.require('./common/db.js');
var tail = require('tail').Tail; var tail = require('tail').Tail;
var client; /* Sub process for the express server */ var client; /* Sub process for the express server */
var lektor; /* Sub process, the lektord player */ var lektor; /* Sub process, the lektord player */
...@@ -24,17 +24,6 @@ tail.on('line', function (data) { ...@@ -24,17 +24,6 @@ tail.on('line', function (data) {
console.log(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 * * Creates the main window and process for the admin interface *
***************************************************************/ ***************************************************************/
...@@ -108,7 +97,7 @@ app.on('ready', () => { ...@@ -108,7 +97,7 @@ app.on('ready', () => {
} }
logger.info('main', 'Reloading DB'); logger.info('main', 'Reloading DB');
let contents = win.webContents; let contents = win.webContents;
cfg.database.all().then(karas => { db.all().then(karas => {
contents.send('reload-db-responce', karas); contents.send('reload-db-responce', karas);
}); });
}); });
...@@ -207,16 +196,16 @@ ipcMain.on('reload-db-request', (event, arg) => { ...@@ -207,16 +196,16 @@ ipcMain.on('reload-db-request', (event, arg) => {
}; };
if (arg) { if (arg) {
logger.debug('main', `Reload DB with search '${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 { } else {
cfg.database.all(0, 15).then(callback); db.all(0, 15).then(callback);
} }
}); });
/* Send the queue to the webpage when asked to */ /* Send the queue to the webpage when asked to */
ipcMain.on('reload-queue-request', (event, arg) => { ipcMain.on('reload-queue-request', (event, arg) => {
logger.info('main', 'Reloading next karas in queue'); 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); event.reply('reload-queue-responce', karas);
}); });
}); });
...@@ -224,7 +213,7 @@ ipcMain.on('reload-queue-request', (event, arg) => { ...@@ -224,7 +213,7 @@ ipcMain.on('reload-queue-request', (event, arg) => {
ipcMain.on('verify-queue-reloaded-request', (event, arg) => { ipcMain.on('verify-queue-reloaded-request', (event, arg) => {
if (lkt.isQueueUpdated()) { if (lkt.isQueueUpdated()) {
lkt.setQueueUpdated(false); lkt.setQueueUpdated(false);
cfg.database.queue(0, 100).then(karas => { db.queue(0, 100).then(karas => {
event.reply('reload-queue-responce', karas); event.reply('reload-queue-responce', karas);
}); });
} }
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter