This repository has been archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demos.js
77 lines (66 loc) · 2.1 KB
/
demos.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
/**
* @license
* Copyright 2019, Mulesoft.
*
* THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF
* THIS CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE").
* THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.
* ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR
* COPYRIGHT LAW IS PROHIBITED.
*
* BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE,
* YOU ACCEPT AND AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE.
* THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED HERE IN CONSIDERATION
* OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
*
* See LICENSE.md for license content.
*/
import * as traceAgent from '@google-cloud/trace-agent';
import * as debugAgent from '@google-cloud/debug-agent';
import path from 'path';
import express from 'express';
import serveStatic from 'serve-static';
import fs from 'fs';
import compression from 'compression';
import config from './config.js';
import { requestLogger } from './lib/logging.js';
/** @typedef {import('net').AddressInfo} AddressInfo */
const IS_PRODUCTION = config.get('NODE_ENV') === 'production';
if (IS_PRODUCTION) {
traceAgent.start();
debugAgent.start();
}
const app = express();
export default app;
app.disable('etag');
app.disable('x-powered-by');
app.set('trust proxy', true);
app.use(requestLogger);
app.use(compression());
app.get('/_ah/health', (req, res) => {
res.status(200).send('ok');
});
const serveDemo = serveStatic('demo-dist');
const demoDev = (req, res) => {
return () => {
const index = path.join('demo-dist', 'index.html');
fs.readFile(index, 'utf8', (err, data) => {
if (err) {
res.status(500).send({
error: 'Unable to read demo app index file',
});
} else {
res.set('Content-Type', 'text/html');
res.send(data);
}
});
};
};
app.get('*', (req, res) => {
serveDemo(req, res, demoDev(req, res));
});
const server = app.listen(config.get('PORT'), () => {
const { port } = /** @type AddressInfo */ (server.address());
/* eslint-disable-next-line no-console */
console.log(`Demos app is listening on port ${port}`);
});