From af39b01d4a2a72a30d3a2fe54dc95cfe4fe9299a Mon Sep 17 00:00:00 2001 From: MartinBraquet Date: Sun, 21 Sep 2025 16:56:42 +0200 Subject: [PATCH] Reload env config after setting env vars --- .env.example | 2 +- common/src/envs/constants.ts | 23 ++++++++++++++++++++++- common/src/envs/prod.ts | 17 +++++++++++++---- common/src/secrets.ts | 5 +++++ 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/.env.example b/.env.example index 7af330d..64e0284 100644 --- a/.env.example +++ b/.env.example @@ -12,4 +12,4 @@ GEODB_API_KEY= # For sending emails (e.g. for user sign up, password reset, notifications, etc.). # Create a free account at https://resend.com and get an API key. Should start with "re_". -RESEND_KEY= \ No newline at end of file +RESEND_KEY= diff --git a/common/src/envs/constants.ts b/common/src/envs/constants.ts index 6c1d9fb..ba0af0c 100644 --- a/common/src/envs/constants.ts +++ b/common/src/envs/constants.ts @@ -15,6 +15,8 @@ export function isModId(id: string) { return MOD_IDS.includes(id) } +export const ENV = isProd() ? 'prod' : 'dev' + export const LOCAL_WEB_DOMAIN = 'localhost:3000'; export const LOCAL_BACKEND_DOMAIN = 'localhost:8088'; @@ -22,7 +24,26 @@ export const IS_GOOGLE_CLOUD = !!process.env.GOOGLE_CLOUD_PROJECT export const IS_VERCEL = !!process.env.NEXT_PUBLIC_VERCEL export const IS_LOCAL = !IS_GOOGLE_CLOUD && !IS_VERCEL export const HOSTING_ENV = IS_GOOGLE_CLOUD ? 'Google Cloud' : IS_VERCEL ? 'Vercel' : IS_LOCAL ? 'local' : 'unknown' -console.log(`Running in ${HOSTING_ENV}`, isProd() ? '(prod)' : '(dev)'); +console.log(`Running in ${HOSTING_ENV} (${ENV})`,); + +// class MissingKeyError implements Error { +// constructor(key: string) { +// this.message = `Missing ENV_CONFIG.${key} in ${ENV}. If you're running locally, you most likely want to run in dev mode: yarn dev.` +// this.name = 'MissingKeyError' +// } +// +// message: string; +// name: string; +// } + +// for (const key of ['supabaseAnonKey', 'supabasePwd', 'googleApplicationCredentials'] as const) { +// if (!(key in ENV_CONFIG) || ENV_CONFIG[key as keyof typeof ENV_CONFIG] == null) { +// throw new MissingKeyError(key) +// } +// } +// if (!ENV_CONFIG.firebaseConfig.apiKey) { +// throw new MissingKeyError('firebaseConfig.apiKey') +// } export const DOMAIN = IS_LOCAL ? LOCAL_WEB_DOMAIN : ENV_CONFIG.domain export const BACKEND_DOMAIN = IS_LOCAL ? LOCAL_BACKEND_DOMAIN : ENV_CONFIG.backendDomain diff --git a/common/src/envs/prod.ts b/common/src/envs/prod.ts index b65a05c..eecce3c 100644 --- a/common/src/envs/prod.ts +++ b/common/src/envs/prod.ts @@ -36,11 +36,11 @@ export const PROD_CONFIG: EnvConfig = { domain: 'compassmeet.com', backendDomain: 'api.compassmeet.com', supabaseInstanceId: 'ltzepxnhhnrnvovqblfr', - supabaseAnonKey: process.env.NEXT_PUBLIC_SUPABASE_KEY || '', - supabasePwd: process.env.SUPABASE_DB_PASSWORD || '', - googleApplicationCredentials: process.env.GOOGLE_APPLICATION_CREDENTIALS, + supabaseAnonKey: '', + supabasePwd: '', + googleApplicationCredentials: undefined, firebaseConfig: { - apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY || '', + apiKey: '', authDomain: "compass-130ba.firebaseapp.com", projectId: "compass-130ba", storageBucket: "compass-130ba.firebasestorage.app", @@ -57,3 +57,12 @@ export const PROD_CONFIG: EnvConfig = { ], faviconPath: '/favicon.ico', } + +export const refreshConfig = () => { + PROD_CONFIG.supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_KEY || '' + PROD_CONFIG.supabasePwd = process.env.SUPABASE_DB_PASSWORD || '' + PROD_CONFIG.googleApplicationCredentials = process.env.GOOGLE_APPLICATION_CREDENTIALS + PROD_CONFIG.firebaseConfig.apiKey = process.env.NEXT_PUBLIC_FIREBASE_API_KEY || '' +} + +refreshConfig() diff --git a/common/src/secrets.ts b/common/src/secrets.ts index ec4affd..43d9cc7 100644 --- a/common/src/secrets.ts +++ b/common/src/secrets.ts @@ -1,6 +1,7 @@ import {SecretManagerServiceClient} from '@google-cloud/secret-manager' import {zip} from 'lodash' import {IS_LOCAL} from "common/envs/constants"; +import {refreshConfig} from "common/envs/prod"; // List of secrets that are available to backend (api, functions, scripts, etc.) // Edit them at: @@ -44,6 +45,8 @@ export const getSecrets = async (credentials?: any, ...ids: SecretId[]) => { const secretIds = ids.length > 0 ? ids : secrets + console.log('secretIds', secretIds) + const fullSecretNames = secretIds.map( (secret: string) => `${client.projectPath(projectId)}/secrets/${secret}/versions/latest` @@ -70,7 +73,9 @@ export const loadSecretsToEnv = async (credentials?: any) => { for (const [key, value] of Object.entries(allSecrets)) { if (key && value) { process.env[key] = value + // console.log(key, value) } } + refreshConfig() }