Контроллеры маршрутов
This commit is contained in:
8
server/controllers/BaseController.js
Normal file
8
server/controllers/BaseController.js
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
class BaseController {
|
||||||
|
constructor(connPool, config) {
|
||||||
|
this.connPool = connPool;
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = BaseController;
|
||||||
11
server/controllers/MiscController.js
Normal file
11
server/controllers/MiscController.js
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
const log = require('../core/getLogger').getLog();
|
||||||
|
const BaseController = require('./BaseController');
|
||||||
|
const Lazy = require('lazy.js');
|
||||||
|
|
||||||
|
class MiscController extends BaseController {
|
||||||
|
async configValue(req) {
|
||||||
|
return Lazy(this.config).pick([req.params.name]).toObject();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = MiscController;
|
||||||
3
server/controllers/index.js
Normal file
3
server/controllers/index.js
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module.exports = {
|
||||||
|
MiscController: require('./MiscController'),
|
||||||
|
}
|
||||||
@@ -1,6 +1,40 @@
|
|||||||
const log = require('./core/getLogger').getLog();
|
const log = require('./core/getLogger').getLog();
|
||||||
|
const c = require('./controllers');
|
||||||
|
|
||||||
function initRoutes(app, connPool, config) {
|
function initRoutes(app, connPool, config) {
|
||||||
|
const misc = new c.MiscController(connPool, config);
|
||||||
|
|
||||||
|
const routes = [
|
||||||
|
['POST', '/api/config/:name', misc, 'configValue', {}],
|
||||||
|
|
||||||
|
['GET', '/api/config/:name', misc, 'configValue', {}],
|
||||||
|
];
|
||||||
|
|
||||||
|
for (route of routes) {
|
||||||
|
const [httpMethod, path, controller, handler, options] = route;
|
||||||
|
|
||||||
|
const callback = async function(req, res) {
|
||||||
|
try {
|
||||||
|
const result = await controller[handler](req, res, options);
|
||||||
|
|
||||||
|
if (result !== false)
|
||||||
|
res.send(result);
|
||||||
|
} catch (e) {
|
||||||
|
res.status(500).send({error: e.message});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
switch (httpMethod) {
|
||||||
|
case 'GET' :
|
||||||
|
app.get(path, callback);
|
||||||
|
break;
|
||||||
|
case 'POST':
|
||||||
|
app.post(path, callback);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new Error(`initRoutes error: unknown httpMethod: ${httpMethod}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|||||||
Reference in New Issue
Block a user