Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultConfig ¶
DefaultConfig generates a base configuration file with random credentials and zeroed service configuration for a given type.
It should be used to bootstrap a config.yaml with sensible defaults.
Example:
err := config.DefaultConfig[*MyServiceConfig]("config.yaml")
if err != nil {
log.Fatal(err)
}
func GetServiceConfig ¶
GetServiceConfig returns the previously registered service-specific configuration with type safety using generics.
If the type does not match what was stored, an error is returned.
Example:
cfg, err := config.GetServiceConfig[*MyServiceConfig]()
if err != nil {
log.Fatal(err)
}
func InitServiceConfig ¶
InitServiceConfig loads a configuration file and binds a service-specific struct to the `Service` field in the global config.
It must be called before accessing the service configuration via GetServiceConfig.
If the configuration file does not exist, a default one will be created. Default values from the provided service config struct will be used if corresponding values are not found in the configuration file.
Example:
type MyServiceConfig struct {
Port int
Mode string
}
svc := &MyServiceConfig{
Port: 8080, // Default value
Mode: "dev", // Default value
}
err := config.InitServiceConfig(svc, "config.yaml")
if err != nil {
log.Fatal(err)
}
func LogConfig ¶
func LogConfig()
LogConfig logs the configuration at debug level, masking sensitive values.
This is a convenience method for safely logging the configuration.
Example:
config.LogConfig()
func SetEnvPrefix ¶
func SetEnvPrefix(prefix string)
SetEnvPrefix sets a prefix for environment variables.
Environment variables that match the pattern {prefix}_* will override the corresponding configuration values. The matching is case-insensitive.
For example, if the prefix is "APP", then the environment variable "APP_LOGGER_LOGLEVEL" will override the value of "logger.logLevel" in the configuration file.
Example:
config.SetEnvPrefix("APP")
Types ¶
type Config ¶
type Config struct {
Environment string `yaml:"environment" mapstructure:"environment"`
AppVersion string `yaml:"-" mapstructure:"-"`
ConfigFile string `yaml:"-" mapstructure:"-"`
AppID string `yaml:"appID" mapstructure:"appID"`
AppSecret string `yaml:"appSecret" mapstructure:"appSecret" sensitive:"true"`
Logger Logger `yaml:"logger" mapstructure:"logger"`
Service any `yaml:"service" mapstructure:"service"`
// contains filtered or unexported fields
}
Config represents the global application configuration.
Fields:
- Environment: The current environment (e.g., "dev", "prod").
- AppVersion: The application version.
- AppID: Unique application identifier.
- AppSecret: Secret key for the application (sensitive).
- Logger: Structured logging configuration.
- Service: Service-specific configuration.
func GetBaseConfig ¶
func GetBaseConfig() *Config
GetBaseConfig returns a pointer to the global configuration base object.
This allows access to common fields like AppID, Logger, and AppSecret.
Example:
cfg := config.GetBaseConfig()
fmt.Println("AppID:", cfg.AppID)
func GetSecureCopy ¶
func GetSecureCopy() Config
GetSecureCopy returns a copy of the configuration with sensitive values masked.
This is useful for logging or displaying the configuration without exposing sensitive information like secrets or passwords.
Example:
secureCfg := config.GetSecureCopy()
fmt.Printf("%+v\n", secureCfg)