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:
parent
683354be6c
commit
529ac38e97
@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
const { Router } = require('express');
|
const { Router } = require('express');
|
||||||
const FeatureController = require('./feature.js');
|
const FeatureController = require('./feature.js');
|
||||||
const ClientMetricsController = require('./metrics.js');
|
const MetricsController = require('./metrics.js');
|
||||||
const register = require('./register.js');
|
const RegisterController = require('./register.js');
|
||||||
const clientApi = require('./client-api.json');
|
const clientApiSpec = require('./client-api.json');
|
||||||
|
|
||||||
class ClientApi {
|
class ClientApi {
|
||||||
constructor(config) {
|
constructor(config) {
|
||||||
@ -13,12 +13,12 @@ class ClientApi {
|
|||||||
|
|
||||||
router.get('/', this.index);
|
router.get('/', this.index);
|
||||||
router.use('/features', new FeatureController(config).router());
|
router.use('/features', new FeatureController(config).router());
|
||||||
router.use('/metrics', new ClientMetricsController(config).router());
|
router.use('/metrics', new MetricsController(config).router());
|
||||||
router.use('/register', register.router(config));
|
router.use('/register', new RegisterController(config).router());
|
||||||
}
|
}
|
||||||
|
|
||||||
index(req, res) {
|
index(req, res) {
|
||||||
res.json(clientApi);
|
res.json(clientApiSpec);
|
||||||
}
|
}
|
||||||
|
|
||||||
router() {
|
router() {
|
||||||
@ -26,6 +26,6 @@ class ClientApi {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ClientApi.api = clientApi;
|
ClientApi.api = clientApiSpec;
|
||||||
|
|
||||||
module.exports = ClientApi;
|
module.exports = ClientApi;
|
||||||
|
@ -4,37 +4,47 @@ const { Router } = require('express');
|
|||||||
const joi = require('joi');
|
const joi = require('joi');
|
||||||
const logger = require('../../logger')('/client-api/register.js');
|
const logger = require('../../logger')('/client-api/register.js');
|
||||||
|
|
||||||
const { clientRegisterSchema } = require('./register-schema');
|
const { clientRegisterSchema: schema } = require('./register-schema');
|
||||||
|
|
||||||
exports.router = config => {
|
class RegisterController {
|
||||||
const { clientInstanceStore, clientApplicationsStore } = config.stores;
|
constructor({ stores: { clientInstanceStore, clientApplicationsStore } }) {
|
||||||
const router = Router();
|
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 data = req.body;
|
||||||
|
const { value: clientRegistration, error } = joi.validate(data, schema);
|
||||||
|
|
||||||
joi.validate(data, clientRegisterSchema, (err, clientRegistration) => {
|
if (error) {
|
||||||
if (err) {
|
logger.warn('Invalid client data posted', error);
|
||||||
logger.warn('Invalid client data posted', err);
|
return res.status(400).json(error);
|
||||||
return res.status(400).json(err);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
clientRegistration.clientIp = req.ip;
|
clientRegistration.clientIp = req.ip;
|
||||||
|
|
||||||
clientApplicationsStore
|
try {
|
||||||
.upsert(clientRegistration)
|
await this.clientApplicationsStore.upsert(clientRegistration);
|
||||||
.then(() => clientInstanceStore.insert(clientRegistration))
|
await this.clientInstanceStore.insert(clientRegistration);
|
||||||
.then(() =>
|
logger.info(
|
||||||
logger.info(`New client registered with
|
`New client registered with appName=${
|
||||||
appName=${clientRegistration.appName} and instanceId=${
|
clientRegistration.appName
|
||||||
clientRegistration.instanceId
|
} and instanceId=${clientRegistration.instanceId}`
|
||||||
}`)
|
);
|
||||||
)
|
return res.status(202).end();
|
||||||
.catch(err => logger.error('failed to register client', err));
|
} 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;
|
||||||
};
|
|
||||||
|
@ -73,3 +73,35 @@ test('should require strategies field', t => {
|
|||||||
})
|
})
|
||||||
.expect(400);
|
.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);
|
||||||
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user