Overview
TailStack uses environment variables for configuration, following the 12-factor app methodology. This approach keeps secrets out of your code and makes it easy to deploy across different environments.Configuration Structure
Configuration is centralized in theconfig/ directory:
Main Configuration
The main configuration file loads environment variables:packages/core/source/Server/src/config/index.ts
Configuration Values
| Key | Environment Variable | Default | Description |
|---|---|---|---|
port | PORT | 5000 | Server port number |
nodeEnv | NODE_ENV | development | Environment mode |
corsOrigin | CORS_ORIGIN | http://localhost:5173 | Allowed CORS origin |
workers | WORKERS | 0 (auto) | Number of cluster workers |
When
workers is set to 0, TailStack automatically uses all available CPU cores via availableParallelism().Environment Variables
Setting Up .env Files
Create environment-specific.env files:
- .env.development
- .env.production
- .env.test
Loading Environment Variables
Thedotenv package loads variables from .env files:
Feature-Specific Configuration
Separate configuration concerns by feature:packages/core/source/Server/src/config/weather.ts
- Keeps related configuration together
- Makes it easy to find and update settings
- Allows importing only what you need
Using Feature Config
src/services/weather.service.ts
Configuration Constants
For values that don’t change between environments, use constants:packages/node/src/constant/config.ts
- Type safety
- Autocomplete in your IDE
- Single source of truth for default values
Validation
Validate configuration at startup to fail fast:src/config/index.ts
Type-Safe Configuration
For better type safety, create a configuration schema:src/config/schema.ts
Configuration Best Practices
Use environment variables for secrets
Use environment variables for secrets
Never hardcode API keys, database passwords, or other secrets. Use environment variables and keep them out of version control.
Provide sensible defaults
Provide sensible defaults
Use the
|| operator to provide default values for non-critical configuration.Validate early
Validate early
Validate required configuration at application startup to catch issues before they cause problems in production.
Separate by environment
Separate by environment
Use different
.env files for development, staging, and production environments.Document your config
Document your config
Provide a
.env.example file showing all available configuration options with example values.Use type safety
Use type safety
TypeScript interfaces help catch configuration errors at compile time.
Example .env.example
Provide a template for your team:.env.example
Environment-Specific Scripts
Use different scripts for different environments:package.json
Accessing Configuration
Import and use configuration throughout your app:Next Steps
Server Setup
See how configuration is used during server initialization
Clustering
Learn how the WORKERS configuration affects clustering
Middleware
Understand how CORS_ORIGIN is used in CORS middleware