mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-17 22:36:01 +00:00
server connection process
This commit is contained in:
@@ -26,8 +26,8 @@ module.exports = {
|
||||
},
|
||||
test(req, res) {
|
||||
const subprocess = fork(`${__dirname}/../proc/connectProcess.js`);
|
||||
subprocess.send(req.body);
|
||||
subprocess.on('message', resp => res.json(resp));
|
||||
subprocess.send(req.body);
|
||||
},
|
||||
|
||||
save_meta: 'post',
|
||||
@@ -52,6 +52,6 @@ module.exports = {
|
||||
get_meta: 'get',
|
||||
async get({ id }) {
|
||||
const res = await this.datastore.find({ _id: id });
|
||||
return res;
|
||||
return res[0];
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,17 +1,38 @@
|
||||
const connections = require('./connections');
|
||||
const socket = require('../utility/socket');
|
||||
const { fork } = require('child_process');
|
||||
|
||||
module.exports = {
|
||||
opened: [],
|
||||
|
||||
handle_databases(id, { databases }) {
|
||||
const existing = this.opened.find(x => x.connection.id == id);
|
||||
if (!existing) return;
|
||||
existing.databases = databases;
|
||||
socket.emit(`database-list-changed-${id}`);
|
||||
},
|
||||
|
||||
async ensureOpened(id) {
|
||||
const existing = this.opened.find(x => x.connection.id == id);
|
||||
if (existing) return existing;
|
||||
|
||||
const connection = await connections.get(id);
|
||||
const subprocess = fork(`${__dirname}/../proc/serverConnectionProcess.js`);
|
||||
const newOpened = {
|
||||
id,
|
||||
subprocess,
|
||||
databases: [],
|
||||
};
|
||||
this.opened.push(newOpened);
|
||||
subprocess.on('message', ({ msgtype, ...message }) => {
|
||||
this[`handle_${msgtype}`](id, message);
|
||||
});
|
||||
subprocess.send({ msgtype: 'connect', ...connection });
|
||||
return newOpened;
|
||||
},
|
||||
|
||||
listDatabases_meta: 'get',
|
||||
async listDatabases({ id }) {
|
||||
const opened = this.ensureOpened(id);
|
||||
const opened = await this.ensureOpened(id);
|
||||
return opened.databases;
|
||||
},
|
||||
};
|
||||
|
||||
@@ -6,6 +6,6 @@ process.on('message', async connection => {
|
||||
process.send(res);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
process.send({ error: e.message });
|
||||
process.send({ msgtype: 'error', error: e.message });
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,20 +1,32 @@
|
||||
function handleConnect() {}
|
||||
let systemConnection;
|
||||
let storedConnection;
|
||||
|
||||
async function handleRefreshDatabases() {
|
||||
const driver = require(`../engines/${storedConnection.engine}/index`);
|
||||
const databases = await driver.listDatabases(systemConnection);
|
||||
process.send({ msgtype: 'databases', databases });
|
||||
}
|
||||
|
||||
async function handleConnect(connection) {
|
||||
storedConnection = connection;
|
||||
const driver = require(`../engines/${storedConnection.engine}/index`);
|
||||
systemConnection = await driver.connect(storedConnection);
|
||||
setInterval(handleRefreshDatabases, 30 * 1000);
|
||||
}
|
||||
|
||||
const messageHandlers = {
|
||||
connect: handleConnect,
|
||||
};
|
||||
|
||||
function handleMessage({ type, ...other }) {
|
||||
const handler = messageHandlers[type];
|
||||
handler(other);
|
||||
async function handleMessage({ msgtype, ...other }) {
|
||||
const handler = messageHandlers[msgtype];
|
||||
await handler(other);
|
||||
}
|
||||
|
||||
process.on('message', async connection => {
|
||||
process.on('message', async message => {
|
||||
try {
|
||||
const connectFunc = require(`../engines/${connection.engine}/connect`);
|
||||
const res = await connectFunc(connection);
|
||||
process.send(res);
|
||||
await handleMessage(message);
|
||||
} catch (e) {
|
||||
process.send({ error: e.message });
|
||||
process.send({ msgtype: 'error', error: e.message });
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user