Skip to content

Commit af39b01

Browse files
committed
Reload env config after setting env vars
1 parent 73a0a5f commit af39b01

4 files changed

Lines changed: 41 additions & 6 deletions

File tree

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ GEODB_API_KEY=
1212

1313
# For sending emails (e.g. for user sign up, password reset, notifications, etc.).
1414
# Create a free account at https://resend.com and get an API key. Should start with "re_".
15-
RESEND_KEY=
15+
RESEND_KEY=

common/src/envs/constants.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,35 @@ export function isModId(id: string) {
1515
return MOD_IDS.includes(id)
1616
}
1717

18+
export const ENV = isProd() ? 'prod' : 'dev'
19+
1820
export const LOCAL_WEB_DOMAIN = 'localhost:3000';
1921
export const LOCAL_BACKEND_DOMAIN = 'localhost:8088';
2022

2123
export const IS_GOOGLE_CLOUD = !!process.env.GOOGLE_CLOUD_PROJECT
2224
export const IS_VERCEL = !!process.env.NEXT_PUBLIC_VERCEL
2325
export const IS_LOCAL = !IS_GOOGLE_CLOUD && !IS_VERCEL
2426
export const HOSTING_ENV = IS_GOOGLE_CLOUD ? 'Google Cloud' : IS_VERCEL ? 'Vercel' : IS_LOCAL ? 'local' : 'unknown'
25-
console.log(`Running in ${HOSTING_ENV}`, isProd() ? '(prod)' : '(dev)');
27+
console.log(`Running in ${HOSTING_ENV} (${ENV})`,);
28+
29+
// class MissingKeyError implements Error {
30+
// constructor(key: string) {
31+
// this.message = `Missing ENV_CONFIG.${key} in ${ENV}. If you're running locally, you most likely want to run in dev mode: yarn dev.`
32+
// this.name = 'MissingKeyError'
33+
// }
34+
//
35+
// message: string;
36+
// name: string;
37+
// }
38+
39+
// for (const key of ['supabaseAnonKey', 'supabasePwd', 'googleApplicationCredentials'] as const) {
40+
// if (!(key in ENV_CONFIG) || ENV_CONFIG[key as keyof typeof ENV_CONFIG] == null) {
41+
// throw new MissingKeyError(key)
42+
// }
43+
// }
44+
// if (!ENV_CONFIG.firebaseConfig.apiKey) {
45+
// throw new MissingKeyError('firebaseConfig.apiKey')
46+
// }
2647

2748
export const DOMAIN = IS_LOCAL ? LOCAL_WEB_DOMAIN : ENV_CONFIG.domain
2849
export const BACKEND_DOMAIN = IS_LOCAL ? LOCAL_BACKEND_DOMAIN : ENV_CONFIG.backendDomain

common/src/envs/prod.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ export const PROD_CONFIG: EnvConfig = {
3636
domain: 'compassmeet.com',
3737
backendDomain: 'api.compassmeet.com',
3838
supabaseInstanceId: 'ltzepxnhhnrnvovqblfr',
39-
supabaseAnonKey: process.env.NEXT_PUBLIC_SUPABASE_KEY || '',
40-
supabasePwd: process.env.SUPABASE_DB_PASSWORD || '',
41-
googleApplicationCredentials: process.env.GOOGLE_APPLICATION_CREDENTIALS,
39+
supabaseAnonKey: '',
40+
supabasePwd: '',
41+
googleApplicationCredentials: undefined,
4242
firebaseConfig: {
43-
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY || '',
43+
apiKey: '',
4444
authDomain: "compass-130ba.firebaseapp.com",
4545
projectId: "compass-130ba",
4646
storageBucket: "compass-130ba.firebasestorage.app",
@@ -57,3 +57,12 @@ export const PROD_CONFIG: EnvConfig = {
5757
],
5858
faviconPath: '/favicon.ico',
5959
}
60+
61+
export const refreshConfig = () => {
62+
PROD_CONFIG.supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_KEY || ''
63+
PROD_CONFIG.supabasePwd = process.env.SUPABASE_DB_PASSWORD || ''
64+
PROD_CONFIG.googleApplicationCredentials = process.env.GOOGLE_APPLICATION_CREDENTIALS
65+
PROD_CONFIG.firebaseConfig.apiKey = process.env.NEXT_PUBLIC_FIREBASE_API_KEY || ''
66+
}
67+
68+
refreshConfig()

common/src/secrets.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {SecretManagerServiceClient} from '@google-cloud/secret-manager'
22
import {zip} from 'lodash'
33
import {IS_LOCAL} from "common/envs/constants";
4+
import {refreshConfig} from "common/envs/prod";
45

56
// List of secrets that are available to backend (api, functions, scripts, etc.)
67
// Edit them at:
@@ -44,6 +45,8 @@ export const getSecrets = async (credentials?: any, ...ids: SecretId[]) => {
4445

4546
const secretIds = ids.length > 0 ? ids : secrets
4647

48+
console.log('secretIds', secretIds)
49+
4750
const fullSecretNames = secretIds.map(
4851
(secret: string) =>
4952
`${client.projectPath(projectId)}/secrets/${secret}/versions/latest`
@@ -70,7 +73,9 @@ export const loadSecretsToEnv = async (credentials?: any) => {
7073
for (const [key, value] of Object.entries(allSecrets)) {
7174
if (key && value) {
7275
process.env[key] = value
76+
// console.log(key, value)
7377
}
7478
}
79+
refreshConfig()
7580
}
7681

0 commit comments

Comments
 (0)