Documentation
¶
Overview ¶
Package config provides the application configuration structures and utilities for loading, validating, and managing configuration settings. The package supports YAML-based configuration for various components such as logging, transports (e.g., Dummy, UDS, QUIC), MDBX nodes, and pprof profiling.
This package includes:
**Logger Configuration**: Defines the settings for application logging, including log levels and environment-based logging adjustments.
**Transport Configuration**: Manages different transport protocols and their settings. This includes DummyTransport, UdsTransport, and QuicTransport, each with its own specific configuration fields like IP address, port, and TLS settings.
**MDBX Configuration**: Provides configuration for MDBX database nodes, including paths, file size limits, and file permissions.
**pprof Configuration**: Configures pprof profiling for performance analysis and debugging, allowing specific services to enable pprof with an address to bind to.
Example usage:
// Load the configuration from a file config, err := config.LoadConfig("/path/to/config.yaml") if err != nil { log.Fatalf("Failed to load configuration: %v", err) } // Access the logger configuration if config.Logger.Enabled { setupLogger(config.Logger) } // Get a specific transport configuration by type transport := config.GetTransportByType(types.UDSTransportType) if transport == nil { log.Fatalf("UDS transport not found") } // Access MDBX configuration for a specific node mdbxNode := config.GetMdbxNodeByName("node1") if mdbxNode == nil { log.Fatalf("MDBX node not found") } // Access pprof configuration for a specific service pprofConfig, err := config.GetPprofByServiceTag("serviceA") if err != nil { log.Fatalf("Failed to get pprof config: %v", err) }
The package also provides methods to validate configuration after loading and ensures that settings are properly configured before the application starts.
Configuration Structure: The configuration is stored in a YAML format and is loaded into the Config struct to allow easy access to different settings.
Example YAML Configuration:
logger: enabled: true environment: "production" level: "info" transports: - type: "dummy" enabled: true ipv4: "127.0.0.1" port: 8080 tls: insecure: true mdbx: enabled: true nodes: - name: "node1" path: "/data/mdbx" maxReaders: 100 maxSize: 1073741824 minSize: 10485760 growthStep: 1048576 filePermissions: 0600 pprof: - enabled: true name: "serviceA" addr: "localhost:6060"
The config package centralizes application settings, providing a clean, consistent way to manage configurations across different components.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { // Logger holds the configuration for the logging system, including log level and environment. Logger Logger `yaml:"logger"` // Transports is a list of various transport configurations (e.g., Dummy, UDS, QUIC). // Each transport has its own specific configuration settings. Transports []Transport `yaml:"transports"` // Mdbx contains the configuration for MDBX database nodes, including paths, sizes, and permissions. Mdbx Mdbx `yaml:"mdbx"` // Pprof is a list of pprof profiling configurations, each tied to a specific service or subsystem. Pprof []Pprof `yaml:"pprof"` }
Config represents the overall application configuration, which includes logging, transports, MDBX nodes, and pprof profiling options. This struct aggregates all major configuration sections for easy management and access throughout the application.
func LoadConfig ¶
LoadConfig loads the configuration from a YAML file into the Config struct. This function reads the specified YAML configuration file and unmarshals it into a Config struct, enabling structured access to configuration settings.
Example usage:
config, err := LoadConfig("/path/to/config.yaml") if err != nil { log.Fatalf("Failed to load config: %v", err) }
Parameters:
filename (string): The path to the YAML configuration file.
Returns:
*Config: Returns a pointer to the Config struct containing the parsed configuration. error: Returns an error if reading or unmarshaling the YAML file fails.
func (Config) GetMdbxNodeByName ¶
GetMdbxNodeByName searches for an MDBX node by its name and returns the corresponding MdbxNode configuration. This method is useful for discovering specific node configurations based on the node's name.
Example usage:
nodeConfig := config.GetMdbxNodeByName("node1") if nodeConfig == nil { log.Fatalf("MDBX node not found") }
Parameters:
name (string): The name of the MDBX node to search for.
Returns:
*MdbxNode: Returns a pointer to the MdbxNode if found, or nil if no node matches the provided name.
func (Config) GetPprofByServiceTag ¶
GetPprofByServiceTag searches through the list of pprof configurations to find the configuration corresponding to the specified service tag (name). This method helps discover pprof settings for a specific service based on its name.
Example usage:
pprofConfig, err := config.GetPprofByServiceTag("my-service") if err != nil { log.Fatalf("Failed to get pprof config: %v", err) }
Parameters:
service (string): The service name (tag) to search for in the pprof configurations.
Returns:
*Pprof: Returns a pointer to the Pprof configuration if found. error: Returns an error if no matching service tag is found.
func (Config) GetTransportByType ¶
func (c Config) GetTransportByType(transportType types.TransportType) *Transport
GetTransportByType retrieves a specific transport configuration based on its type (e.g., UDS, Dummy). This method allows you to access a particular transport configuration when multiple transports are defined.
Example usage:
transport := config.GetTransportByType(types.DummyTransportType) if transport == nil { log.Fatalf("Transport not found") }
Parameters:
transportType (types.TransportType): The type of transport to search for.
Returns:
*Transport: Returns a pointer to the matching transport configuration if found, or nil if no match is found.
func (Config) Validate ¶
Validate checks the integrity of the loaded configuration. Currently, it returns nil, but you can extend it to perform validation on the various configuration fields to ensure they are set correctly.
Example usage:
if err := config.Validate(); err != nil { log.Fatalf("Invalid configuration: %v", err) }
Returns:
error: Returns nil if the configuration is valid, or an error if validation fails.
type DTLS ¶
type DTLS struct { // Cert is the path to the certificate file used for DTLS encryption. Cert string `yaml:"cert" json:"cert" mapstructure:"cert"` // Key is the path to the private key file used for DTLS encryption. Key string `yaml:"key" json:"key" mapstructure:"key"` // RootCA is the path to the root CA file used to validate the peer's certificate. RootCA string `yaml:"root_ca" json:"root_ca" mapstructure:"root_ca"` // Insecure determines if the DTLS should skip certificate verification. Insecure bool `yaml:"insecure" json:"insecure" mapstructure:"insecure"` }
DTLS represents the DTLS configuration used by the UDP transport. It is similar to TLS, but designed for datagram-based communication.
type DummyTransport ¶
type DummyTransport struct { // Type specifies the transport type, typically represented as types.DummyTransportType. Type types.TransportType `yaml:"type" json:"type" mapstructure:"type"` // Enabled determines whether this transport configuration is active. Enabled bool `yaml:"enabled" json:"enabled" mapstructure:"enabled"` // IPv4 defines the IPv4 address the dummy transport will bind to. IPv4 string `yaml:"ipv4" json:"ipv4" mapstructure:"ipv4"` // Port specifies the port on which the dummy transport will listen. Port int `yaml:"port" json:"port" mapstructure:"port"` // TLS holds the TLS configuration for the dummy transport. // Although this is typically not used in dummy transports, it allows for optional secure communication. TLS TLS `yaml:"tls" json:"tls" mapstructure:"tls"` }
DummyTransport represents the configuration for a dummy transport used in testing or development environments. It implements the TransportConfig interface, providing basic transport settings like IP address, port, and optional TLS configurations.
func (DummyTransport) Addr ¶
func (q DummyTransport) Addr() string
Addr returns the full address (IPv4 and port) as a string for the dummy transport. This is the address the dummy transport binds to.
Example usage:
addr := dummyTransport.Addr()
Returns:
string: The formatted IPv4 address and port.
func (DummyTransport) GetTLSConfig ¶
func (q DummyTransport) GetTLSConfig() (*tls.Config, error)
GetTLSConfig returns a basic TLS configuration for the dummy transport. It skips certificate verification and uses an empty list of certificates by default, making it more suitable for testing or internal use.
Example usage:
tlsConfig, err := dummyTransport.GetTLSConfig() if err != nil { log.Fatalf("Failed to get TLS config: %v", err) }
Returns:
*tls.Config: The TLS configuration for the dummy transport. error: Returns an error if setting up TLS fails.
func (DummyTransport) GetTransportType ¶
func (q DummyTransport) GetTransportType() types.TransportType
GetTransportType returns the transport type, which is typically Dummy for this struct.
Example usage:
transportType := dummyTransport.GetTransportType()
Returns:
types.TransportType: The transport type for the dummy transport.
func (*DummyTransport) UnmarshalYAML ¶
func (d *DummyTransport) UnmarshalYAML(value *yaml.Node) error
UnmarshalYAML provides custom unmarshaling logic for the DummyTransport from a YAML configuration. It decodes the common fields such as type, enabled status, IPv4 address, port, and TLS configuration.
Example YAML format:
type: dummy enabled: true ipv4: "127.0.0.1" port: 8080 tls: insecure: true
Parameters:
value (*yaml.Node): The YAML node to be decoded.
Returns:
error: Returns an error if the unmarshaling fails; otherwise, nil.
type Logger ¶
type Logger struct { // Enabled determines whether logging is enabled or disabled in the application. // If set to false, logging will be disabled. Enabled bool `yaml:"enabled"` // Environment defines the environment in which the application is running (e.g., "production" or "development"). // This field can be used to adjust logging behavior based on the environment (e.g., verbose logging in development, minimal logging in production). Environment string `yaml:"environment"` // Level specifies the log level (e.g., "debug", "info", "warn", "error"). The log level controls // the verbosity of the log output, with higher levels (like "error") showing only critical messages. Level string `yaml:"level"` }
Logger represents the configuration for the logging system used by the application. It allows customization of logging behavior, including enabling/disabling logging, setting the environment (e.g., production, development), and specifying the log level.
type Mdbx ¶
type Mdbx struct { // Enabled determines if MDBX is enabled for the application. Enabled bool `yaml:"enabled"` // Nodes is a list of MDBX nodes. Each node contains its own configuration, allowing // multiple MDBX databases to be configured with different paths, sizes, and performance settings. Nodes []MdbxNode `yaml:"nodes"` }
Mdbx represents the global MDBX configuration. It enables or disables MDBX functionality and holds a list of MDBX nodes, each of which corresponds to a specific MDBX instance configuration.
type MdbxNode ¶
type MdbxNode struct { // Name is the identifier for the MDBX node, allowing the system to distinguish between multiple nodes. Name string `yaml:"name"` // Path specifies the file system path where the MDBX database files are stored. Path string `yaml:"path"` // MaxReaders defines the maximum number of readers allowed for the MDBX instance. // This controls how many concurrent read transactions can be active at the same time. MaxReaders int `yaml:"maxReaders"` // MaxSize defines the maximum size of the MDBX database in bytes. This is the upper limit // on the size the database can grow to on disk. MaxSize int64 `yaml:"maxSize"` // MinSize defines the minimum size of the MDBX database in bytes. The database will allocate // at least this amount of space on disk. MinSize int64 `yaml:"minSize"` // GrowthStep specifies the size in bytes by which the MDBX database will grow when it needs more space. // This controls how efficiently the database expands on disk. GrowthStep int64 `yaml:"growthStep"` // FilePermissions sets the file system permissions for the MDBX database files. It defaults to 0600, // which grants read and write access to the file owner only. FilePermissions uint `yaml:"filePermissions"` }
MdbxNode represents the configuration for an individual MDBX node. Each node corresponds to an instance of the MDBX database, with specific configurations for file path, size, and performance optimizations.
type Pprof ¶
type Pprof struct { // Enabled determines if pprof profiling is enabled for the service. Enabled bool `yaml:"enabled"` // Name is the name of the service that uses pprof for profiling. // This allows the configuration to be tied to a specific service. Name string `yaml:"name"` // Addr is the address where the pprof server will be hosted. // Typically, this will be in the form of an IP address and port (e.g., "127.0.0.1:6060"). Addr string `yaml:"addr"` }
Pprof represents the configuration for enabling pprof profiling in Go services. It allows you to configure the pprof server, including whether it is enabled, the service name, and the address on which the pprof server should run.
type QuicTransport ¶
type QuicTransport struct { // Type defines the transport type, typically represented as types.QUICTransportType. Type types.TransportType `yaml:"type" json:"type" mapstructure:"type"` // Enabled determines if the QUIC transport is enabled. Enabled bool `yaml:"enabled" json:"enabled" mapstructure:"enabled"` // IPv4 is the IPv4 address where the QUIC server or client will bind. IPv4 string `yaml:"ipv4" json:"ipv4" mapstructure:"ipv4"` // Port is the port number on which the QUIC transport will operate. Port int `yaml:"port" json:"port" mapstructure:"port"` // TLS holds the TLS configuration for the QUIC transport, as QUIC requires // TLS for secure communication. TLS TLS `yaml:"tls" json:"tls" mapstructure:"tls"` }
QuicTransport represents the configuration for QUIC-based transport. It implements the TransportConfig interface and provides all necessary fields to configure and use QUIC transport, which is a fast, connection-oriented protocol over UDP, often used for low-latency applications.
func (QuicTransport) Addr ¶
func (q QuicTransport) Addr() string
Addr returns the full address (IPv4 and port) as a string for the QUIC transport. This address is used by the QUIC server or client to bind or connect to.
Example usage:
addr := quicTransport.Addr()
Returns:
string: The full IPv4 address and port.
func (QuicTransport) GetTLSConfig ¶
func (q QuicTransport) GetTLSConfig() (*tls.Config, error)
GetTLSConfig loads the TLS configuration required for QUIC transport. It checks for the existence of the certificate and key files, loads them, and optionally loads the Root CA if specified.
Example usage:
tlsConfig, err := quicTransport.GetTLSConfig() if err != nil { log.Fatalf("Failed to load TLS config: %v", err) }
Returns:
*tls.Config: The TLS configuration for the QUIC transport. error: Returns an error if TLS setup fails.
func (QuicTransport) GetTransportType ¶
func (q QuicTransport) GetTransportType() types.TransportType
GetTransportType returns the transport type, which is typically QUIC for this struct.
Example usage:
transportType := quicTransport.GetTransportType()
Returns:
types.TransportType: The transport type.
func (*QuicTransport) UnmarshalYAML ¶
func (q *QuicTransport) UnmarshalYAML(value *yaml.Node) error
UnmarshalYAML is a custom YAML unmarshaler for QuicTransport. It decodes the YAML configuration into the QuicTransport struct fields, mapping the common transport fields like Type, Enabled, IPv4, Port, and TLS.
Example YAML format:
type: quic enabled: true ipv4: "127.0.0.1" port: 4242 tls: cert: "/path/to/cert.pem" key: "/path/to/key.pem" rootCa: "/path/to/rootCA.pem"
Parameters:
value (*yaml.Node): The YAML node to be decoded.
Returns:
error: Returns an error if unmarshaling fails; otherwise, nil.
type TLS ¶
type TLS struct { // Insecure determines whether to skip verifying the server's TLS certificate. Insecure bool `yaml:"insecure"` // Cert is the path to the TLS certificate used for encryption. Cert string `json:"cert"` // Key is the path to the private key corresponding to the TLS certificate. Key string `json:"key"` // RootCA is the path to the Root Certificate Authority used for validating the server's TLS certificate. RootCA string `json:"rootCa"` }
TLS holds the TLS configuration used by the transport if needed. While Unix Domain Sockets typically don't use TLS, other transports like QUIC may require it for secure communication.
type TcpTransport ¶
type TcpTransport struct { // Type defines the transport type, typically represented as types.TCPTransportType. Type types.TransportType `yaml:"type" json:"type" mapstructure:"type"` // Enabled determines if the TCP transport is enabled. Enabled bool `yaml:"enabled" json:"enabled" mapstructure:"enabled"` // IPv4 is the IPv4 address where the TCP server or client will bind. IPv4 string `yaml:"ipv4" json:"ipv4" mapstructure:"ipv4"` // Port is the port number on which the TCP transport will operate. Port int `yaml:"port" json:"port" mapstructure:"port"` // TLS holds the TLS configuration for the TCP transport, if TLS is required. TLS *TLS `yaml:"tls" json:"tls" mapstructure:"tls"` }
TcpTransport represents the configuration for TCP-based transport. It implements the TransportConfig interface and provides all necessary fields to configure and use TCP transport.
func (TcpTransport) Addr ¶
func (t TcpTransport) Addr() string
Addr returns the full address (IPv4 and port) as a string for the TCP transport. This address is used by the TCP server or client to bind or connect to.
Example usage:
addr := tcpTransport.Addr()
Returns:
string: The full IPv4 address and port.
func (TcpTransport) GetTLSConfig ¶
func (t TcpTransport) GetTLSConfig() (*tls.Config, error)
GetTLSConfig loads the TLS configuration if specified. This allows the TCP transport to use TLS for secure communication.
Example usage:
tlsConfig, err := tcpTransport.GetTLSConfig() if err != nil { log.Fatalf("Failed to load TLS config: %v", err) }
Returns:
*tls.Config: The TLS configuration for the TCP transport, or nil if not using TLS. error: Returns an error if TLS setup fails.
func (TcpTransport) GetTransportType ¶
func (t TcpTransport) GetTransportType() types.TransportType
GetTransportType returns the transport type, which is typically TCP for this struct.
Example usage:
transportType := tcpTransport.GetTransportType()
Returns:
types.TransportType: The transport type.
func (*TcpTransport) UnmarshalYAML ¶
func (t *TcpTransport) UnmarshalYAML(value *yaml.Node) error
UnmarshalYAML is a custom YAML unmarshaler for TcpTransport. It decodes the YAML configuration into the TcpTransport struct fields, mapping the common transport fields like Type, Enabled, IPv4, Port, and TLS.
Example YAML format:
type: tcp enabled: true ipv4: "127.0.0.1" port: 4242 tls: insecure: true cert: "/path/to/cert.pem" key: "/path/to/key.pem" rootCa: "/path/to/rootCA.pem"
Parameters:
value (*yaml.Node): The YAML node to be decoded.
Returns:
error: Returns an error if unmarshaling fails; otherwise, nil.
type Transport ¶
type Transport struct { // Type defines the type of transport being used (e.g., UDS, Dummy, QUIC). Type types.TransportType `yaml:"type"` // Enabled indicates whether the transport is enabled or disabled. Enabled bool `yaml:"enabled"` // Config holds the specific configuration for the given transport type. // This is populated dynamically based on the Type field during unmarshalling. Config TransportConfig `yaml:"-"` }
Transport holds the generic transport configuration for different types of communication protocols, including UDS, QUIC, and Dummy transports.
This struct is used to unmarshal transport configuration from YAML and determine which specific transport type to instantiate based on the type field.
func (*Transport) UnmarshalYAML ¶
UnmarshalYAML unmarshals a YAML node into the Transport struct. It first decodes the common transport fields (Type, Enabled) and then dynamically unmarshals the specific transport configuration (DummyTransport, UdsTransport, QuicTransport) based on the transport type.
Example YAML configuration:
type: uds enabled: true config: socket: /tmp/my-uds.sock
Parameters:
value (*yaml.Node): The YAML node to be decoded.
Returns:
error: Returns an error if unmarshaling fails; otherwise, nil.
type TransportConfig ¶
type TransportConfig interface { // GetTransportType returns the type of transport (e.g., UDS, Dummy). GetTransportType() types.TransportType }
TransportConfig defines an interface for transport configurations. Each transport implementation (e.g., DummyTransport, UdsTransport) must implement this interface to provide its specific transport type.
type UdpTransport ¶
type UdpTransport struct { // Type defines the transport type, typically represented as types.UDPTransportType. Type types.TransportType `yaml:"type" json:"type" mapstructure:"type"` // Enabled determines if the UDP transport is enabled. Enabled bool `yaml:"enabled" json:"enabled" mapstructure:"enabled"` // IPv4 is the IPv4 address where the UDP server or client will bind. IPv4 string `yaml:"ipv4" json:"ipv4" mapstructure:"ipv4"` // Port is the port number on which the UDP transport will operate. Port int `yaml:"port" json:"port" mapstructure:"port"` // DTLS holds the DTLS configuration for the UDP transport, if DTLS is required. DTLS *DTLS `yaml:"dtls" json:"dtls" mapstructure:"dtls"` }
UdpTransport represents the configuration for UDP-based transport, with optional DTLS support.
func (UdpTransport) Addr ¶
func (t UdpTransport) Addr() string
Addr returns the full address (IPv4 and port) as a string for the UDP transport. This address is used by the UDP server or client to bind or connect to.
Example usage:
addr := udpTransport.Addr()
Returns:
string: The full IPv4 address and port.
func (UdpTransport) GetDTLSConfig ¶
func (t UdpTransport) GetDTLSConfig() (*tls.Config, error)
GetDTLSConfig loads the DTLS configuration if specified. This allows the UDP transport to use DTLS for secure communication.
Example usage:
dtlsConfig, err := udpTransport.GetDTLSConfig() if err != nil { log.Fatalf("Failed to load DTLS config: %v", err) }
Returns:
*tls.Config: The DTLS configuration for the UDP transport, or nil if not using DTLS. error: Returns an error if DTLS setup fails.
func (UdpTransport) GetTransportType ¶
func (t UdpTransport) GetTransportType() types.TransportType
GetTransportType returns the transport type, which is typically UDP for this struct.
Example usage:
transportType := udpTransport.GetTransportType()
Returns:
types.TransportType: The transport type.
func (*UdpTransport) UnmarshalYAML ¶
func (t *UdpTransport) UnmarshalYAML(value *yaml.Node) error
UnmarshalYAML is a custom YAML unmarshaler for UdpTransport. It decodes the YAML configuration into the UdpTransport struct fields, mapping the common transport fields like Type, Enabled, IPv4, Port, and DTLS.
Example YAML format:
type: udp enabled: true ipv4: "127.0.0.1" port: 4242 dtls: insecure: true cert: "/path/to/cert.pem" key: "/path/to/key.pem" rootCa: "/path/to/rootCA.pem"
Parameters:
value (*yaml.Node): The YAML node to be decoded.
Returns:
error: Returns an error if unmarshaling fails; otherwise, nil.
type UdsTransport ¶
type UdsTransport struct { // Type defines the transport type, typically represented as types.TransportTypeUDS for UDS. Type types.TransportType `yaml:"type" json:"type" mapstructure:"type"` // Enabled determines if this transport configuration is active or not. Enabled bool `yaml:"enabled" json:"enabled" mapstructure:"enabled"` // Socket is the file path to the Unix Domain Socket. This field is required to establish // UDS communication, representing the location where the socket is created. Socket string `yaml:"socket" json:"socket" mapstructure:"socket"` }
UdsTransport represents the configuration for a Unix Domain Socket (UDS) transport. It implements the TransportConfig interface to be used in applications requiring UDS transport configuration for inter-process communication (IPC) on the same machine.
func (UdsTransport) Addr ¶
func (u UdsTransport) Addr() string
Addr returns the address (file path) of the UDS socket. This method implements the Addr() method from the TransportConfig interface.
Example usage:
socketPath := udsTransport.Addr()
Returns:
string: The Unix Domain Socket file path.
func (UdsTransport) GetTransportType ¶
func (u UdsTransport) GetTransportType() types.TransportType
GetTransportType returns the type of transport, which is typically types.TransportTypeUDS for Unix Domain Socket communication.
Example usage:
transportType := udsTransport.GetTransportType()
Returns:
types.TransportType: The type of transport.
func (*UdsTransport) UnmarshalYAML ¶
func (u *UdsTransport) UnmarshalYAML(value *yaml.Node) error
UnmarshalYAML provides custom unmarshaling logic for UdsTransport from YAML format. It reads the YAML fields and assigns them to the UdsTransport struct, ensuring proper decoding of all transport fields. This method is useful when loading configurations from a YAML file.
Example YAML format:
type: uds enabled: true socket: /tmp/my-uds.sock
Parameters:
value (*yaml.Node): The YAML node to be decoded.
Returns:
error: Returns an error if unmarshaling fails; otherwise, nil.