-
Notifications
You must be signed in to change notification settings - Fork 8.1k
/
ui_render_mixin.js
183 lines (165 loc) · 5.55 KB
/
ui_render_mixin.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { defaults } from 'lodash';
import { props, reduce as reduceAsync } from 'bluebird';
import Boom from 'boom';
import { resolve } from 'path';
import { i18n } from '@kbn/i18n';
import { AppBootstrap } from './bootstrap';
export function uiRenderMixin(kbnServer, server, config) {
function replaceInjectedVars(request, injectedVars) {
const { injectedVarsReplacers = [] } = kbnServer.uiExports;
return reduceAsync(
injectedVarsReplacers,
async (acc, replacer) => await replacer(acc, request, kbnServer.server),
injectedVars
);
}
let defaultInjectedVars = {};
kbnServer.afterPluginsInit(() => {
const { defaultInjectedVarProviders = [] } = kbnServer.uiExports;
defaultInjectedVars = defaultInjectedVarProviders
.reduce((allDefaults, { fn, pluginSpec }) => (
defaults(
allDefaults,
fn(kbnServer.server, pluginSpec.readConfigValue(kbnServer.config, []))
)
), {});
});
// render all views from ./views
server.setupViews(resolve(__dirname, 'views'));
server.route({
path: '/bundles/app/{id}/bootstrap.js',
method: 'GET',
config: { auth: false },
async handler(request, reply) {
try {
const { id } = request.params;
const app = server.getUiAppById(id) || server.getHiddenUiAppById(id);
if (!app) {
throw Boom.notFound(`Unknown app: ${id}`);
}
const basePath = config.get('server.basePath');
const bootstrap = new AppBootstrap({
templateData: {
appId: app.getId(),
bundlePath: `${basePath}/bundles`,
styleSheetPath: app.getStyleSheetUrlPath() ? `${basePath}/${app.getStyleSheetUrlPath()}` : null,
},
translations: await server.getUiTranslations()
});
const body = await bootstrap.getJsFile();
const etag = await bootstrap.getJsFileHash();
reply(body)
.header('cache-control', 'must-revalidate')
.header('content-type', 'application/javascript')
.etag(etag);
} catch (err) {
reply(err);
}
}
});
server.route({
path: '/app/{id}',
method: 'GET',
async handler(req, reply) {
const id = req.params.id;
const app = server.getUiAppById(id);
if (!app) return reply(Boom.notFound('Unknown app ' + id));
try {
if (kbnServer.status.isGreen()) {
await reply.renderApp(app);
} else {
await reply.renderStatusPage();
}
} catch (err) {
reply(Boom.wrap(err));
}
}
});
async function getLegacyKibanaPayload({ app, translations, request, includeUserProvidedConfig, injectedVarsOverrides }) {
const uiSettings = request.getUiSettingsService();
return {
app,
translations,
bundleId: `app:${app.getId()}`,
nav: server.getUiNavLinks(),
version: kbnServer.version,
branch: config.get('pkg.branch'),
buildNum: config.get('pkg.buildNum'),
buildSha: config.get('pkg.buildSha'),
basePath: config.get('server.basePath'),
serverName: config.get('server.name'),
devMode: config.get('env.dev'),
uiSettings: await props({
defaults: uiSettings.getDefaults(),
user: includeUserProvidedConfig && uiSettings.getUserProvided()
}),
vars: await replaceInjectedVars(
request,
defaults(
injectedVarsOverrides,
await server.getInjectedUiAppVars(app.getId()),
defaultInjectedVars,
),
)
};
}
async function renderApp({ app, reply, includeUserProvidedConfig = true, injectedVarsOverrides = {} }) {
try {
const request = reply.request;
const translations = await server.getUiTranslations();
const basePath = config.get('server.basePath');
return reply.view('ui_app', {
uiPublicUrl: `${basePath}/ui`,
bootstrapScriptUrl: `${basePath}/bundles/app/${app.getId()}/bootstrap.js`,
i18n: (id, options) => i18n.translate(id, options),
injectedMetadata: {
version: kbnServer.version,
buildNumber: config.get('pkg.buildNum'),
basePath,
legacyMetadata: await getLegacyKibanaPayload({
app,
translations,
request,
includeUserProvidedConfig,
injectedVarsOverrides
}),
},
});
} catch (err) {
reply(err);
}
}
server.decorate('reply', 'renderApp', function (app, injectedVarsOverrides) {
return renderApp({
app,
reply: this,
includeUserProvidedConfig: true,
injectedVarsOverrides,
});
});
server.decorate('reply', 'renderAppWithDefaultConfig', function (app) {
return renderApp({
app,
reply: this,
includeUserProvidedConfig: false,
});
});
}