feat: added runtime env config endpoint

This commit is contained in:
=
2025-01-06 14:56:50 +05:30
parent 3291f1f908
commit d0648ca596
3 changed files with 27 additions and 7 deletions
+2
View File
@@ -157,6 +157,8 @@ const envSchema = z
INFISICAL_CLOUD: zodStrBool.default("false"), INFISICAL_CLOUD: zodStrBool.default("false"),
MAINTENANCE_MODE: zodStrBool.default("false"), MAINTENANCE_MODE: zodStrBool.default("false"),
CAPTCHA_SECRET: zpStr(z.string().optional()), CAPTCHA_SECRET: zpStr(z.string().optional()),
CAPTCHA_SITE_KEY: zpStr(z.string().optional()),
INTERCOM_ID: zpStr(z.string().optional()),
// TELEMETRY // TELEMETRY
OTEL_TELEMETRY_COLLECTION_ENABLED: zodStrBool.default("false"), OTEL_TELEMETRY_COLLECTION_ENABLED: zodStrBool.default("false"),
+4 -6
View File
@@ -120,12 +120,10 @@ export const main = async ({ db, hsmModule, auditLogDb, smtp, logger, queue, key
await server.register(registerRoutes, { smtp, queue, db, auditLogDb, keyStore, hsmModule }); await server.register(registerRoutes, { smtp, queue, db, auditLogDb, keyStore, hsmModule });
if (appCfg.isProductionMode) { await server.register(registerServeUI, {
await server.register(registerServeUI, { standaloneMode: appCfg.STANDALONE_MODE || IS_PACKAGED,
standaloneMode: appCfg.STANDALONE_MODE || IS_PACKAGED, dir: path.join(__dirname, IS_PACKAGED ? "../../../" : "../../")
dir: path.join(__dirname, IS_PACKAGED ? "../../../" : "../../") });
});
}
await server.ready(); await server.ready();
server.swagger(); server.swagger();
+21 -1
View File
@@ -2,7 +2,7 @@ import path from "node:path";
import staticServe from "@fastify/static"; import staticServe from "@fastify/static";
import { IS_PACKAGED } from "@app/lib/config/env"; import { getConfig, IS_PACKAGED } from "@app/lib/config/env";
// to enabled this u need to set standalone mode to true // to enabled this u need to set standalone mode to true
export const registerServeUI = async ( export const registerServeUI = async (
@@ -15,6 +15,26 @@ export const registerServeUI = async (
dir: string; dir: string;
} }
) => { ) => {
// use this only for frontend runtime static non-sensitive configuration in standalone mode
// that app needs before loading like posthog dsn key
// for most of the other usecase use server config
server.route({
method: "GET",
url: "/runtime-ui-env.js",
handler: (_req, res) => {
const appCfg = getConfig();
void res.type("application/javascript");
const config = {
CAPTCHA_SITE_KEY: appCfg.CAPTCHA_SITE_KEY,
POSTHOG_API_KEY: appCfg.POSTHOG_PROJECT_API_KEY,
INTERCOM_ID: appCfg.INTERCOM_ID,
TELEMETRY_CAPTURING_ENABLED: appCfg.TELEMETRY_ENABLED
};
const js = `window.__INFISICAL_RUNTIME_ENV__ = Object.freeze(${JSON.stringify(config)});`;
void res.send(js);
}
});
if (standaloneMode) { if (standaloneMode) {
const frontendName = IS_PACKAGED ? "frontend" : "frontend-build"; const frontendName = IS_PACKAGED ? "frontend" : "frontend-build";
const frontendPath = path.join(dir, frontendName); const frontendPath = path.join(dir, frontendName);