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

chore(modernize): Modernize ClientRegisterController

This commit is contained in:
ivaosthu 2018-11-29 20:46:24 +01:00 committed by Ivar Conradi Østhus
parent 683354be6c
commit 529ac38e97
3 changed files with 75 additions and 33 deletions

View File

@ -2,9 +2,9 @@
const { Router } = require('express');
const FeatureController = require('./feature.js');
const ClientMetricsController = require('./metrics.js');
const register = require('./register.js');
const clientApi = require('./client-api.json');
const MetricsController = require('./metrics.js');
const RegisterController = require('./register.js');
const clientApiSpec = require('./client-api.json');
class ClientApi {
constructor(config) {
@ -13,12 +13,12 @@ class ClientApi {
router.get('/', this.index);
router.use('/features', new FeatureController(config).router());
router.use('/metrics', new ClientMetricsController(config).router());
router.use('/register', register.router(config));
router.use('/metrics', new MetricsController(config).router());
router.use('/register', new RegisterController(config).router());
}
index(req, res) {
res.json(clientApi);
res.json(clientApiSpec);
}
router() {
@ -26,6 +26,6 @@ class ClientApi {
}
}
ClientApi.api = clientApi;
ClientApi.api = clientApiSpec;
module.exports = ClientApi;

View File

@ -4,37 +4,47 @@ const { Router } = require('express');
const joi = require('joi');
const logger = require('../../logger')('/client-api/register.js');
const { clientRegisterSchema } = require('./register-schema');
const { clientRegisterSchema: schema } = require('./register-schema');
exports.router = config => {
const { clientInstanceStore, clientApplicationsStore } = config.stores;
class RegisterController {
constructor({ stores: { clientInstanceStore, clientApplicationsStore } }) {
const router = Router();
this._router = router;
this.clientInstanceStore = clientInstanceStore;
this.clientApplicationsStore = clientApplicationsStore;
router.post('/', (req, res) => {
router.post('/', (req, res) => this.handleRegister(req, res));
}
async handleRegister(req, res) {
const data = req.body;
const { value: clientRegistration, error } = joi.validate(data, schema);
joi.validate(data, clientRegisterSchema, (err, clientRegistration) => {
if (err) {
logger.warn('Invalid client data posted', err);
return res.status(400).json(err);
if (error) {
logger.warn('Invalid client data posted', error);
return res.status(400).json(error);
}
clientRegistration.clientIp = req.ip;
clientApplicationsStore
.upsert(clientRegistration)
.then(() => clientInstanceStore.insert(clientRegistration))
.then(() =>
logger.info(`New client registered with
appName=${clientRegistration.appName} and instanceId=${
clientRegistration.instanceId
}`)
)
.catch(err => logger.error('failed to register client', err));
try {
await this.clientApplicationsStore.upsert(clientRegistration);
await this.clientInstanceStore.insert(clientRegistration);
logger.info(
`New client registered with appName=${
clientRegistration.appName
} and instanceId=${clientRegistration.instanceId}`
);
return res.status(202).end();
} catch (err) {
logger.error('failed to register client', err);
return res.status(500).end();
}
}
res.status(202).end();
});
});
router() {
return this._router;
}
}
return router;
};
module.exports = RegisterController;

View File

@ -73,3 +73,35 @@ test('should require strategies field', t => {
})
.expect(400);
});
test('should fail if store fails', t => {
t.plan(0);
// --- start custom config
const stores = store.createStores();
stores.clientApplicationsStore = {
upsert: () => {
throw new Error('opps');
},
};
const app = getApp({
baseUriPath: '',
stores,
eventBus,
});
// --- end custom config
const request = supertest(app);
return request
.post('/api/client/register')
.send({
appName: 'demo',
instanceId: 'test',
strategies: ['default'],
started: Date.now(),
interval: 10,
})
.expect(500);
});