Skip to content

Commit 45009f7

Browse files
authored
chore: split configs (#1902)
* fix: make configs separate * fix: imports
1 parent 7ab6cc3 commit 45009f7

35 files changed

Lines changed: 207 additions & 222 deletions

src/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'reflect-metadata';
55
import type { FastifyInstance } from 'fastify';
66
import fp from 'fastify-plugin';
77

8+
import { REDIS_CONNECTION } from './config/redis';
89
import { registerDependencies } from './di/container';
910
import databasePlugin from './plugins/database';
1011
import metaPlugin from './plugins/meta';
@@ -17,7 +18,6 @@ import { maintenancePlugin } from './services/maintenance/maintenance.controller
1718
import MemberServiceApi from './services/member';
1819
import tagPlugin from './services/tag/tag.controller';
1920
import websocketsPlugin from './services/websockets/websocket.controller';
20-
import { REDIS_CONNECTION } from './utils/config';
2121

2222
export default async function (instance: FastifyInstance): Promise<void> {
2323
await instance

src/config/env.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { config } from 'dotenv';
2+
3+
const Environment = {
4+
production: 'production',
5+
staging: 'staging',
6+
development: 'development',
7+
test: 'test',
8+
} as const;
9+
10+
export const NODE_ENV: string | undefined = process.env.NODE_ENV;
11+
12+
const once = (fn) => {
13+
let called = false;
14+
return function (...args) {
15+
if (called) return;
16+
called = true;
17+
return fn.apply(this, args);
18+
};
19+
};
20+
21+
/**
22+
* Pull env vars from file depending on `NODE_ENV` variable
23+
*/
24+
export const getEnv = once(() => {
25+
switch (NODE_ENV) {
26+
case Environment.production:
27+
config({ path: '.env.production', override: true });
28+
return Environment.production;
29+
case Environment.staging:
30+
config({ path: '.env.staging', override: true });
31+
return Environment.staging;
32+
case Environment.test:
33+
config({ path: '.env.test', override: true });
34+
return Environment.test;
35+
default:
36+
config({ path: '.env.development', override: true });
37+
return Environment.development;
38+
}
39+
});
40+
41+
export const PROD = NODE_ENV === Environment.production;
42+
export const STAGING = NODE_ENV === Environment.staging;
43+
export const DEV = NODE_ENV === Environment.development;
44+
export const TEST = NODE_ENV === Environment.test;

src/config/helpers.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export class ExpectedEnvVariable extends Error {
2+
constructor(envVarName: string) {
3+
super(`Expected to find env variable "${envVarName}" but it was undefined.`);
4+
this.name = 'MissingEnvVar';
5+
}
6+
}
7+
8+
/**
9+
*
10+
* @param name the name of the env var
11+
* @throws an error if the var is not defined
12+
* @returns the env var value if it is defined
13+
*/
14+
export function requiredEnvVar(name: string) {
15+
const varValue = process.env[name];
16+
if (varValue === undefined) {
17+
throw new ExpectedEnvVariable(name);
18+
}
19+
return varValue;
20+
}

src/config/mailer.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { getEnv } from './env';
2+
import { requiredEnvVar } from './helpers';
3+
4+
getEnv();
5+
6+
export const MAILER_CONNECTION = requiredEnvVar('MAILER_CONNECTION');
7+
export const MAILER_CONFIG_FROM_EMAIL =
8+
process.env.MAILER_CONFIG_FROM_EMAIL ?? 'no-reply@graasp.org';

src/config/redis.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { getEnv } from './env';
2+
import { requiredEnvVar } from './helpers';
3+
4+
getEnv();
5+
6+
export const REDIS_CONNECTION = requiredEnvVar('REDIS_CONNECTION');

src/config/secrets.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { getEnv } from './env';
2+
import { requiredEnvVar } from './helpers';
3+
4+
// ensure env is setup
5+
getEnv();
6+
7+
/**
8+
* Session cookie key
9+
*/
10+
export const SECURE_SESSION_SECRET_KEY = requiredEnvVar('SECURE_SESSION_SECRET_KEY');
11+
export const SECURE_SESSION_EXPIRATION_IN_SECONDS = 604800; // 7days
12+
export const MAX_SECURE_SESSION_EXPIRATION_IN_SECONDS = 15552000; // 6 * 30days -> 6 months
13+
14+
/**
15+
* JWT
16+
*/
17+
export const JWT_SECRET = requiredEnvVar('JWT_SECRET');
18+
/** Register token expiration, in minutes */
19+
export const REGISTER_TOKEN_EXPIRATION_IN_MINUTES = 60;
20+
/** Login token expiration, in minutes */
21+
export const LOGIN_TOKEN_EXPIRATION_IN_MINUTES = 30;
22+
23+
/** Password reset token Secret */
24+
export const PASSWORD_RESET_JWT_SECRET: string = requiredEnvVar('PASSWORD_RESET_JWT_SECRET');
25+
/** Password reset token expiration, in minutes */
26+
export const PASSWORD_RESET_JWT_EXPIRATION_IN_MINUTES = 1440; // 24 hours
27+
28+
/** Email change token Secret */
29+
export const EMAIL_CHANGE_JWT_SECRET: string = requiredEnvVar('EMAIL_CHANGE_JWT_SECRET');
30+
/** Email change token expiration, in minutes */
31+
export const EMAIL_CHANGE_JWT_EXPIRATION_IN_MINUTES = 1440; // 24 hours
32+
33+
/** Graasp apps authentication */
34+
export const APPS_JWT_SECRET = requiredEnvVar('APPS_JWT_SECRET');
35+
36+
// TODO: remove mobile auth variables as it is deprecated and not supported anymore
37+
/**
38+
* Mobile auth
39+
*/
40+
export const AUTH_TOKEN_JWT_SECRET = requiredEnvVar('AUTH_TOKEN_JWT_SECRET');
41+
/** Auth token expiration, in minutes */
42+
export const AUTH_TOKEN_EXPIRATION_IN_MINUTES = 10080; // 7 days
43+
44+
export const REFRESH_TOKEN_JWT_SECRET = requiredEnvVar('REFRESH_TOKEN_JWT_SECRET');
45+
/** Refresh token expiration, in minutes */
46+
export const REFRESH_TOKEN_EXPIRATION_IN_MINUTES = 86400; // 60 days

src/di/container.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import type { FastifyInstance } from 'fastify';
55

66
import Etherpad from '@graasp/etherpad-api';
77

8+
import { MAILER_CONFIG_FROM_EMAIL, MAILER_CONNECTION } from '../config/mailer';
9+
import { REDIS_CONNECTION } from '../config/redis';
810
import { CRON_3AM_MONDAY, JobServiceBuilder } from '../jobs';
911
import { BaseLogger } from '../logger';
1012
import { MailerService } from '../plugins/mailer/mailer.service';
@@ -21,11 +23,8 @@ import {
2123
FILE_STORAGE_TYPE,
2224
GEOLOCATION_API_KEY,
2325
IMAGE_CLASSIFIER_API,
24-
MAILER_CONFIG_FROM_EMAIL,
25-
MAILER_CONNECTION,
2626
MEILISEARCH_MASTER_KEY,
2727
MEILISEARCH_URL,
28-
REDIS_CONNECTION,
2928
S3_FILE_ITEM_PLUGIN_OPTIONS,
3029
} from '../utils/config';
3130
import {

src/fastify.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,11 @@ import type { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';
33
import { fastify } from 'fastify';
44

55
import registerAppPlugins from './app';
6+
import { DEV, NODE_ENV, PROD } from './config/env';
67
import { client } from './drizzle/db';
78
import ajvFormats from './schemas/ajvFormats';
89
import { initSentry } from './sentry';
9-
import {
10-
APP_VERSION,
11-
CORS_ORIGIN_REGEX,
12-
DEV,
13-
ENVIRONMENT,
14-
HOST_LISTEN_ADDRESS,
15-
PORT,
16-
PROD,
17-
} from './utils/config';
10+
import { APP_VERSION, CORS_ORIGIN_REGEX, HOST_LISTEN_ADDRESS, PORT } from './utils/config';
1811
import { GREETING } from './utils/constants';
1912

2013
export const instance = fastify({
@@ -65,7 +58,7 @@ const start = async () => {
6558

6659
try {
6760
await instance.listen({ port: PORT, host: HOST_LISTEN_ADDRESS });
68-
instance.log.info('App is running version %s in %s mode', APP_VERSION, ENVIRONMENT);
61+
instance.log.info('App is running version %s in %s mode', APP_VERSION, NODE_ENV);
6962
if (DEV) {
7063
// greet the world
7164
// eslint-disable-next-line no-console

src/jobs.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { type ConnectionOptions, Queue, Worker } from 'bullmq';
22

3+
import { REDIS_CONNECTION } from './config/redis';
34
import { BaseLogger } from './logger';
4-
import { JOB_SCHEDULING, REDIS_CONNECTION } from './utils/config';
5+
import { JOB_SCHEDULING } from './utils/config';
56

67
const connection: ConnectionOptions = {
78
url: REDIS_CONNECTION,

src/sentry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import '@sentry/tracing';
33

44
import type { FastifyInstance } from 'fastify';
55

6+
import { NODE_ENV } from './config/env';
67
import {
78
APP_VERSION,
8-
NODE_ENV,
99
SENTRY_DSN,
1010
SENTRY_ENABLE_PERFORMANCE,
1111
SENTRY_ENABLE_PROFILING,

0 commit comments

Comments
 (0)