1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/lib/routes/controller.js

66 lines
1.4 KiB
JavaScript
Raw Normal View History

'use strict';
const { Router } = require('express');
const checkPermission = require('../middleware/permission-checker');
/**
* Base class for Controllers to standardize binding to express Router.
*/
class Controller {
constructor(config) {
const router = Router();
this.app = router;
this.config = config;
}
get(path, handler, permission) {
this.app.get(
path,
checkPermission(this.config, permission),
handler.bind(this),
);
}
post(path, handler, permission) {
this.app.post(
path,
checkPermission(this.config, permission),
handler.bind(this),
);
}
put(path, handler, permission) {
this.app.put(
path,
checkPermission(this.config, permission),
handler.bind(this),
);
}
delete(path, handler, permission) {
this.app.delete(
path,
checkPermission(this.config, permission),
handler.bind(this),
);
}
fileupload(path, filehandler, handler, permission) {
this.app.post(
path,
checkPermission(this.config, permission),
filehandler,
handler.bind(this),
);
}
use(path, router) {
this.app.use(path, router);
}
get router() {
return this.app;
}
}
module.exports = Controller;