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:
parent
01e3216e05
commit
42a4df8aae
@ -1,20 +1,19 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const { Router } = require('express');
|
const Controller = require('../controller');
|
||||||
|
|
||||||
const logger = require('../../logger')('/admin-api/archive.js');
|
const logger = require('../../logger')('/admin-api/archive.js');
|
||||||
const { FEATURE_REVIVED } = require('../../event-type');
|
const { FEATURE_REVIVED } = require('../../event-type');
|
||||||
const extractUser = require('../../extract-user');
|
const extractUser = require('../../extract-user');
|
||||||
|
|
||||||
class ArchiveController {
|
class ArchiveController extends Controller {
|
||||||
constructor({ featureToggleStore, eventStore }) {
|
constructor({ featureToggleStore, eventStore }) {
|
||||||
const router = Router();
|
super();
|
||||||
this._router = router;
|
|
||||||
this.featureToggleStore = featureToggleStore;
|
this.featureToggleStore = featureToggleStore;
|
||||||
this.eventStore = eventStore;
|
this.eventStore = eventStore;
|
||||||
|
|
||||||
router.get('/features', this.getArchivedFeatures.bind(this));
|
this.get('/features', this.getArchivedFeatures);
|
||||||
router.post('/revive/:name', this.reviveFeatureToggle.bind(this));
|
this.post('/revive/:name', this.reviveFeatureToggle);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getArchivedFeatures(req, res) {
|
async getArchivedFeatures(req, res) {
|
||||||
@ -37,10 +36,6 @@ class ArchiveController {
|
|||||||
return res.status(500).end();
|
return res.status(500).end();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
router() {
|
|
||||||
return this._router;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = ArchiveController;
|
module.exports = ArchiveController;
|
||||||
|
@ -1,35 +1,35 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const { Router } = require('express');
|
const Controller = require('../controller');
|
||||||
|
|
||||||
const eventDiffer = require('../../event-differ');
|
const eventDiffer = require('../../event-differ');
|
||||||
const version = 1;
|
const version = 1;
|
||||||
|
|
||||||
module.exports.router = function(config) {
|
class EventController extends Controller {
|
||||||
const { eventStore } = config.stores;
|
constructor({ eventStore }) {
|
||||||
const router = Router();
|
super();
|
||||||
|
this.eventStore = eventStore;
|
||||||
|
this.get('/', this.getEvents);
|
||||||
|
this.get('/:name', this.getEventsForToggle);
|
||||||
|
}
|
||||||
|
|
||||||
router.get('/', (req, res) => {
|
async getEvents(req, res) {
|
||||||
eventStore.getEvents().then(events => {
|
const events = await this.eventStore.getEvents();
|
||||||
eventDiffer.addDiffs(events);
|
eventDiffer.addDiffs(events);
|
||||||
res.json({ version, events });
|
res.json({ version, events });
|
||||||
});
|
}
|
||||||
});
|
|
||||||
|
|
||||||
router.get('/:name', (req, res) => {
|
async getEventsForToggle(req, res) {
|
||||||
const toggleName = req.params.name;
|
const toggleName = req.params.name;
|
||||||
eventStore.getEventsFilterByName(toggleName).then(events => {
|
const events = await this.eventStore.getEventsFilterByName(toggleName);
|
||||||
|
|
||||||
if (events) {
|
if (events) {
|
||||||
eventDiffer.addDiffs(events);
|
eventDiffer.addDiffs(events);
|
||||||
res.json({
|
res.json({ toggleName, events });
|
||||||
toggleName,
|
|
||||||
events,
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
res.status(404).json({ error: 'Could not find events' });
|
res.status(404).json({ error: 'Could not find events' });
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
return router;
|
module.exports = EventController;
|
||||||
};
|
|
||||||
|
@ -1,36 +1,32 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const { Router } = require('express');
|
const Controller = require('../controller');
|
||||||
|
|
||||||
const features = require('./feature.js');
|
const features = require('./feature.js');
|
||||||
const ArchiveController = require('./archive.js');
|
const ArchiveController = require('./archive.js');
|
||||||
const events = require('./event.js');
|
const EventController = require('./event.js');
|
||||||
const strategies = require('./strategy');
|
const strategies = require('./strategy');
|
||||||
const metrics = require('./metrics');
|
const metrics = require('./metrics');
|
||||||
const user = require('./user');
|
const user = require('./user');
|
||||||
const apiDef = require('./api-def.json');
|
const apiDef = require('./api-def.json');
|
||||||
|
|
||||||
class AdminApi {
|
class AdminApi extends Controller {
|
||||||
constructor(config) {
|
constructor(config) {
|
||||||
const router = Router();
|
super();
|
||||||
this._router = router;
|
|
||||||
|
|
||||||
router.get('/', (req, res) => res.json(apiDef));
|
const stores = config.stores;
|
||||||
router.use('/features', features.router(config));
|
|
||||||
router.use('/archive', new ArchiveController(config.stores).router());
|
this.app.get('/', this.index);
|
||||||
router.use('/strategies', strategies.router(config));
|
this.app.use('/features', features.router(config));
|
||||||
router.use('/events', events.router(config));
|
this.app.use('/archive', new ArchiveController(stores).router());
|
||||||
router.use('/metrics', metrics.router(config));
|
this.app.use('/strategies', strategies.router(config));
|
||||||
router.use('/user', user.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) {
|
index(req, res) {
|
||||||
res.json(apiDef);
|
res.json(apiDef);
|
||||||
}
|
}
|
||||||
|
|
||||||
router() {
|
|
||||||
return this._router;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = AdminApi;
|
module.exports = AdminApi;
|
||||||
|
26
lib/routes/controller.js
Normal file
26
lib/routes/controller.js
Normal 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;
|
Loading…
Reference in New Issue
Block a user