Documentation
¶
Overview ¶
Package gs_conf provides a layered configuration system for Go-Spring applications. It unifies multiple configuration sources and resolves them into a single immutable property set.
The supported sources include:
- Built-in system defaults (SysConf)
- Local configuration files (e.g., ./conf/app.yaml)
- Remote configuration files (from config servers)
- Dynamically supplied remote properties
- Operating system environment variables
- Command-line arguments
Sources are merged in a defined order so that later sources override properties from earlier ones. This enables flexible deployment patterns: defaults and packaged files supply baseline values, while environment variables and CLI options can easily override them in containerized or cloud-native environments.
The package also supports profile-specific configuration files (e.g., app-dev.yaml) and allows adding extra directories or files at runtime.
Index ¶
Constants ¶
const CommandArgsPrefix = "GS_ARGS_PREFIX"
CommandArgsPrefix defines the environment variable name used to override the default option prefix. This allows users to customize the prefix used for command-line options if needed.
Variables ¶
var SysConf = conf.New()
SysConf is the global built-in configuration instance which usually holds the framework’s own default properties. It is loaded before any environment, file or command-line overrides.
Functions ¶
This section is empty.
Types ¶
type AppConfig ¶
type AppConfig struct { LocalFile *PropertySources // Configuration sources from local files. RemoteFile *PropertySources // Configuration sources from remote files. RemoteProp conf.Properties // Properties fetched from a remote server. Environment *Environment // Environment variables as configuration source. CommandArgs *CommandArgs // Command-line arguments as configuration source. }
AppConfig represents a layered configuration for the application runtime. The layers, in their merge order, typically include:
- System defaults (SysConf)
- Local configuration files
- Remote configuration files
- Dynamically supplied remote properties
- Environment variables
- Command-line arguments
Layers appearing later in the list override earlier ones when keys conflict.
func NewAppConfig ¶
func NewAppConfig() *AppConfig
NewAppConfig creates a new instance of AppConfig.
type BootConfig ¶
type BootConfig struct { LocalFile *PropertySources // Configuration sources from local files. Environment *Environment // Environment variables as configuration source. CommandArgs *CommandArgs // Command-line arguments as configuration source. }
BootConfig represents a layered configuration used during application boot. It typically includes only system, local file, environment and command-line sources — no remote sources.
func NewBootConfig ¶
func NewBootConfig() *BootConfig
NewBootConfig creates a new instance of BootConfig.
func (*BootConfig) Refresh ¶
func (c *BootConfig) Refresh() (conf.Properties, error)
Refresh merges all layers of configurations into a read-only properties.
type CommandArgs ¶
type CommandArgs struct{}
CommandArgs represents a structure for handling command-line parameters.
func NewCommandArgs ¶
func NewCommandArgs() *CommandArgs
NewCommandArgs creates and returns a new CommandArgs instance.
func (*CommandArgs) CopyTo ¶
func (c *CommandArgs) CopyTo(p *conf.MutableProperties) error
CopyTo extracts command-line parameters and stores them as key-value pairs. Supported formats include:
- <prefix> key=value
- <prefix> key (defaults to "true")
- <prefix>key=value (inline form)
The default prefix is "-D", which can be overridden by the environment variable `GS_ARGS_PREFIX`.
type ConfigType ¶
type ConfigType string
ConfigType defines the type of configuration: local or remote.
const ( ConfigTypeLocal ConfigType = "local" ConfigTypeRemote ConfigType = "remote" )
type Environment ¶
type Environment struct{}
Environment represents the environment configuration.
func NewEnvironment ¶
func NewEnvironment() *Environment
NewEnvironment initializes a new instance of Environment.
func (*Environment) CopyTo ¶
func (c *Environment) CopyTo(p *conf.MutableProperties) error
CopyTo adds environment variables. Variables with the prefix "GS_" are transformed:
- Prefix "GS_" is removed.
- Remaining underscores '_' are replaced by dots '.'.
- Keys are converted to lowercase.
All other variables are stored as-is.
type NamedPropertyCopier ¶
type NamedPropertyCopier struct { PropertyCopier Name string }
NamedPropertyCopier is a wrapper around PropertyCopier that also carries a human-readable Name. The Name is used for logging, debugging or error reporting when merging multiple sources.
func NewNamedPropertyCopier ¶
func NewNamedPropertyCopier(name string, p PropertyCopier) *NamedPropertyCopier
NewNamedPropertyCopier creates a new instance of NamedPropertyCopier.
func (*NamedPropertyCopier) CopyTo ¶
func (c *NamedPropertyCopier) CopyTo(out *conf.MutableProperties) error
type PropertyCopier ¶
type PropertyCopier interface {
CopyTo(out *conf.MutableProperties) error
}
PropertyCopier defines the interface for any configuration source that can copy its key-value pairs into a target conf.MutableProperties.
type PropertySources ¶
type PropertySources struct {
// contains filtered or unexported fields
}
PropertySources represents a collection of configuration files associated with a particular configuration type and logical name. It supports both default directories and additional user-supplied directories or files.
func NewPropertySources ¶
func NewPropertySources(configType ConfigType, configName string) *PropertySources
NewPropertySources creates a new instance of PropertySources.
func (*PropertySources) AddDir ¶
func (p *PropertySources) AddDir(dirs ...string)
AddDir registers one or more additional directories to search for configuration files. Non-existent directories are silently ignored, but if the path exists and is not a directory, it panics.
func (*PropertySources) AddFile ¶
func (p *PropertySources) AddFile(files ...string)
AddFile registers one or more additional configuration files. Non-existent files are silently ignored, but if the path exists and is a directory, it panics.
func (*PropertySources) Reset ¶
func (p *PropertySources) Reset()
Reset clears all previously added extra directories and files.
type SysConfig ¶ added in v1.2.2
type SysConfig struct { Environment *Environment // Environment variables as configuration source. CommandArgs *CommandArgs // Command-line arguments as configuration source. }
SysConfig represents the init-level configuration layer composed of environment variables and command-line arguments.