Documentation
¶
Index ¶
- func NewContext(ctx context.Context, cfg *Config) context.Context
- func NewRequest(r *http.Request, cfg *Config) *http.Request
- type Config
- func (c *Config) All() map[string]interface{}
- func (c *Config) Get(key string) any
- func (c *Config) GetBool(key string) bool
- func (c *Config) GetDuration(key string) time.Duration
- func (c *Config) GetFloat64(key string) float64
- func (c *Config) GetInt(key string) int
- func (c *Config) GetIntSlice(key string) []int
- func (c *Config) GetString(key string) string
- func (c *Config) GetStringMap(key string) map[string]any
- func (c *Config) GetStringMapString(key string) map[string]string
- func (c *Config) GetStringMapStringSlice(key string) map[string][]string
- func (c *Config) GetStringSlice(key string) []string
- func (c *Config) GetTime(key string) time.Time
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
func FromContext ¶
func FromRequest ¶
func MustConfig ¶
MustConfig creates and returns a new Config instance using the provided options. It behaves like NewConfig but will terminate the program with a log message if an error occurs. This is typically used at application startup when config loading is critical.
Example:
var AppConfig = config.MustConfig(
config.WithRequiredConfigPath("env.yaml"),
config.WithDefaults(map[string]any{
"SERVICE_PORT": ":8080",
}),
)
func NewConfig ¶
NewConfig creates and returns a new Config instance using the provided options. It returns an error if any option fails.
func (*Config) GetFloat64 ¶
func (*Config) GetIntSlice ¶
func (*Config) GetStringMapString ¶
func (*Config) GetStringMapStringSlice ¶
func (*Config) GetStringSlice ¶
type Option ¶
Option is a function that configures the Viper instance.
func WithDefaults ¶
WithDefaults injects fallback config values into the Viper instance.
Example:
config.WithDefaults(map[string]any{
"SERVICE_PORT": ":8080",
"DEBUG_MODE": true,
})
func WithOptionalConfigPaths ¶
WithOptionalConfigPaths loads the first config file found from the given list of paths. It will silently skip missing files but return an error if a file is found but unreadable.
Example:
config.WithOptionalConfigPaths("env.yaml", "../env.yaml")
func WithRequiredConfigPath ¶
WithRequiredConfigPath forces the given config file to exist and be readable, or returns an error.
Example:
config.WithRequiredConfigPath("env.yaml")