-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
DevServer.ts
618 lines (506 loc) Β· 21 KB
/
DevServer.ts
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/* eslint-disable global-require,no-restricted-syntax */
import dotenv from '@cubejs-backend/dotenv';
import { CubePreAggregationConverter, CubeSchemaConverter, ScaffoldingTemplate, SchemaFormat } from '@cubejs-backend/schema-compiler';
import spawn from 'cross-spawn';
import path from 'path';
import fs from 'fs-extra';
import { getRequestIdFromRequest } from '@cubejs-backend/api-gateway';
import { LivePreviewWatcher } from '@cubejs-backend/cloud';
import { AppContainer, DependencyTree, PackageFetcher, DevPackageFetcher } from '@cubejs-backend/templates';
import jwt from 'jsonwebtoken';
import isDocker from 'is-docker';
import type { Application as ExpressApplication, Request, Response } from 'express';
import type { ChildProcess } from 'child_process';
import { executeCommand, getEnv, keyByDataSource, packageExists } from '@cubejs-backend/shared';
import crypto from 'crypto';
import type { BaseDriver } from '@cubejs-backend/query-orchestrator';
import { CubejsServerCore } from './server';
import { ExternalDbTypeFn, ServerCoreInitializedOptions, DatabaseType } from './types';
import DriverDependencies from './DriverDependencies';
const repo = {
owner: 'cube-js',
name: 'cubejs-playground-templates'
};
type DevServerOptions = {
externalDbTypeFn: ExternalDbTypeFn;
isReadyForQueryProcessing: () => boolean;
dockerVersion?: string;
};
export class DevServer {
protected applyTemplatePackagesPromise: Promise<any> | null = null;
protected dashboardAppProcess: ChildProcess & { dashboardUrlPromise?: Promise<any> } | null = null;
protected livePreviewWatcher = new LivePreviewWatcher();
public constructor(
protected readonly cubejsServer: CubejsServerCore,
protected readonly options: DevServerOptions
) {
}
public initDevEnv(app: ExpressApplication, options: ServerCoreInitializedOptions) {
const port = process.env.PORT || 4000; // TODO
const apiUrl = process.env.CUBEJS_API_URL || `http://localhost:${port}`;
// todo: empty/default `apiSecret` in dev mode to allow the DB connection wizard
const cubejsToken = jwt.sign({}, options.apiSecret || 'secret', { expiresIn: '1d' });
if (process.env.NODE_ENV !== 'production') {
console.log('π Authentication checks are disabled in developer mode. Please use NODE_ENV=production to enable it.');
} else {
console.log(`π Your temporary cube.js token: ${cubejsToken}`);
}
console.log(`π¦
Dev environment available at ${apiUrl}`);
if (
(
this.options.externalDbTypeFn({
authInfo: null,
securityContext: null,
requestId: '',
}) || ''
).toLowerCase() !== 'cubestore'
) {
console.log('β οΈ Your pre-aggregations will be on an external database. It is recommended to use Cube Store for optimal performance');
}
this.cubejsServer.event('Dev Server Start');
const serveStatic = require('serve-static');
const catchErrors = (handler) => async (req, res, next) => {
try {
await handler(req, res, next);
} catch (e) {
console.error(((e as Error).stack || e).toString());
this.cubejsServer.event('Dev Server Error', { error: ((e as Error).stack || e).toString() });
res.status(500).json({ error: ((e as Error).stack || e).toString() });
}
};
app.get('/playground/context', catchErrors((req, res) => {
this.cubejsServer.event('Dev Server Env Open');
res.json({
cubejsToken,
basePath: options.basePath,
anonymousId: this.cubejsServer.anonymousId,
coreServerVersion: this.cubejsServer.coreServerVersion,
dockerVersion: this.options.dockerVersion || null,
projectFingerprint: this.cubejsServer.projectFingerprint,
dbType: options.dbType || null,
shouldStartConnectionWizardFlow: !this.options.isReadyForQueryProcessing(),
livePreview: options.livePreview,
isDocker: isDocker(),
telemetry: options.telemetry,
identifier: this.getIdentifier(options.apiSecret),
previewFeatures: getEnv('previewFeatures'),
});
}));
app.get('/playground/db-schema', catchErrors(async (req, res) => {
this.cubejsServer.event('Dev Server DB Schema Load');
const driver = await this.cubejsServer.getDriver({
dataSource: req.body.dataSource || 'default',
authInfo: null,
securityContext: null,
requestId: getRequestIdFromRequest(req),
});
const tablesSchema = await driver.tablesSchema();
this.cubejsServer.event('Dev Server DB Schema Load Success');
if (Object.keys(tablesSchema || {}).length === 0) {
this.cubejsServer.event('Dev Server DB Schema Load Empty');
}
res.json({ tablesSchema });
}));
app.get('/playground/files', catchErrors(async (req, res) => {
this.cubejsServer.event('Dev Server Files Load');
const files = await this.cubejsServer.repository.dataSchemaFiles();
res.json({
files: files.map(f => ({
...f,
absPath: path.resolve(path.join(this.cubejsServer.repository.localPath(), f.fileName))
}))
});
}));
app.post('/playground/generate-schema', catchErrors(async (req, res) => {
this.cubejsServer.event('Dev Server Generate Schema');
if (!req.body) {
throw new Error('Your express app config is missing body-parser middleware. Typical config can look like: `app.use(bodyParser.json({ limit: \'50mb\' }));`');
}
if (!req.body.tables) {
throw new Error('You have to select at least one table');
}
const dataSource = req.body.dataSource || 'default';
const driver = await this.cubejsServer.getDriver({
dataSource,
authInfo: null,
securityContext: null,
requestId: getRequestIdFromRequest(req),
});
const tablesSchema = req.body.tablesSchema || (await driver.tablesSchema());
if (!Object.values(SchemaFormat).includes(req.body.format)) {
throw new Error(`Unknown schema format. Must be one of ${Object.values(SchemaFormat)}`);
}
const scaffoldingTemplate = new ScaffoldingTemplate(tablesSchema, driver, {
format: req.body.format,
snakeCase: true
});
const files = scaffoldingTemplate.generateFilesByTableNames(req.body.tables, { dataSource });
await fs.emptyDir(path.join(options.schemaPath, 'cubes'));
await fs.emptyDir(path.join(options.schemaPath, 'views'));
await fs.writeFile(path.join(options.schemaPath, 'views', 'example_view.yml'), `# In Cube, views are used to expose slices of your data graph and act as data marts.
# You can control which measures and dimensions are exposed to BIs or data apps,
# as well as the direction of joins between the exposed cubes.
# You can learn more about views in documentation here - https://cube.dev/docs/schema/reference/view
# The following example shows a view defined on top of orders and customers cubes.
# Both orders and customers cubes are exposed using the "includes" parameter to
# control which measures and dimensions are exposed.
# Prefixes can also be applied when exposing measures or dimensions.
# In this case, the customers' city dimension is prefixed with the cube name,
# resulting in "customers_city" when querying the view.
# views:
# - name: example_view
#
# cubes:
# - join_path: orders
# includes:
# - status
# - created_date
#
# - total_amount
# - count
#
# - join_path: orders.customers
# prefix: true
# includes:
# - city`);
await Promise.all(files.map(file => fs.writeFile(path.join(options.schemaPath, 'cubes', file.fileName), file.content)));
res.json({ files });
}));
let lastApplyTemplatePackagesError = null;
app.get('/playground/dashboard-app-create-status', catchErrors(async (req, res) => {
const sourcePath = path.join(options.dashboardAppPath, 'src');
if (lastApplyTemplatePackagesError) {
const toThrow = lastApplyTemplatePackagesError;
lastApplyTemplatePackagesError = null;
throw toThrow;
}
if (this.applyTemplatePackagesPromise) {
if (req.query.instant) {
res.status(404).json({ error: 'Dashboard app creating' });
return;
}
await this.applyTemplatePackagesPromise;
}
// docker-compose share a volume for /dashboard-app and directory will be empty
if (!fs.pathExistsSync(options.dashboardAppPath) || fs.readdirSync(options.dashboardAppPath).length === 0) {
res.status(404).json({
error: `Dashboard app not found in '${path.resolve(options.dashboardAppPath)}' directory`
});
return;
}
if (!fs.pathExistsSync(sourcePath)) {
res.status(404).json({
error: `Dashboard app corrupted. Please remove '${path.resolve(options.dashboardAppPath)}' directory and recreate it`
});
return;
}
res.json({
status: 'created',
installedTemplates: AppContainer.getPackageVersions(options.dashboardAppPath)
});
}));
app.get('/playground/start-dashboard-app', catchErrors(async (req, res) => {
this.cubejsServer.event('Dev Server Start Dashboard App');
if (!this.dashboardAppProcess) {
const { dashboardAppPort = 3000 } = options;
this.dashboardAppProcess = spawn('npm', [
'run',
'start',
'--',
'--port',
dashboardAppPort.toString(),
...(isDocker() ? ['--host', '0.0.0.0'] : [])
], {
cwd: options.dashboardAppPath,
env: <any>{
...process.env,
PORT: dashboardAppPort
}
});
this.dashboardAppProcess.dashboardUrlPromise = new Promise((resolve) => {
this.dashboardAppProcess.stdout.on('data', (data) => {
console.log(data.toString());
if (data.toString().match(/Compiled/)) {
resolve(options.dashboardAppPort);
}
});
});
this.dashboardAppProcess.on('close', exitCode => {
if (exitCode !== 0) {
console.log(`Dashboard react-app failed with exit code ${exitCode}`);
this.cubejsServer.event('Dev Server Dashboard App Failed', { exitCode });
}
this.dashboardAppProcess = null;
});
}
await this.dashboardAppProcess.dashboardUrlPromise;
res.json({ dashboardPort: options.dashboardAppPort });
}));
app.get('/playground/dashboard-app-status', catchErrors(async (req, res) => {
this.cubejsServer.event('Dev Server Dashboard App Status');
const dashboardPort = this.dashboardAppProcess && await this.dashboardAppProcess.dashboardUrlPromise;
res.json({
running: !!dashboardPort,
dashboardPort,
dashboardAppPath: path.resolve(options.dashboardAppPath)
});
}));
let driverPromise: Promise<void> | null = null;
let driverError: Error | null = null;
app.get('/playground/driver', catchErrors(async (req: Request, res: Response) => {
const { driver } = req.query;
if (!driver || !DriverDependencies[driver]) {
return res.status(400).json('Wrong driver');
}
if (packageExists(DriverDependencies[driver])) {
return res.json({ status: 'installed' });
} else if (driverPromise) {
return res.json({ status: 'installing' });
} else if (driverError) {
return res.status(500).json({
status: 'error',
error: driverError.toString()
});
}
return res.json({ status: null });
}));
app.post('/playground/driver', catchErrors((req, res) => {
const { driver } = req.body;
if (!DriverDependencies[driver]) {
return res.status(400).json(`'${driver}' driver dependency not found`);
}
async function installDriver() {
driverError = null;
try {
await executeCommand(
'npm',
['install', DriverDependencies[driver], '--save-dev'],
{ cwd: path.resolve('.') }
);
} catch (error) {
driverError = error as Error;
} finally {
driverPromise = null;
}
}
if (!driverPromise) {
driverPromise = installDriver();
}
return res.json({
dependency: DriverDependencies[driver]
});
}));
app.post('/playground/apply-template-packages', catchErrors(async (req, res) => {
this.cubejsServer.event('Dev Server Download Template Packages');
const fetcher = process.env.TEST_TEMPLATES ? new DevPackageFetcher(repo) : new PackageFetcher(repo);
this.cubejsServer.event('Dev Server App File Write');
const { toApply, templateConfig } = req.body;
const applyTemplates = async () => {
const manifestJson = await fetcher.manifestJSON();
const response = await fetcher.downloadPackages();
let templatePackages: string[];
if (typeof toApply === 'string') {
const template = manifestJson.templates.find(({ name }) => name === toApply);
templatePackages = template.templatePackages;
} else {
templatePackages = toApply;
}
const dt = new DependencyTree(manifestJson, templatePackages);
const appContainer = new AppContainer(
dt.getRootNode(),
{
appPath: options.dashboardAppPath,
packagesPath: response.packagesPath
},
templateConfig
);
this.cubejsServer.event('Dev Server Create Dashboard App');
await appContainer.applyTemplates();
this.cubejsServer.event('Dev Server Create Dashboard App Success');
this.cubejsServer.event('Dev Server Dashboard Npm Install');
await appContainer.ensureDependencies();
this.cubejsServer.event('Dev Server Dashboard Npm Install Success');
fetcher.cleanup();
};
if (this.applyTemplatePackagesPromise) {
this.applyTemplatePackagesPromise = this.applyTemplatePackagesPromise.then(applyTemplates);
} else {
this.applyTemplatePackagesPromise = applyTemplates();
}
const promise = this.applyTemplatePackagesPromise;
promise.then(() => {
if (promise === this.applyTemplatePackagesPromise) {
this.applyTemplatePackagesPromise = null;
}
}, (err) => {
lastApplyTemplatePackagesError = err;
if (promise === this.applyTemplatePackagesPromise) {
this.applyTemplatePackagesPromise = null;
}
});
res.json(true); // TODO
}));
app.get('/playground/manifest', catchErrors(async (_, res) => {
const fetcher = process.env.TEST_TEMPLATES ? new DevPackageFetcher(repo) : new PackageFetcher(repo);
res.json(await fetcher.manifestJSON());
}));
app.get('/playground/live-preview/start/:token', catchErrors(async (req: Request, res: Response) => {
this.livePreviewWatcher.setAuth(req.params.token);
this.livePreviewWatcher.startWatch();
res.setHeader('Content-Type', 'text/html');
res.write('<html><body><script>window.close();</script></body></html>');
res.end();
}));
app.get('/playground/live-preview/stop', catchErrors(async (req, res) => {
this.livePreviewWatcher.stopWatch();
res.json({ active: false });
}));
app.get('/playground/live-preview/status', catchErrors(async (req, res) => {
const statusObj = await this.livePreviewWatcher.getStatus();
res.json(statusObj);
}));
app.post('/playground/live-preview/token', catchErrors(async (req, res) => {
const token = await this.livePreviewWatcher.createTokenWithPayload(req.body);
res.json({ token });
}));
app.use(serveStatic(path.join(__dirname, '../../../playground'), {
lastModified: false,
etag: false,
setHeaders: (res, url) => {
if (url.indexOf('/index.html') !== -1) {
res.setHeader('Cache-Control', 'no-cache');
}
}
}));
/**
* The `/playground/test-connection` endpoint request.
*/
type TestConnectionRequest = {
body: {
dataSource?: string,
variables: {
[env: string]: string,
},
},
};
app.post('/playground/test-connection', catchErrors(
async (req: TestConnectionRequest, res) => {
const { dataSource, variables } = req.body || {};
// With multiple data sources enabled, we need to use
// CUBEJS_DS_<dataSource>_DB_TYPE environment variable
// instead of CUBEJS_DB_TYPE.
const type = keyByDataSource('CUBEJS_DB_TYPE', dataSource);
let driver: BaseDriver | null = null;
try {
if (!variables || !variables[type]) {
throw new Error(`${type} is required`);
}
// Backup env variables for restoring
const originalProcessEnv = process.env;
process.env = {
...process.env,
};
// We suppose that variables names passed to the endpoint have their
// final form depending on whether multiple data sources are enabled
// or not. So, we don't need to convert anything here.
for (const [envName, value] of Object.entries(variables)) {
process.env[envName] = <string>value;
}
// With multiple data sources enabled, we need to put the dataSource
// parameter to the driver instance to read an appropriate set of
// driver configuration parameters. It can be undefined if multiple
// data source is disabled.
driver = CubejsServerCore.createDriver(
<DatabaseType>variables[type],
{ dataSource },
);
// Restore original process.env
process.env = originalProcessEnv;
await driver.testConnection();
this.cubejsServer.event('test_database_connection_success');
return res.json('ok');
} catch (error) {
this.cubejsServer.event('test_database_connection_error');
return res.status(400).json({
error: error.toString()
});
} finally {
if (driver && (<any>driver).release) {
await (<any>driver).release();
}
}
}
));
app.post('/playground/env', catchErrors(async (req, res) => {
let { variables = {} } = req.body || {};
if (!variables.CUBEJS_API_SECRET) {
variables.CUBEJS_API_SECRET = options.apiSecret;
}
let envs: Record<string, string> = {};
const envPath = path.join(process.cwd(), '.env');
if (fs.existsSync(envPath)) {
envs = dotenv.parse(fs.readFileSync(envPath));
}
const schemaPath = envs.CUBEJS_SCHEMA_PATH || process.env.CUBEJS_SCHEMA_PATH || 'model';
variables.CUBEJS_EXTERNAL_DEFAULT = 'true';
variables.CUBEJS_SCHEDULED_REFRESH_DEFAULT = 'true';
variables.CUBEJS_DEV_MODE = 'true';
variables.CUBEJS_SCHEMA_PATH = schemaPath;
variables = Object.entries(variables).map(([key, value]) => ([key, value].join('=')));
const repositoryPath = path.join(process.cwd(), schemaPath);
if (!fs.existsSync(repositoryPath)) {
fs.mkdirSync(repositoryPath);
}
fs.writeFileSync(path.join(process.cwd(), '.env'), variables.join('\n'));
if (!fs.existsSync(path.join(process.cwd(), 'package.json'))) {
fs.writeFileSync(
path.join(process.cwd(), 'package.json'),
JSON.stringify({
name: 'cube-docker',
version: '0.0.1',
private: true,
createdAt: new Date().toJSON(),
dependencies: {}
}, null, 2)
);
}
dotenv.config({ override: true });
await this.cubejsServer.resetInstanceState();
res.status(200).json(req.body.variables || {});
}));
app.post('/playground/token', catchErrors(async (req, res) => {
const { payload = {} } = req.body;
const jwtOptions = typeof payload.exp != null ? {} : { expiresIn: '1d' };
const token = jwt.sign(payload, options.apiSecret, jwtOptions);
res.json({ token });
}));
app.post('/playground/schema/pre-aggregation', catchErrors(async (req: Request, res: Response) => {
const { cubeName, preAggregationName, code } = req.body;
const schemaConverter = new CubeSchemaConverter(this.cubejsServer.repository, [
new CubePreAggregationConverter({
cubeName,
preAggregationName,
code
})
]);
try {
await schemaConverter.generate();
} catch (error) {
res.status(400).json({ error: (error as Error).message || error });
}
schemaConverter.getSourceFiles().forEach(({ cubeName: currentCubeName, fileName, source }) => {
if (currentCubeName === cubeName) {
this.cubejsServer.repository.writeDataSchemaFile(fileName, source);
}
});
res.json('ok');
}));
}
protected getIdentifier(apiSecret: string): string {
return crypto.createHash('md5')
.update(apiSecret)
.digest('hex')
.replace(/[^\d]/g, '')
.slice(0, 10);
}
}