fix env vars

This commit is contained in:
Simon Larsen 2022-12-29 15:50:53 +00:00
parent bebc275b2e
commit 37fdc73393
No known key found for this signature in database
GPG Key ID: AB45983AA9C81CDE
8 changed files with 63 additions and 71 deletions

View File

@ -1,48 +1,16 @@
import App from 'CommonServer/Utils/StartServer';
import path from 'path';
import Express, {
ExpressApplication,
ExpressRequest,
ExpressResponse,
ExpressStatic,
} from 'CommonServer/Utils/Express';
import Express, { ExpressApplication } from 'CommonServer/Utils/Express';
import logger from 'CommonServer/Utils/Logger';
export const APP_NAME: string = 'accounts';
const app: ExpressApplication = Express.getExpressApp();
app.use(ExpressStatic(path.join(__dirname, 'public')));
app.get([`/${APP_NAME}/env.js`, '/env.js'], (_req: ExpressRequest, res: ExpressResponse) => {
const script = `
if(!process){
process = {}
}
if(!process.env){
process.env = {}
}
const envVars = ${JSON.stringify(process.env)};
process.env = JSON.parse(envVars);
window.process = process;
`;
res.writeHead(200, { "Content-Type": "text/javascript" });
res.send(script);
});
app.use(`/${APP_NAME}`, ExpressStatic(path.join(__dirname, 'public')));
app.get('/*', (_req: ExpressRequest, res: ExpressResponse) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
const init: Function = async (): Promise<void> => {
try {
// init the app
await App(APP_NAME);
await App(APP_NAME, undefined, true);
} catch (err) {
logger.error('App Init Failed:');
logger.error(err);

View File

@ -270,4 +270,25 @@ export default class Response {
oneUptimeResponse.end(html);
this.logResponse(req, res, { html: html as string });
}
public static sendJavaScriptResponse(
req: ExpressRequest,
res: ExpressResponse,
javascript: string
): void {
const oneUptimeRequest: OneUptimeRequest = req as OneUptimeRequest;
const oneUptimeResponse: OneUptimeResponse = res as OneUptimeResponse;
oneUptimeResponse.set(
'ExpressRequest-Id',
oneUptimeRequest.id.toString()
);
oneUptimeResponse.set('Pod-Id', process.env['POD_NAME']);
oneUptimeResponse.logBody = { javascript: javascript as string };
oneUptimeResponse.writeHead(200, { 'Content-Type': 'text/javascript' });
oneUptimeResponse.end(javascript);
this.logResponse(req, res, { javascript: javascript as string });
}
}

View File

@ -12,8 +12,8 @@ import Express, {
ExpressApplication,
RequestHandler,
OneUptimeRequest,
ExpressStatic,
} from './Express';
// Connect common api's.
import CommonAPI from '../API/Index';
import NotFoundException from 'Common/Types/Exception/NotFoundException';
@ -93,12 +93,42 @@ app.use(logRequest);
const init: Function = async (
appName: string,
port?: Port
port?: Port,
isFrontendApp?: boolean
): Promise<ExpressApplication> => {
await Express.launchApplication(appName, port);
LocalCache.setString('app', 'name', appName);
CommonAPI(appName);
if (isFrontendApp) {
app.use(ExpressStatic('/usr/src/app/public'));
app.get(
[`/${appName}/env.js`, '/env.js'],
(req: ExpressRequest, res: ExpressResponse) => {
const script: string = `
if(!window.process){
window.process = {}
}
if(!window.process.env){
window.process.env = {}
}
const envVars = '${JSON.stringify(process.env)}';
window.process.env = JSON.parse(envVars);
`;
Response.sendJavaScriptResponse(req, res, script);
}
);
app.use(`/${appName}`, ExpressStatic('/usr/src/app/public'));
app.get('/*', (_req: ExpressRequest, res: ExpressResponse) => {
res.sendFile('/usr/src/app/public/index.html');
});
}
// Attach Error Handler.
app.use(
(

View File

@ -6,7 +6,7 @@ import URL from 'Common/Types/API/URL';
import SubscriptionPlan from 'Common/Types/Billing/SubscriptionPlan';
export const env: Function = (key: string): string => {
return process.env[key] || '';
return process.env[key] || window.process.env[key] || '';
};
export const HTTP_PROTOCOL: Protocol =

View File

@ -1,29 +1,15 @@
import App from 'CommonServer/Utils/StartServer';
import path from 'path';
import Express, {
ExpressApplication,
ExpressRequest,
ExpressResponse,
ExpressStatic,
} from 'CommonServer/Utils/Express';
import Express, { ExpressApplication } from 'CommonServer/Utils/Express';
import logger from 'CommonServer/Utils/Logger';
export const APP_NAME: string = 'dashboard';
const app: ExpressApplication = Express.getExpressApp();
app.use(ExpressStatic(path.join(__dirname, 'public')));
app.use(`/${APP_NAME}`, ExpressStatic(path.join(__dirname, 'public')));
app.get('/*', (_req: ExpressRequest, res: ExpressResponse) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
const init: Function = async (): Promise<void> => {
try {
// init the app
await App(APP_NAME);
await App(APP_NAME, undefined, true);
} catch (err) {
logger.error('App Init Failed:');
logger.error(err);

View File

@ -26,6 +26,7 @@
<meta name="msapplication-TileColor" content="#121212">
<meta name="msapplication-TileImage" content="/dashboard/assets/img/favicons/mstile-144x144.png">
<meta name="theme-color" content="#121212">
<script src="/dashboard/env.js"></script>
<title>OneUptime Dashboard</title>

View File

@ -1,11 +1,5 @@
import App from 'CommonServer/Utils/StartServer';
import path from 'path';
import Express, {
ExpressApplication,
ExpressRequest,
ExpressResponse,
ExpressStatic,
} from 'CommonServer/Utils/Express';
import Express, { ExpressApplication } from 'CommonServer/Utils/Express';
import logger from 'CommonServer/Utils/Logger';
import Port from 'Common/Types/Port';
import { PostgresAppInstance } from 'CommonServer/Infrastructure/PostgresDatabase';
@ -13,18 +7,10 @@ export const APP_NAME: string = 'status-page';
const app: ExpressApplication = Express.getExpressApp();
app.use(ExpressStatic(path.join(__dirname, 'public')));
app.use(`/${APP_NAME}`, ExpressStatic(path.join(__dirname, 'public')));
app.get('/*', (_req: ExpressRequest, res: ExpressResponse) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
const init: Function = async (): Promise<void> => {
try {
// init the app
await App(APP_NAME, new Port(3105));
await App(APP_NAME, new Port(3105), true);
// connect to the database.
await PostgresAppInstance.connect(

View File

@ -14,7 +14,7 @@
<meta name="theme-color" content="#000000">
<meta name="slack-app-id" content="ACVBMTPJQ">
<meta name="description" content="This is the login page for OneUptime Dashboard">
<script src="/status-page/env.js"></script>
<title>Status Page</title>