forked from konstructio/console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
107 lines (93 loc) · 2.7 KB
/
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
/* eslint-disable no-undef */
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path');
const http = require('http');
const NextServer = require('next/dist/server/next-server').default;
const defaultNextConfig = require('next/dist/server/config-shared').defaultConfig;
const Analytics = require('analytics-node');
process.env.NODE_ENV = 'production';
process.chdir(__dirname);
const nextConfig = require('./next.config');
const { GIT_PROVIDER, HEARTBEAT_PERIOD_MINUTES } = process.env;
process.on('SIGTERM', () => process.exit(0));
process.on('SIGINT', () => process.exit(0));
let handler;
const server = http.createServer(async (req, res) => {
try {
await handler(req, res);
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
res.statusCode = 500;
res.end('internal server error');
}
});
const currentPort = parseInt(process.env.PORT, 10) || 3000;
const ANALYTICS_ID = '0gAYkX5RV3vt7s4pqCOOsDb6WHPLT30M';
function sendHeartbeat() {
const {
CLOUD,
CLUSTER_ID,
CLUSTER_TYPE,
DOMAIN_NAME,
KUBEFIRST_TEAM,
KUBEFIRST_VERSION,
USE_TELEMETRY,
} = process.env;
try {
const isTelemetryEnabled = USE_TELEMETRY === 'true';
const analytics = new Analytics(ANALYTICS_ID, {
enable: isTelemetryEnabled,
});
if (isTelemetryEnabled) {
analytics.identify({
userId: CLUSTER_ID,
});
analytics.track({
userId: CLUSTER_ID,
event: 'kubefirst.console.healthz',
properties: {
cli_version: KUBEFIRST_VERSION,
cloud_provider: CLOUD,
cluster_id: CLUSTER_ID,
cluster_type: CLUSTER_TYPE,
domain: DOMAIN_NAME,
git_provider: GIT_PROVIDER,
kubefirst_team: KUBEFIRST_TEAM,
},
});
}
} catch (error) {
// eslint-disable-next-line no-console
console.log('error sending hearbeat event', error);
}
}
sendHeartbeat();
setInterval(sendHeartbeat, (HEARTBEAT_PERIOD_MINUTES || 20) * 60 * 1000);
server.listen(currentPort, (err) => {
if (err) {
// eslint-disable-next-line no-console
console.error('Failed to start server', err);
process.exit(1);
}
const nextServer = new NextServer({
hostname: 'localhost',
port: currentPort,
dir: path.join(__dirname),
customServer: true,
dev: false,
conf: {
...defaultNextConfig,
...nextConfig,
distDir: '.next',
experimental: {
...defaultNextConfig.experimental,
...nextConfig.experimental,
serverComponents: true,
},
},
});
handler = nextServer.getRequestHandler();
// eslint-disable-next-line no-console
console.log('Kubefirst is Ready on port', currentPort);
});