-
Notifications
You must be signed in to change notification settings - Fork 8k
/
kbn_server.js
158 lines (130 loc) · 4.23 KB
/
kbn_server.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { constant, once, compact, flatten } from 'lodash';
import { reconfigureLogging } from '@kbn/legacy-logging';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { fromRoot, pkg } from '../../core/server/utils';
import { Config } from './config';
import httpMixin from './http';
import { coreMixin } from './core';
import { loggingMixin } from './logging';
import warningsMixin from './warnings';
import configCompleteMixin from './config/complete';
import { optimizeMixin } from '../../optimize';
import { uiMixin } from '../ui';
/**
* @typedef {import('./kbn_server').KibanaConfig} KibanaConfig
* @typedef {import('./kbn_server').KibanaCore} KibanaCore
* @typedef {import('./kbn_server').LegacyPlugins} LegacyPlugins
*/
const rootDir = fromRoot('.');
export default class KbnServer {
/**
* @param {Record<string, any>} settings
* @param {KibanaConfig} config
* @param {KibanaCore} core
*/
constructor(settings, config, core) {
this.name = pkg.name;
this.version = pkg.version;
this.build = pkg.build || false;
this.rootDir = rootDir;
this.settings = settings || {};
this.config = config;
const { setupDeps, startDeps, logger, __internals, env } = core;
this.server = __internals.hapiServer;
this.newPlatform = {
env: {
mode: env.mode,
packageInfo: env.packageInfo,
},
__internals,
coreContext: {
logger,
},
setup: setupDeps,
start: startDeps,
stop: null,
};
this.ready = constant(
this.mixin(
// Sets global HTTP behaviors
httpMixin,
coreMixin,
loggingMixin,
warningsMixin,
// tell the config we are done loading plugins
configCompleteMixin,
uiMixin,
// setup routes that serve the @kbn/optimizer output
optimizeMixin
)
);
this.listen = once(this.listen);
}
/**
* Extend the KbnServer outside of the constraints of a plugin. This allows access
* to APIs that are not exposed (intentionally) to the plugins and should only
* be used when the code will be kept up to date with Kibana.
*
* @param {...function} - functions that should be called to mixin functionality.
* They are called with the arguments (kibana, server, config)
* and can return a promise to delay execution of the next mixin
* @return {Promise} - promise that is resolved when the final mixin completes.
*/
async mixin(...fns) {
for (const fn of compact(flatten(fns))) {
await fn.call(this, this, this.server, this.config);
}
}
/**
* Tell the server to listen for incoming requests, or get
* a promise that will be resolved once the server is listening.
*
* @return undefined
*/
async listen() {
await this.ready();
const { server, config } = this;
if (process.env.isDevCliChild) {
// help parent process know when we are ready
process.send(['SERVER_LISTENING']);
}
server.log(
['listening', 'info'],
`Server running at ${server.info.uri}${
config.get('server.rewriteBasePath') ? config.get('server.basePath') : ''
}`
);
return server;
}
async close() {
if (!this.server) {
return;
}
await this.server.stop();
}
async inject(opts) {
if (!this.server) {
await this.ready();
}
return await this.server.inject(opts);
}
applyLoggingConfiguration(settings) {
const config = Config.withDefaultSchema(settings);
const loggingConfig = config.get('logging');
const opsConfig = config.get('ops');
const subset = {
ops: opsConfig,
logging: loggingConfig,
};
const plain = JSON.stringify(subset, null, 2);
this.server.log(['info', 'config'], 'New logging configuration:\n' + plain);
reconfigureLogging(this.server, loggingConfig, opsConfig.interval);
}
}