1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-31 00:16:47 +01:00

chore(modernize): Spin out base class for Controllers

This commit is contained in:
ivaosthu 2018-11-30 10:11:36 +01:00 committed by Ivar Conradi Østhus
parent 01e3216e05
commit 42a4df8aae
4 changed files with 69 additions and 52 deletions

View File

@ -1,20 +1,19 @@
'use strict';
const { Router } = require('express');
const Controller = require('../controller');
const logger = require('../../logger')('/admin-api/archive.js');
const { FEATURE_REVIVED } = require('../../event-type');
const extractUser = require('../../extract-user');
class ArchiveController {
class ArchiveController extends Controller {
constructor({ featureToggleStore, eventStore }) {
const router = Router();
this._router = router;
super();
this.featureToggleStore = featureToggleStore;
this.eventStore = eventStore;
router.get('/features', this.getArchivedFeatures.bind(this));
router.post('/revive/:name', this.reviveFeatureToggle.bind(this));
this.get('/features', this.getArchivedFeatures);
this.post('/revive/:name', this.reviveFeatureToggle);
}
async getArchivedFeatures(req, res) {
@ -37,10 +36,6 @@ class ArchiveController {
return res.status(500).end();
}
}
router() {
return this._router;
}
}
module.exports = ArchiveController;

View File

@ -1,35 +1,35 @@
'use strict';
const { Router } = require('express');
const Controller = require('../controller');
const eventDiffer = require('../../event-differ');
const version = 1;
module.exports.router = function(config) {
const { eventStore } = config.stores;
const router = Router();
class EventController extends Controller {
constructor({ eventStore }) {
super();
this.eventStore = eventStore;
this.get('/', this.getEvents);
this.get('/:name', this.getEventsForToggle);
}
router.get('/', (req, res) => {
eventStore.getEvents().then(events => {
async getEvents(req, res) {
const events = await this.eventStore.getEvents();
eventDiffer.addDiffs(events);
res.json({ version, events });
});
});
}
router.get('/:name', (req, res) => {
async getEventsForToggle(req, res) {
const toggleName = req.params.name;
eventStore.getEventsFilterByName(toggleName).then(events => {
const events = await this.eventStore.getEventsFilterByName(toggleName);
if (events) {
eventDiffer.addDiffs(events);
res.json({
toggleName,
events,
});
res.json({ toggleName, events });
} else {
res.status(404).json({ error: 'Could not find events' });
}
});
});
}
}
return router;
};
module.exports = EventController;

View File

@ -1,36 +1,32 @@
'use strict';
const { Router } = require('express');
const Controller = require('../controller');
const features = require('./feature.js');
const ArchiveController = require('./archive.js');
const events = require('./event.js');
const EventController = require('./event.js');
const strategies = require('./strategy');
const metrics = require('./metrics');
const user = require('./user');
const apiDef = require('./api-def.json');
class AdminApi {
class AdminApi extends Controller {
constructor(config) {
const router = Router();
this._router = router;
super();
router.get('/', (req, res) => res.json(apiDef));
router.use('/features', features.router(config));
router.use('/archive', new ArchiveController(config.stores).router());
router.use('/strategies', strategies.router(config));
router.use('/events', events.router(config));
router.use('/metrics', metrics.router(config));
router.use('/user', user.router(config));
const stores = config.stores;
this.app.get('/', this.index);
this.app.use('/features', features.router(config));
this.app.use('/archive', new ArchiveController(stores).router());
this.app.use('/strategies', strategies.router(config));
this.app.use('/events', new EventController(stores).router());
this.app.use('/metrics', metrics.router(config));
this.app.use('/user', user.router(config));
}
index(req, res) {
res.json(apiDef);
}
router() {
return this._router;
}
}
module.exports = AdminApi;

26
lib/routes/controller.js Normal file
View File

@ -0,0 +1,26 @@
'use strict';
const { Router } = require('express');
/**
* Base class for Controllers to standardize binding to express Router.
*/
class Controller {
constructor() {
const router = Router();
this.app = router;
}
get(path, handler) {
this.app.get(path, handler.bind(this));
}
post(path, handler) {
this.app.post(path, handler.bind(this));
}
router() {
return this.app;
}
}
module.exports = Controller;