From cd58555409c9ef9fcea45f04ce3f0137bd1f8eda Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Sat, 14 Mar 2020 10:22:10 +0100 Subject: [PATCH] refactor - backend single bundle --- packages/api/src/controllers/connections.js | 2 +- .../src/controllers/databaseConnections.js | 2 +- .../api/src/controllers/serverConnections.js | 3 +- packages/api/src/deps.js | 3 -- packages/api/src/index.js | 41 +++++-------------- packages/api/src/main.js | 37 +++++++++++++++++ packages/api/src/proc/connectProcess.js | 28 +++++++------ .../api/src/proc/databaseConnectionProcess.js | 18 ++++---- packages/api/src/proc/index.js | 12 ++++-- .../api/src/proc/serverConnectionProcess.js | 20 +++++---- 10 files changed, 98 insertions(+), 68 deletions(-) delete mode 100644 packages/api/src/deps.js create mode 100644 packages/api/src/main.js diff --git a/packages/api/src/controllers/connections.js b/packages/api/src/controllers/connections.js index c09d15ae..549522a1 100644 --- a/packages/api/src/controllers/connections.js +++ b/packages/api/src/controllers/connections.js @@ -27,7 +27,7 @@ module.exports = { raw: true, }, test(req, res) { - const subprocess = fork(`${__dirname}/../proc/connectProcess.js`); + const subprocess = fork(process.argv[1], ['connectProcess']); subprocess.on('message', resp => res.json(resp)); subprocess.send(req.body); }, diff --git a/packages/api/src/controllers/databaseConnections.js b/packages/api/src/controllers/databaseConnections.js index 2d5bc867..7e235df6 100644 --- a/packages/api/src/controllers/databaseConnections.js +++ b/packages/api/src/controllers/databaseConnections.js @@ -31,7 +31,7 @@ module.exports = { const existing = this.opened.find(x => x.conid == conid && x.database == database); if (existing) return existing; const connection = await connections.get({ conid }); - const subprocess = fork(`${__dirname}/../proc/databaseConnectionProcess.js`); + const subprocess = fork(process.argv[1], ['databaseConnectionProcess']); const newOpened = { conid, database, diff --git a/packages/api/src/controllers/serverConnections.js b/packages/api/src/controllers/serverConnections.js index f7fc1062..1b28b2dd 100644 --- a/packages/api/src/controllers/serverConnections.js +++ b/packages/api/src/controllers/serverConnections.js @@ -1,4 +1,3 @@ - const connections = require('./connections'); const socket = require('../utility/socket'); const { fork } = require('child_process'); @@ -20,7 +19,7 @@ module.exports = { const existing = this.opened.find(x => x.conid == conid); if (existing) return existing; const connection = await connections.get({ conid }); - const subprocess = fork(`${__dirname}/../proc/serverConnectionProcess.js`); + const subprocess = fork(process.argv[1], ['serverConnectionProcess']); const newOpened = { conid, subprocess, diff --git a/packages/api/src/deps.js b/packages/api/src/deps.js deleted file mode 100644 index e3ec9242..00000000 --- a/packages/api/src/deps.js +++ /dev/null @@ -1,3 +0,0 @@ -require('./proc'); - -module.exports = {}; \ No newline at end of file diff --git a/packages/api/src/index.js b/packages/api/src/index.js index 378d0da9..4bbcc39b 100644 --- a/packages/api/src/index.js +++ b/packages/api/src/index.js @@ -1,9 +1,3 @@ -const express = require('express'); -const bodyParser = require('body-parser'); -const http = require('http'); -const cors = require('cors'); -const io = require('socket.io'); -require('./deps'); // require('socket.io-client'); // "socket.io-client": "^2.3.0", @@ -11,29 +5,14 @@ require('./deps'); // "uuid": "^3.4.0", // "uws": "10.148.1" +const processName = process.argv[2]; +if (processName && processName.endsWith('Process')) { + const proc = require('./proc'); -const useController = require('./utility/useController'); -const connections = require('./controllers/connections'); -const serverConnections = require('./controllers/serverConnections'); -const databaseConnections = require('./controllers/databaseConnections'); -const tables = require('./controllers/tables'); -const socket = require('./utility/socket'); - -const app = express(); - -const server = http.createServer(app); -socket.set(io(server)); - -app.use(cors()); -app.use(bodyParser.json()); - -app.get('/', (req, res) => { - res.send('DbGate API'); -}); - -useController(app, '/connections', connections); -useController(app, '/server-connections', serverConnections); -useController(app, '/database-connections', databaseConnections); -useController(app, '/tables', tables); - -server.listen(3000); + const module = proc[processName]; + module.start(); +} else { + const main = require('./main'); + + main.start(); +} diff --git a/packages/api/src/main.js b/packages/api/src/main.js new file mode 100644 index 00000000..62c78be9 --- /dev/null +++ b/packages/api/src/main.js @@ -0,0 +1,37 @@ +const express = require('express'); +const bodyParser = require('body-parser'); +const http = require('http'); +const cors = require('cors'); +const io = require('socket.io'); + +const useController = require('./utility/useController'); +const connections = require('./controllers/connections'); +const serverConnections = require('./controllers/serverConnections'); +const databaseConnections = require('./controllers/databaseConnections'); +const tables = require('./controllers/tables'); +const socket = require('./utility/socket'); + +function start() { + console.log('process.argv', process.argv); + + const app = express(); + + const server = http.createServer(app); + socket.set(io(server)); + + app.use(cors()); + app.use(bodyParser.json()); + + app.get('/', (req, res) => { + res.send('DbGate API'); + }); + + useController(app, '/connections', connections); + useController(app, '/server-connections', serverConnections); + useController(app, '/database-connections', databaseConnections); + useController(app, '/tables', tables); + + server.listen(3000); +} + +module.exports = { start }; diff --git a/packages/api/src/proc/connectProcess.js b/packages/api/src/proc/connectProcess.js index c843c969..0f533da7 100644 --- a/packages/api/src/proc/connectProcess.js +++ b/packages/api/src/proc/connectProcess.js @@ -1,14 +1,18 @@ const engines = require('@dbgate/engines'); -const driverConnect = require('../utility/driverConnect') +const driverConnect = require('../utility/driverConnect'); -process.on('message', async connection => { - try { - const driver = engines(connection); - const conn = await driverConnect(driver, connection); - const res = await driver.getVersion(conn); - process.send(res); - } catch (e) { - console.log(e); - process.send({ msgtype: 'error', error: e.message }); - } -}); +function start() { + process.on('message', async connection => { + try { + const driver = engines(connection); + const conn = await driverConnect(driver, connection); + const res = await driver.getVersion(conn); + process.send(res); + } catch (e) { + console.log(e); + process.send({ msgtype: 'error', error: e.message }); + } + }); +} + +module.exports = { start }; diff --git a/packages/api/src/proc/databaseConnectionProcess.js b/packages/api/src/proc/databaseConnectionProcess.js index 59bed790..48fe0a2f 100644 --- a/packages/api/src/proc/databaseConnectionProcess.js +++ b/packages/api/src/proc/databaseConnectionProcess.js @@ -56,10 +56,14 @@ async function handleMessage({ msgtype, ...other }) { await handler(other); } -process.on('message', async message => { - try { - await handleMessage(message); - } catch (e) { - process.send({ msgtype: 'error', error: e.message }); - } -}); +function start() { + process.on('message', async message => { + try { + await handleMessage(message); + } catch (e) { + process.send({ msgtype: 'error', error: e.message }); + } + }); +} + +module.exports = { start }; diff --git a/packages/api/src/proc/index.js b/packages/api/src/proc/index.js index f495e4f6..a4f87bce 100644 --- a/packages/api/src/proc/index.js +++ b/packages/api/src/proc/index.js @@ -1,3 +1,9 @@ -require('./connectProcess'); -require('./databaseConnectionProcess'); -require('./serverConnectionProcess'); \ No newline at end of file +const connectProcess = require('./connectProcess'); +const databaseConnectionProcess = require('./databaseConnectionProcess'); +const serverConnectionProcess = require('./serverConnectionProcess'); + +module.exports = { + connectProcess, + databaseConnectionProcess, + serverConnectionProcess, +}; diff --git a/packages/api/src/proc/serverConnectionProcess.js b/packages/api/src/proc/serverConnectionProcess.js index 013129cf..bd4fb8f1 100644 --- a/packages/api/src/proc/serverConnectionProcess.js +++ b/packages/api/src/proc/serverConnectionProcess.js @@ -1,5 +1,5 @@ const engines = require('@dbgate/engines'); -const driverConnect = require('../utility/driverConnect') +const driverConnect = require('../utility/driverConnect'); let systemConnection; let storedConnection; @@ -28,10 +28,14 @@ async function handleMessage({ msgtype, ...other }) { await handler(other); } -process.on('message', async message => { - try { - await handleMessage(message); - } catch (e) { - process.send({ msgtype: 'error', error: e.message }); - } -}); +function start() { + process.on('message', async message => { + try { + await handleMessage(message); + } catch (e) { + process.send({ msgtype: 'error', error: e.message }); + } + }); +} + +module.exports = { start };