-
Notifications
You must be signed in to change notification settings - Fork 50
/
index.js
104 lines (87 loc) · 2.9 KB
/
index.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
import { makeHandleRequest, serveStaticApp, serveMonaco } from './common';
import { proxyHandler } from './proxy.js';
import { handleTracking } from './tracking.js';
import jsyaml from 'js-yaml';
//import { requestLogger } from './utils/other'; //uncomment this to log the outgoing traffic
const express = require('express');
const compression = require('compression');
const cors = require('cors');
const fs = require('fs');
const merge = require('lodash.merge');
global.config = {};
try {
// config from the copnfiguration file
const defaultConfig = jsyaml.load(
fs.readFileSync('./settings/defaultConfig.yaml'),
);
// config retrieved from busola's config map
// path exist after docker build
if (fs.existsSync('./config/config.yaml')) {
const configFromMap = jsyaml.load(fs.readFileSync('./config/config.yaml'));
global.config = merge(defaultConfig, configFromMap).config;
} else {
global.config = defaultConfig.config;
}
} catch (e) {
console.log('Error while reading the configuration files', e?.message || e);
}
const app = express();
app.disable('x-powered-by');
app.use(express.raw({ type: '*/*', limit: '100mb' }));
const gzipEnabled = global.config.features?.GZIP?.isEnabled;
if (gzipEnabled)
app.use(
compression({
filter: (req, res) => {
if (/\?.*follow=/.test(req.originalUrl)) {
// compression interferes with ReadableStreams. Small chunks are not transmitted for unknown reason
return false;
}
// fallback to standard filter function
return compression.filter(req, res);
},
}),
);
if (process.env.NODE_ENV === 'development') {
app.use(cors({ origin: '*' }));
}
app.get('/proxy', proxyHandler);
let server = null;
if (
process.env.BUSOLA_SSL_ENABLED === 1 &&
process.env.BUSOLA_SSL_KEY_FILE !== '' &&
process.env.BUSOLA_SSL_CRT_FILE !== ''
) {
const https = require('https');
const options = {
key: fs.readFileSync(process.env.BUSOLA_SSL_KEY_FILE),
cert: fs.readFileSync(process.env.BUSOLA_SSL_CRT_FILE),
};
server = https.createServer(options, app);
} else {
const http = require('http');
server = http.createServer(app);
}
// requestLogger(require("http")); //uncomment this to log the outgoing traffic
// requestLogger(require("https")); //uncomment this to log the outgoing traffic
const port = process.env.PORT || 3001;
const address = process.env.ADDRESS || 'localhost';
const isDocker = process.env.IS_DOCKER === 'true';
const handleRequest = makeHandleRequest();
if (isDocker) {
// Running in dev mode
// yup, order matters here
serveMonaco(app);
app.use('/backend', handleRequest);
serveStaticApp(app, '/', '/core-ui');
} else {
// Running in prod mode
handleTracking(app);
app.use('/backend', handleRequest);
}
process.on('SIGINT', function() {
process.exit();
});
server.listen(port, '0.0.0.0', address, () => {
console.log(`Busola backend server started @ ${port}!`);
});