Configuration Management
The dagger.json
file is responsible for configuring the behavior of the Dagger application. It provides a centralized location to manage various settings, including application-specific options, development environment variables, and deployment parameters.
Configuration Options
dagger.json
Structure
{
"name": "your-app-name",
"version": "1.0.0",
"description": "A description of your application",
"author": "Your Name",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/your-username/your-repo.git"
},
"dependencies": {
"dependency1": "^1.0.0",
"dependency2": "~2.0.0"
},
"scripts": {
"start": "npm run build && node index.js",
"build": "webpack --mode production",
"dev": "webpack --mode development --watch"
},
"config": {
"port": 3000,
"database": {
"host": "localhost",
"port": 27017,
"name": "your-database-name"
},
"logging": {
"level": "info"
}
}
}
This structure provides a clear and organized way to manage application configurations.
config
Object
The config
object is a top-level container that encapsulates all application-specific settings.
port
The port
option defines the port number on which the application listens for incoming connections. Default value: 3000
.
Example:
{
"config": {
"port": 8080
}
}
This sets the application to listen on port 8080
.
database
The database
object configures the application’s database connection.
host
The host
property specifies the database server hostname or IP address. Default value: localhost
.
Example:
{
"config": {
"database": {
"host": "127.0.0.1"
}
}
}
This sets the database host to the local machine’s IP address.
port
The port
property defines the database server’s port number. Default value: 27017
.
Example:
{
"config": {
"database": {
"port": 27018
}
}
}
This sets the database port to 27018
.
name
The name
property defines the name of the database to connect to.
Example:
{
"config": {
"database": {
"name": "my-application-db"
}
}
}
This sets the database name to my-application-db
.
logging
The logging
object controls the application’s logging behavior.
level
The level
property specifies the logging verbosity level. Possible values include: debug
, info
, warn
, error
, fatal
. Default value: info
.
Example:
{
"config": {
"logging": {
"level": "debug"
}
}
}
This enables debug-level logging.
Configuration Loading
The Dagger application loads configuration from the dagger.json
file during startup. This file should be placed at the root of the project directory. The configuration is loaded using a dedicated configuration manager that provides the necessary functionality for accessing and manipulating the settings.
Configuration Environment Variables
The dagger.json
file can also be extended with environment variables to provide flexibility and dynamic configuration. These variables can be defined in the .env
file located in the root directory of the project.
Example:
DATABASE_HOST=my-db-server
DATABASE_PORT=5432
This sets the DATABASE_HOST
and DATABASE_PORT
environment variables, which can be used to override the default database settings defined in the dagger.json
file. The dotenv
package is used to load and parse environment variables.
Configuration Management in Code
The configuration manager exposes methods for accessing and manipulating the loaded configuration:
getConfig(): Object
: Retrieves the entire configuration object.getValue(key: string): any
: Retrieves the value associated with a specific configuration key.setValue(key: string, value: any): void
: Sets the value for a given configuration key.hasValue(key: string): boolean
: Checks if a given configuration key exists.
Example:
// Get the current database hostname from configuration.
const databaseHost = configManager.getValue('database.host');
// Set the logging level to 'debug'.
configManager.setValue('logging.level', 'debug');
These methods allow the application to dynamically access and modify configuration settings based on runtime conditions or user preferences.