System Architecture
Core Features
Data Management
Frontend Components
Extensibility
The following files were used as context for generating this wiki page:
This page details the configuration management within the project, focusing on how environment variables are loaded and how application-specific settings for services like AWS and "Better Auth" are defined and validated. The system leverages dotenv for loading environment variables and zod for robust schema validation, ensuring that all necessary configurations are present and correctly formatted at application startup.
The configuration architecture aims to provide a clear, centralized, and validated approach to managing application settings, reducing runtime errors caused by missing or malformed environment variables.
The project utilizes a dedicated module, load-env.ts, to manage the loading of environment variables from .env files. This module ensures that environment variables are loaded early in the application lifecycle, supporting various deployment and development environments. It searches for .env files in predefined locations to maximize flexibility.
The loadEnv function attempts to load the file from several common paths:
.envdist/src/.env).src/.env).process.cwd()/.env).Once an .env file is found, its variables are loaded and can optionally override existing environment variables. The ensureEnvLoaded function provides a mechanism to guarantee that environment variables have been processed, preventing redundant loading.
// apps/api/src/config/load-env.ts
const searchPaths = [
// When compiled to dist/src (Nest build output)
path.join(__dirname, '..', '..', '.env'),
// When running with ts-node directly from src
path.join(__dirname, '..', '.env'),
// Fallback to current working directory
path.join(process.cwd(), '.env'),
];
function loadEnv(): void {
for (const envPath of searchPaths) {
if (existsSync(envPath)) {
config({ path: envPath, override: true });
envLoaded = true;
return;
}
}
envLoaded = true;
}Sources: apps/api/src/config/load-env.ts:1-25
The project uses @nestjs/config's registerAs function in conjunction with zod schemas to define and validate specific configuration objects. This approach provides type safety and ensures that critical application settings are correctly structured and present.
The AWS configuration (aws.config.ts) defines the necessary parameters for interacting with Amazon Web Services. It includes settings for region, access keys, and S3 bucket details.
The AWS configuration is validated at application startup using a zod schema. If any required environment variables are missing or invalid, the application will throw an error and fail to start, preventing potential runtime issues.
The "Better Auth" configuration (better-auth.config.ts) specifies the URL for an external authentication service.
The BETTER_AUTH_URL environment variable is required. If it is not provided or is not a valid URL, the application will fail to start.
For local development, environment variables are typically managed through .env files. The project provides example .env.example files that should be copied and filled with actual credentials.
Copy the example environment files for each application and package.
cp apps/app/.env.example apps/app/.env
cp apps/portal/.env.example apps/portal/.env
cp packages/db/.env.example packages/db/.envPopulate the newly created .env files with your specific credentials and settings.
Ensure all required variables are present. The README.md specifically highlights the need for NEXT_PUBLIC_PORTAL_URL and REVALIDATION_SECRET in comp/apps/app/.env which might be missing from .env.example.
| Variable | Description