forked from microfleet/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.js
More file actions
52 lines (45 loc) · 1.28 KB
/
Copy pathhttp.js
File metadata and controls
52 lines (45 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// @flow
import type { ValidateSync } from './validator';
/**
* Project deps
* @private
*/
const is = require('is');
const assert = require('assert');
const { PluginsTypes } = require('../constants');
/**
* Plugin Name
* @type {String}
*/
exports.name = 'http';
/**
* Plugin Type
* @type {String}
*/
exports.type = PluginsTypes.transport;
/**
* Configuration validator helper.
* @param {Object} config - Configuration to validate.
* @param {Function} validator - Instance of ms-validation to be used.
*/
function validateConfig(config: Object, validator: ValidateSync) {
if (is.fn(validator)) {
// validate core http config
assert.ifError(validator('http', config).error);
// validate handler config
if (config.server.handlerConfig) {
const validatorName = `http.${config.server.handler}`;
assert.ifError(validator(validatorName, config.server.handlerConfig).error);
}
}
}
/**
* Attaches HTTP handler.
* @param {Object} config - HTTP handler configuration to attach.
*/
exports.attach = function createHttpServer(config: Object): PluginInterface {
validateConfig(config, this.validateSync);
// eslint-disable-next-line import/no-dynamic-require
const handler = require(`./http/handlers/${config.server.handler}`);
return handler(config, this);
};