oneuptime/Dashboard/index.ts

140 lines
4.4 KiB
TypeScript
Raw Normal View History

2022-04-01 20:17:29 +00:00
import {
2022-03-21 22:26:54 +00:00
ExpressRequest,
ExpressResponse,
2022-03-22 11:19:12 +00:00
ExpressStatic,
2022-04-10 21:57:14 +00:00
} from 'CommonServer/Utils/Express';
2021-01-26 14:03:42 +00:00
2022-04-10 21:57:14 +00:00
import app from 'CommonServer/utils/StartServer';
2021-04-09 10:35:09 +00:00
2022-04-01 20:17:29 +00:00
import path from 'path';
2019-08-02 12:56:16 +00:00
2022-03-19 17:00:54 +00:00
app.get(
['/env.js', '/dashboard/env.js'],
2022-03-21 22:26:02 +00:00
(req: ExpressRequest, res: ExpressResponse) => {
2022-03-19 17:00:54 +00:00
const isClustLocal = req.get('host').includes('cluster.local');
if (!isClustLocal) {
global.dashboardHost = 'https://' + req.host + '/dashboard';
2022-02-28 12:04:28 +00:00
2022-03-19 17:00:54 +00:00
global.homeHost = 'https://' + req.host;
2022-02-28 12:04:28 +00:00
2022-03-19 17:00:54 +00:00
global.accountsHost = 'https://' + req.host + '/accounts';
2022-02-28 12:04:28 +00:00
2022-03-19 17:00:54 +00:00
global.backendHost = 'https://' + req.host + '/api';
2022-02-28 12:04:28 +00:00
2022-03-19 17:00:54 +00:00
global.realtimeHost = 'https://' + req.host + '/realtime';
}
if (req.host.includes('localhost')) {
if (req.get('host').includes('localhost:')) {
global.dashboardHost =
2022-03-20 22:14:51 +00:00
'http://' + req.host + ':' + (process.env['PORT'] || 3002);
2022-02-28 12:04:28 +00:00
2022-03-19 17:00:54 +00:00
global.accountsHost = 'http://' + req.host + ':' + 3003;
2022-02-28 12:04:28 +00:00
2022-03-19 17:00:54 +00:00
global.homeHost = 'http://' + req.host + ':' + 1444;
2022-02-28 12:04:28 +00:00
2022-03-19 17:00:54 +00:00
global.backendHost = 'http://' + req.host + ':' + 3002;
2022-02-28 12:04:28 +00:00
2022-03-19 17:00:54 +00:00
global.realtimeHost = 'http://' + req.host + ':' + 3300;
} else if (!isClustLocal) {
global.dashboardHost = 'http://' + req.host + '/dashboard';
2022-02-28 12:04:28 +00:00
2022-03-19 17:00:54 +00:00
global.accountsHost = 'http://' + req.host + '/accounts';
2022-02-28 12:04:28 +00:00
2022-03-19 17:00:54 +00:00
global.homeHost = 'http://' + req.host;
2022-02-28 12:04:28 +00:00
2022-03-19 17:00:54 +00:00
global.backendHost = 'http://' + req.host + '/api';
2022-02-28 12:04:28 +00:00
2022-03-19 17:00:54 +00:00
global.realtimeHost = 'http://' + req.host + '/realtime';
}
2020-04-29 15:08:13 +00:00
}
2020-03-23 20:59:33 +00:00
2022-03-19 17:00:54 +00:00
const env = {
2022-03-22 11:19:12 +00:00
REACT_APP_IS_SAAS_SERVICE: process.env['IS_SAAS_SERVICE'],
2022-03-19 17:00:54 +00:00
...(!isClustLocal && {
REACT_APP_HOST: global.dashboardHost,
REACT_APP_ACCOUNTS_HOST: global.accountsHost,
REACT_APP_BACKEND_HOST: global.backendHost,
}),
REACT_APP_DOMAIN: req.host,
2022-03-22 11:19:12 +00:00
REACT_APP_STRIPE_PUBLIC_KEY: process.env['STRIPE_PUBLIC_KEY'],
2022-03-19 17:00:54 +00:00
REACT_APP_PUSHNOTIFICATION_PUBLIC_KEY:
process.env.PUSHNOTIFICATION_PUBLIC_KEY,
2022-03-22 11:19:12 +00:00
REACT_APP_AMPLITUDE_PUBLIC_KEY: process.env['AMPLITUDE_PUBLIC_KEY'],
REACT_APP_VERSION: process.env['REACT_APP_VERSION'],
2022-03-19 17:00:54 +00:00
REACT_APP_STATUSPAGE_DOMAIN: process.env.STATUSPAGE_DOMAIN,
};
res.contentType('application/javascript');
res.send('window._env = ' + JSON.stringify(env));
}
);
2019-09-09 13:40:01 +00:00
//APP VERSION
2022-03-19 17:00:54 +00:00
app.use(
['/dashboard/api/version', '/dashboard/version'],
2022-04-01 20:17:29 +00:00
(_req: ExpressRequest, res: ExpressResponse) => {
2022-03-19 17:00:54 +00:00
res.setHeader('Content-Type', 'application/json');
2022-03-22 11:19:12 +00:00
res.json({ dashboardVersion: process.env['npm_package_version'] });
2022-03-19 17:00:54 +00:00
}
);
2022-03-19 17:00:54 +00:00
app.get(
['/dashboard/status', '/status'],
2022-04-01 20:17:29 +00:00
(_req: ExpressRequest, res: ExpressResponse) => {
2022-03-19 17:00:54 +00:00
res.setHeader('Content-Type', 'application/json');
res.send(
JSON.stringify({
status: 200,
message: 'Service Status - OK',
serviceType: 'oneuptime-dashboard',
})
);
}
);
2021-12-14 17:31:23 +00:00
2022-03-22 11:19:12 +00:00
app.use(ExpressStatic(path.join(__dirname, 'build')));
2021-12-29 16:20:17 +00:00
2020-04-20 20:17:59 +00:00
app.use(
'/dashboard/static/js',
2022-03-22 11:19:12 +00:00
ExpressStatic(path.join(__dirname, 'build', 'static', 'js'))
2020-04-20 20:17:59 +00:00
);
2020-03-22 14:22:56 +00:00
2022-03-22 11:19:12 +00:00
app.use('/dashboard', ExpressStatic(path.join(__dirname, 'build')));
2021-12-29 16:20:17 +00:00
// app.use(
// /^\/dashboard\/static\/js\/([0-9]|[1-9][0-9]|[1-9][0-9][0-9])\.(.+)\.chunk\.js$/,
2022-04-02 11:35:36 +00:00
// function(req:Request, res: ExpressResponse, next: NextFunction) {
2021-12-29 16:20:17 +00:00
// let baseUrls = req.baseUrl;
// baseUrls = baseUrls.split('/');
// const fileName = baseUrls[baseUrls.length - 1];
// if (fileName) {
// res.sendFile(
// path.join(__dirname, 'build', 'static', 'js', fileName)
// );
// } else {
// return next();
// }
// }
// );
// app.use(/^\/dashboard\/static\/js\/main\.(.+)\.chunk\.js$/, function(
// req,
// res,
// next
// ) {
// let baseUrls = req.baseUrl;
// baseUrls = baseUrls.split('/');
// const fileName = baseUrls[baseUrls.length - 1];
// if (fileName) {
// res.sendFile(path.join(__dirname, 'build', 'static', 'js', fileName));
// } else {
// return next();
// }
// });
2022-04-01 20:17:29 +00:00
app.get('/*', (_req: ExpressRequest, res: ExpressResponse) => {
2020-02-27 11:15:46 +00:00
res.sendFile(path.join(__dirname, 'build', 'index.html'));
2019-08-02 12:56:16 +00:00
});