Configuration
Relevant source files
The following files were used as context for generating this wiki page:
Configuration in the Ever Demand platform is managed on a per-package basis within its Lerna monorepo structure. This decentralized approach allows each application (backend, admin UI, shop UIs) to have its own tailored settings. The primary mechanism for configuration is the use of .env files at the package level, which are processed by TypeScript scripts to generate environment-specific configuration files or are loaded directly at runtime.
This system provides flexibility, allowing developers to run and build different parts of the platform with distinct settings, such as API endpoints and feature flags. The root package.json orchestrates the execution of these configuration and build scripts for each workspace.
Configuration Architecture
The platform's configuration is not centralized. Instead, each package in the packages/ directory is responsible for its own configuration. The general flow involves using a .env file to store environment variables, which are then used by scripts to prepare the application for a specific environment (e.g., development, production).
High-Level Configuration Flow
The diagram below illustrates the general process of how environment variables are sourced from a .env file and injected into an application at build or runtime.
This diagram shows two primary patterns: build-time configuration generation for frontend apps and runtime configuration loading for the backend.
Sources: packages/admin-web-angular/scripts/configure.ts, packages/core/src/app.module.ts
Monorepo Scripts
The root package.json file contains scripts that use Lerna to manage the various packages. Scripts like build:all and run:* are essential for orchestrating the build and execution of the entire platform, which implicitly triggers the configuration processes within each package.
| Script | Description |
|---|---|
bootstrap | Installs all dependencies for all packages using Lerna. |
build:all | Builds all packages in the correct order, including common modules, server, and all frontend applications. |
run:server | Builds and starts the backend API server. |
run:admin | Starts the Angular admin web application. |
run:shopweb | Starts the Angular shop web application. |
Sources: package.json:23-45
Backend Configuration (@ever-platform/core)
The backend, built with NestJS, loads its configuration at runtime directly from environment variables.
Sources: packages/core/src/app.module.ts
Environment Loading
The backend uses dotenv to load variables from a .env file into process.env. The envalid library is used to validate and type-cast these variables, ensuring that the application starts with a valid configuration. This is evident from the import of env from ./env in app.module.ts.
Database Configuration
The primary database is MongoDB, with configuration for TypeORM also present for other potential database integrations. The connection settings are configured within ApplicationModule.
Key database configuration details include:
- Connection URI: Defined by
env.DB_URI. - SSL Support: SSL is enabled if
process.env.DB_SSL_MODEis set and not'false'. The CA certificate is read fromprocess.env.DB_CA_CERT, decoded from base64, and written to a temporary file. - Connection Pooling: A pool size is set, although the code shows it commented out, a default value of 50 is noted.
- Entities: TypeORM entities are gathered from
ServicesApp.getEntities()and registered with the default connection.
The following snippet from app.module.ts shows the TypeORM connection setup:
// packages/core/src/app.module.ts:111-131
const connectionSettings: TypeOrmModuleOptions = {
// Note: do not change this connection name, it should be default one!
// TODO: put this into settings (it's mongo only during testing of TypeORM integration!)
type: 'mongodb',
url: env.DB_URI,
ssl: isSSL,
sslCA: isSSL ? [sslCertPath] : undefined,
host: process.env.DB_HOST || 'localhost',
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME || 'ever_development',
port: process.env.DB_PORT ? parseInt(process.env.DB_PORT, 10) : 27017,
entities,
synchronize: true,
useNewUrlParser: true,
// autoReconnect: true,
// reconnectTries: Number.MAX_VALUE,
// poolSize: poolSize,
connectTimeoutMS: connectTimeoutMS,
logging: true,
logger: 'file', // Removes console logging, instead logs all queries in a file ormlogs.log
useUnifiedTopology: true,
};
Sources: packages/core/src/app.module.ts:83-132
Frontend Configuration
All frontend applications (Angular, Expo) follow a similar pattern for handling configuration. A script, typically scripts/configure.ts, is executed before the application is built or served. This script reads environment variables and generates TypeScript files (src/environments/environment.ts and src/environments/environment.prod.ts) that are then consumed by the application.
Configuration Generation Flow
This sequence diagram details the process for generating frontend configuration files.
Angular Applications (Admin & Shop)
Both the Admin and Shop Angular applications use a scripts/configure.ts file to generate their respective environment.ts files from .env variables. The script uses yargs to check for the --environment=prod flag to determine which file content to generate.
Key Configuration Variables
The table below lists some of the common configuration variables generated for the Angular frontends.
| Variable | Description | Example Value |
|---|---|---|
production | Boolean flag indicating if the app is in production mode. | true / false |
GQL_ENDPOINT | The URL for the GraphQL API endpoint. | 'http://localhost:8443/graphql' |
GQL_SUBSCRIPTIONS_ENDPOINT | The WebSocket URL for GraphQL subscriptions. | 'ws://localhost:2086/subscriptions' |
GOOGLE_MAPS_API_KEY | API key for Google Maps services. | 'AIzaSy...' |
DEFAULT_LATITUDE | Default latitude for map views. | 42.6459136 |
DEFAULT_LONGITUDE | Default longitude for map views. | 23.3332736 |
SETTINGS_MAINTENANCE_API_URL | URL for the maintenance microservice. | 'https://maintenance.ever.co/status' |
Sources: packages/admin-web-angular/scripts/configure.ts:20-80, packages/shop-web-angular/scripts/configure.ts:25-50
Build-Time Environment Switching
The angular.json file for each project defines fileReplacements to ensure the correct environment file is used during a production build.
// packages/admin-web-angular/angular.json:62-67
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
]
Sources: packages/admin-web-angular/angular.json:62-67
Webpack Configuration
The shop-web-angular package uses a custom webpack configuration. The webpack.common.js file loads environment-specific JSON configuration (config.prod.json or config.dev.json) and uses Webpack's DefinePlugin to make variables available globally within the application bundle.
Sources: packages/shop-web-angular/config/webpack.common.js:33-35, 137-147
Mobile Application (Expo)
The Expo mobile app also follows the same configuration pattern, using a scripts/configure.ts script to generate an environment.ts file. A notable configuration detail is the advice to use 10.0.2.2 instead of localhost for the Android Virtual Device to connect to the local development server.
// packages/shop-mobile-expo/scripts/configure.ts:121-125
/**
* Contain app endpoints
*
* _🚧 warning: if you're using a AVD (Android Virtual Device)_
* _and localhost doesn't works, replace it with **10.0.2.2**_
*/
Sources: packages/shop-mobile-expo/scripts/configure.ts:121-125, packages/shop-mobile-expo/README.md:46-48