1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/lib/routes/client-api/register.js
ivaosthu ccaab0c47b fix: LogProvider as option injected to unleash.
Instead of instructing users to do static calls
in to Unleash, she should instead be allwed to
specify the log provider as an option to Unleash.

This commit introduces the "getLogger" option,
a function responsible for creating a logger.
2020-02-20 08:34:24 +01:00

46 lines
1.4 KiB
JavaScript

'use strict';
const joi = require('joi');
const Controller = require('../controller');
const { clientRegisterSchema: schema } = require('./register-schema');
class RegisterController extends Controller {
constructor({ clientInstanceStore, clientApplicationsStore }, getLogger) {
super();
this.logger = getLogger('/api/client/register');
this.clientInstanceStore = clientInstanceStore;
this.clientApplicationsStore = clientApplicationsStore;
this.post('/', this.handleRegister);
}
async handleRegister(req, res) {
const data = req.body;
const { value: clientRegistration, error } = joi.validate(data, schema);
if (error) {
this.logger.warn('Invalid client data posted', error);
return res.status(400).json(error);
}
clientRegistration.clientIp = req.ip;
try {
await this.clientApplicationsStore.upsert(clientRegistration);
await this.clientInstanceStore.insert(clientRegistration);
this.logger.info(
`New client registered with appName=${
clientRegistration.appName
} and instanceId=${clientRegistration.instanceId}`
);
return res.status(202).end();
} catch (err) {
this.logger.error('failed to register client', err);
return res.status(500).end();
}
}
}
module.exports = RegisterController;