mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-30 23:13:57 +00:00
refactor, controller - support for query params
This commit is contained in:
@@ -1,36 +0,0 @@
|
|||||||
const os = require('os');
|
|
||||||
const path = require('path');
|
|
||||||
const fs = require('fs-extra');
|
|
||||||
const express = require('express');
|
|
||||||
const router = express.Router();
|
|
||||||
const { fork } = require('child_process');
|
|
||||||
const _ = require('lodash');
|
|
||||||
|
|
||||||
function datadir() {
|
|
||||||
return path.join(os.homedir(), 'dbgate-data');
|
|
||||||
}
|
|
||||||
|
|
||||||
router.post('/test', async (req, res) => {
|
|
||||||
const subprocess = fork(`${__dirname}/connectProcess.js`);
|
|
||||||
subprocess.send(req.body);
|
|
||||||
subprocess.on('message', resp => res.json(resp));
|
|
||||||
});
|
|
||||||
|
|
||||||
router.post('/save', async (req, res) => {
|
|
||||||
await fs.mkdir(datadir());
|
|
||||||
const fileName = `${new Date().getTime()}.con`;
|
|
||||||
await fs.writeFile(path.join(datadir(), fileName), JSON.stringify(req.body));
|
|
||||||
res.json({ fileName });
|
|
||||||
});
|
|
||||||
|
|
||||||
router.get('/list', async (req, res) => {
|
|
||||||
const files = await fs.readdir(datadir());
|
|
||||||
|
|
||||||
res
|
|
||||||
.json(
|
|
||||||
await Promise.all(files.filter(x => x.endsWith('.con')).map(x => fs.readFile(path.join(datadir(), x), 'utf-8')))
|
|
||||||
)
|
|
||||||
.map(x => _.omit(JSON.parse(x), 'password'));
|
|
||||||
});
|
|
||||||
|
|
||||||
module.exports = router;
|
|
||||||
@@ -14,10 +14,12 @@ module.exports = {
|
|||||||
const dir = await datadir();
|
const dir = await datadir();
|
||||||
this.datastore = nedb.create(path.join(dir, 'connections.jsonl'));
|
this.datastore = nedb.create(path.join(dir, 'connections.jsonl'));
|
||||||
},
|
},
|
||||||
|
|
||||||
list_meta: 'get',
|
list_meta: 'get',
|
||||||
async list() {
|
async list() {
|
||||||
return this.datastore.find();
|
return this.datastore.find();
|
||||||
},
|
},
|
||||||
|
|
||||||
test_meta: {
|
test_meta: {
|
||||||
method: 'post',
|
method: 'post',
|
||||||
raw: true,
|
raw: true,
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const bodyParser = require('body-parser');
|
const bodyParser = require('body-parser');
|
||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
const connection = require('./connection');
|
|
||||||
const useController = require('./utility/useController');
|
const useController = require('./utility/useController');
|
||||||
const connections = require('./controllers/connections');
|
const connections = require('./controllers/connections');
|
||||||
const app = express();
|
const app = express();
|
||||||
@@ -14,6 +13,5 @@ app.get('/', (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
useController(app, '/connections', connections);
|
useController(app, '/connections', connections);
|
||||||
app.use('/connection', connection);
|
|
||||||
|
|
||||||
app.listen(3000);
|
app.listen(3000);
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ module.exports = function useController(app, route, controller) {
|
|||||||
controller._init_called = true;
|
controller._init_called = true;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
let params = [{ ...req.body }];
|
let params = [{ ...req.body, ...req.query }];
|
||||||
if (rawParams) params = [req, res];
|
if (rawParams) params = [req, res];
|
||||||
const data = await controller[key](...params);
|
const data = await controller[key](...params);
|
||||||
res.json(data);
|
res.json(data);
|
||||||
|
|||||||
Reference in New Issue
Block a user