refactor - backend single bundle

This commit is contained in:
Jan Prochazka 2020-03-14 10:22:10 +01:00
parent 6a13d9fa19
commit cd58555409
10 changed files with 98 additions and 68 deletions

View File

@ -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);
},

View File

@ -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,

View File

@ -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,

View File

@ -1,3 +0,0 @@
require('./proc');
module.exports = {};

View File

@ -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();
}

37
packages/api/src/main.js Normal file
View File

@ -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 };

View File

@ -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 };

View File

@ -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 };

View File

@ -1,3 +1,9 @@
require('./connectProcess');
require('./databaseConnectionProcess');
require('./serverConnectionProcess');
const connectProcess = require('./connectProcess');
const databaseConnectionProcess = require('./databaseConnectionProcess');
const serverConnectionProcess = require('./serverConnectionProcess');
module.exports = {
connectProcess,
databaseConnectionProcess,
serverConnectionProcess,
};

View File

@ -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 };