Documentation
¶
Overview ¶
Package config provides configuration management with hot-reload support.
Index ¶
- func Load[T any](path string) T
- func LoadFrom[T any](src Source[T]) T
- func NewCfg[T any](path string) T
- type BaseConfig
- type CORSConfig
- type CasbinConfig
- type Config
- type DatabaseConfig
- type FileSource
- type JWTConfig
- type KafkaConfig
- type LogConfig
- type LogFileConfig
- type MongoConfig
- type OtelConfig
- type RedisConfig
- type Source
- type Store
- type SystemConfig
- type WatchableSource
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BaseConfig ¶
type BaseConfig = Config
BaseConfig is an alias for Config for backward compatibility.
type CORSConfig ¶
type CORSConfig struct {
AllowOrigins []string `mapstructure:"allow_origins"`
AllowMethods []string `mapstructure:"allow_methods"`
AllowHeaders []string `mapstructure:"allow_headers"`
AllowCredentials bool `mapstructure:"allow_credentials"`
}
CORSConfig contains CORS settings.
type CasbinConfig ¶
type CasbinConfig struct {
Model string `mapstructure:"model"`
ModelFile string `mapstructure:"model_file"`
Enabled bool `mapstructure:"enabled"`
SkipPaths []string `mapstructure:"skip_paths"`
AdminUsers []string `mapstructure:"admin_users"`
}
CasbinConfig contains Casbin settings.
type Config ¶
type Config struct {
System SystemConfig `mapstructure:"system"`
Database DatabaseConfig `mapstructure:"database"`
Redis RedisConfig `mapstructure:"redis"`
Mongodb MongoConfig `mapstructure:"mongodb"`
Kafka KafkaConfig `mapstructure:"kafka"`
Log LogConfig `mapstructure:"log"`
JWT JWTConfig `mapstructure:"jwt"`
CORS CORSConfig `mapstructure:"cors"`
Casbin CasbinConfig `mapstructure:"casbin"`
Otel OtelConfig `mapstructure:"otel"`
}
Config is the main application configuration.
type DatabaseConfig ¶ added in v0.0.4
type DatabaseConfig struct {
Driver string `mapstructure:"driver"` // mysql, postgres, sqlite
Host string `mapstructure:"host"` // mysql/postgres: server host
Port int `mapstructure:"port"` // mysql/postgres: server port
User string `mapstructure:"user"` // mysql/postgres: username
Password string `mapstructure:"password"` // mysql/postgres: password
Database string `mapstructure:"database"` // database name or sqlite file path
// MySQL specific
Charset string `mapstructure:"charset"` // mysql: character set (default: utf8mb4)
// PostgreSQL specific
SSLMode string `mapstructure:"ssl_mode"` // postgres: disable, require, verify-ca, verify-full
Schema string `mapstructure:"schema"` // postgres: search_path schema
// Common options
TimeZone string `mapstructure:"timezone"` // timezone for parsing time (e.g., "Asia/Shanghai", "Local")
MaxIdleConns int `mapstructure:"max_idle_conns"` // max idle connections in pool
MaxOpenConns int `mapstructure:"max_open_conns"` // max open connections in pool
MaxLifetime int `mapstructure:"max_lifetime"` // max connection lifetime in seconds
ConnMaxIdleTime int `mapstructure:"conn_max_idle_time"` // max connection idle time in seconds
}
DatabaseConfig contains database connection settings. Supports mysql, postgres, and sqlite drivers.
type FileSource ¶
FileSource loads configuration from a local file.
type JWTConfig ¶
type JWTConfig struct {
Secret string `mapstructure:"secret"`
Expire time.Duration `mapstructure:"expire"`
SkipPaths []string `mapstructure:"skip_paths"`
Enabled bool `mapstructure:"enabled"`
}
JWTConfig contains JWT settings.
type KafkaConfig ¶
type KafkaConfig struct {
Brokers []string `mapstructure:"brokers"`
ClientID string `mapstructure:"client_id"`
Topic string `mapstructure:"topic"`
}
KafkaConfig contains Kafka connection settings.
type LogConfig ¶
type LogConfig struct {
Level string `mapstructure:"level"`
Format string `mapstructure:"format"` // json, text, color
Output string `mapstructure:"output"` // stdout, stderr
File LogFileConfig `mapstructure:"file"`
}
LogConfig contains logging settings.
type LogFileConfig ¶
type LogFileConfig struct {
Filename string `mapstructure:"filename"`
MaxSize int `mapstructure:"max_size"`
MaxBackups int `mapstructure:"max_backups"`
MaxAge int `mapstructure:"max_age"`
Compress bool `mapstructure:"compress"`
Format string `mapstructure:"format"`
}
LogFileConfig contains file logging settings.
type MongoConfig ¶
type MongoConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
User string `mapstructure:"user"`
Password string `mapstructure:"password"`
Database string `mapstructure:"database"`
}
MongoConfig contains MongoDB connection settings.
type OtelConfig ¶ added in v0.0.3
type OtelConfig struct {
Enabled bool `mapstructure:"enabled"` // 是否启用 OpenTelemetry
ServiceName string `mapstructure:"service_name"` // 服务名称
Environment string `mapstructure:"environment"` // 环境标识: dev, test, prod
OTLPEndpoint string `mapstructure:"otlp_endpoint"` // OTLP gRPC 端点地址
ExporterType string `mapstructure:"exporter_type"` // 导出器类型: otlp, stdout
SamplingRatio float64 `mapstructure:"sampling_ratio"` // 采样率: 0.0 - 1.0
}
OtelConfig contains OpenTelemetry settings.
type RedisConfig ¶
type RedisConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Password string `mapstructure:"password"`
DB int `mapstructure:"database"`
Database int `mapstructure:"database"` // Alias for DB
PoolSize int `mapstructure:"pool_size"`
}
RedisConfig contains Redis connection settings.
type Store ¶
type Store[T any] struct { // contains filtered or unexported fields }
Store provides atomic read/update for configuration with hot-reload.
func (*Store[T]) Current ¶
func (s *Store[T]) Current() T
Current returns the current configuration.