Skip to content

Introduction

econfig is the configuration management module of ee-go, responsible for loading and providing application configuration data. It supports layered configuration loading (reading local files in development, embedded or external files in production), automatically merges environment configuration into default configuration, and uses Viper as the configuration container underneath. All configuration values are stored and accessed as map[string]any.

Import

go
import "github.com/wallace5303/ee-go/econfig"

API

Init()

Description: Initialize the configuration system, load default and environment configuration and merge them. Loading strategy: in development mode, it prioritizes reading config.default.json and config.local.json from the project directory; in production mode, it prioritizes reading config.default.json and config.prod.json from the embedded static filesystem, falling back to external files if embedded files don't exist. If configuration files don't exist, it throws an exception and exits. The merged configuration is stored in the Vip global variable. This function is called internally by eboot.New(), users typically don't need to call it manually.

Parameters: None

Returns: None

Example:

go
// Normally called automatically by eboot.New()
econfig.Init()

See also: Vip


Get(key)

Description: Get a configuration value by key name. Key names are case-insensitive (Viper feature). If the key doesn't exist, returns nil.

Parameters:

ParameterTypeRequiredDescription
keystringYesConfiguration key name, case-insensitive

Returns: any — configuration value for the key, returns nil if key doesn't exist

Example:

go
port := econfig.Get("http.port")
enable := econfig.Get("http.enable")
fmt.Printf("HTTP port: %v, enable: %v\n", port, enable)

GetAll()

Description: Return a complete map of all configuration items.

Parameters: None

Returns: map[string]any — map containing all configuration key-value pairs

Example:

go
allCfg := econfig.GetAll()
for k, v := range allCfg {
    fmt.Printf("%s = %v\n", k, v)
}

GetLogger()

Description: Get the business log configuration item. Corresponds to the logger key in the configuration file. If the configuration item doesn't exist or the type doesn't match, it throws an exception and exits.

Parameters: None

Returns: map[string]any — log configuration map, containing log level, output path, and other settings

Example:

go
logCfg := econfig.GetLogger()
logLevel := logCfg["level"]
logDir := logCfg["dir"]

See also: GetCoreLogger()


GetCoreLogger()

Description: Get the core log configuration item. Corresponds to the core_logger key in the configuration file. Core logs are used to record framework internal logs, separate from business logs. If the configuration item doesn't exist or the type doesn't match, it throws an exception and exits.

Parameters: None

Returns: map[string]any — core log configuration map

Example:

go
coreLogCfg := econfig.GetCoreLogger()
coreLevel := coreLogCfg["level"]

See also: GetLogger()


GetHttp()

Description: Get the HTTP service configuration item. Corresponds to the http key in the configuration file, containing port, enable status, SSL, and other settings. If the configuration item doesn't exist or the type doesn't match, it throws an exception and exits.

Parameters: None

Returns: map[string]any — HTTP configuration map, typical fields include enable, port, ssl, etc.

Example:

go
httpCfg := econfig.GetHttp()
if httpCfg["enable"] == true {
    port := httpCfg["port"]
    fmt.Printf("HTTP server on port: %v\n", port)
}

GetStatic()

Description: Get the static resource configuration item. Corresponds to the static key in the configuration file, controlling whether to enable the embedded static filesystem. If the configuration item doesn't exist or the type doesn't match, it throws an exception and exits.

Parameters: None

Returns: map[string]any — static resource configuration map, typical fields include enable, etc.

Example:

go
staticCfg := econfig.GetStatic()
if staticCfg["enable"] == true {
    // Use embedded filesystem to read resources
} else {
    // Use external file path to read resources
}

Vip

Description: Global Viper configuration instance, available after Init() completes. Provides the full Viper API capabilities (such as Set, IsSet, Sub, etc.), can be used for advanced configuration operations.

Type: *viper.Viper

Example:

go
// Check if a key exists
if econfig.Vip.IsSet("custom.key") {
    val := econfig.Vip.Get("custom.key")
}

// Dynamically set configuration value
econfig.Vip.Set("app.name", "my-app")