mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-25 17:46:00 +00:00
server connection process
This commit is contained in:
@@ -26,8 +26,8 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
test(req, res) {
|
test(req, res) {
|
||||||
const subprocess = fork(`${__dirname}/../proc/connectProcess.js`);
|
const subprocess = fork(`${__dirname}/../proc/connectProcess.js`);
|
||||||
subprocess.send(req.body);
|
|
||||||
subprocess.on('message', resp => res.json(resp));
|
subprocess.on('message', resp => res.json(resp));
|
||||||
|
subprocess.send(req.body);
|
||||||
},
|
},
|
||||||
|
|
||||||
save_meta: 'post',
|
save_meta: 'post',
|
||||||
@@ -52,6 +52,6 @@ module.exports = {
|
|||||||
get_meta: 'get',
|
get_meta: 'get',
|
||||||
async get({ id }) {
|
async get({ id }) {
|
||||||
const res = await this.datastore.find({ _id: id });
|
const res = await this.datastore.find({ _id: id });
|
||||||
return res;
|
return res[0];
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,17 +1,38 @@
|
|||||||
const connections = require('./connections');
|
const connections = require('./connections');
|
||||||
const socket = require('../utility/socket');
|
const socket = require('../utility/socket');
|
||||||
|
const { fork } = require('child_process');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
opened: [],
|
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) {
|
async ensureOpened(id) {
|
||||||
const existing = this.opened.find(x => x.connection.id == id);
|
const existing = this.opened.find(x => x.connection.id == id);
|
||||||
if (existing) return existing;
|
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',
|
listDatabases_meta: 'get',
|
||||||
async listDatabases({ id }) {
|
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);
|
process.send(res);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(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 = {
|
const messageHandlers = {
|
||||||
connect: handleConnect,
|
connect: handleConnect,
|
||||||
};
|
};
|
||||||
|
|
||||||
function handleMessage({ type, ...other }) {
|
async function handleMessage({ msgtype, ...other }) {
|
||||||
const handler = messageHandlers[type];
|
const handler = messageHandlers[msgtype];
|
||||||
handler(other);
|
await handler(other);
|
||||||
}
|
}
|
||||||
|
|
||||||
process.on('message', async connection => {
|
process.on('message', async message => {
|
||||||
try {
|
try {
|
||||||
const connectFunc = require(`../engines/${connection.engine}/connect`);
|
await handleMessage(message);
|
||||||
const res = await connectFunc(connection);
|
|
||||||
process.send(res);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
process.send({ error: e.message });
|
process.send({ msgtype: 'error', error: e.message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user