Documentation
¶
Overview ¶
Package options 提供各种组件的配置选项
这个包定义了常用外部服务和组件的配置选项类型,包括:
- Redis 配置选项
- MySQL 配置选项
- PostgreSQL 配置选项
所有配置选项类型都实现了以下标准方法:
- Validate() - 验证配置的有效性
- Complete() - 补充默认值
- AddFlags() - 添加命令行标志支持
这些配置选项支持多种配置方式:
- 命令行标志 (via pflag)
- 配置文件 (via viper/mapstructure)
- 环境变量
- 代码直接设置
示例用法:
// 创建默认配置
opts := options.NewRedisOptions()
// 添加命令行标志
fs := pflag.NewFlagSet("app", pflag.ExitOnError)
opts.AddFlags(fs)
fs.Parse(os.Args[1:])
// 验证配置
if err := opts.Validate(); err != nil {
log.Fatal(err)
}
// 补充默认值
if err := opts.Complete(); err != nil {
log.Fatal(err)
}
此包属于 Layer 1 (Foundation),不依赖任何其他 goagent 包
Index ¶
Constants ¶
View Source
const MaxRedisDB = 15
MaxRedisDB is the maximum Redis database number (0-15 by default)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MySQLOptions ¶
type MySQLOptions struct {
Host string `mapstructure:"host" yaml:"host" json:"host"`
Port int `mapstructure:"port" yaml:"port" json:"port"`
User string `mapstructure:"user" yaml:"user" json:"user"`
Password string `mapstructure:"password" yaml:"password" json:"-"` // json:"-" 防止序列化泄露
Database string `mapstructure:"database" yaml:"database" json:"database"`
Charset string `mapstructure:"charset" yaml:"charset" json:"charset"`
ParseTime bool `mapstructure:"parse_time" yaml:"parse_time" json:"parse_time"`
Loc string `mapstructure:"loc" yaml:"loc" json:"loc"`
MaxIdleConns int `mapstructure:"max_idle_conns" yaml:"max_idle_conns" json:"max_idle_conns"`
MaxOpenConns int `mapstructure:"max_open_conns" yaml:"max_open_conns" json:"max_open_conns"`
ConnMaxLifetime time.Duration `mapstructure:"conn_max_lifetime" yaml:"conn_max_lifetime" json:"conn_max_lifetime"`
LogLevel string `mapstructure:"log_level" yaml:"log_level" json:"log_level"`
AutoMigrate bool `mapstructure:"auto_migrate" yaml:"auto_migrate" json:"auto_migrate"`
}
MySQLOptions MySQL 配置选项
type PostgresOptions ¶
type PostgresOptions struct {
Host string `mapstructure:"host" yaml:"host" json:"host"`
Port int `mapstructure:"port" yaml:"port" json:"port"`
User string `mapstructure:"user" yaml:"user" json:"user"`
Password string `mapstructure:"password" yaml:"password" json:"-"` // json:"-" 防止序列化泄露
Database string `mapstructure:"database" yaml:"database" json:"database"`
SSLMode string `mapstructure:"ssl_mode" yaml:"ssl_mode" json:"ssl_mode"`
TimeZone string `mapstructure:"time_zone" yaml:"time_zone" json:"time_zone"`
MaxIdleConns int `mapstructure:"max_idle_conns" yaml:"max_idle_conns" json:"max_idle_conns"`
MaxOpenConns int `mapstructure:"max_open_conns" yaml:"max_open_conns" json:"max_open_conns"`
ConnMaxLifetime time.Duration `mapstructure:"conn_max_lifetime" yaml:"conn_max_lifetime" json:"conn_max_lifetime"`
LogLevel string `mapstructure:"log_level" yaml:"log_level" json:"log_level"`
AutoMigrate bool `mapstructure:"auto_migrate" yaml:"auto_migrate" json:"auto_migrate"`
}
PostgresOptions PostgreSQL 配置选项
func NewPostgresOptions ¶
func NewPostgresOptions() *PostgresOptions
NewPostgresOptions 创建默认 PostgreSQL 配置
func (*PostgresOptions) AddFlags ¶
func (o *PostgresOptions) AddFlags(fs *pflag.FlagSet)
AddFlags 添加命令行标志
func (*PostgresOptions) Validate ¶
func (o *PostgresOptions) Validate() error
Validate 验证 PostgreSQL 配置
type RedisOptions ¶
type RedisOptions struct {
Addr string `mapstructure:"addr" yaml:"addr" json:"addr"`
Password string `mapstructure:"password" yaml:"password" json:"-"` // json:"-" 防止序列化泄露
DB int `mapstructure:"db" yaml:"db" json:"db"`
PoolSize int `mapstructure:"pool_size" yaml:"pool_size" json:"pool_size"`
MinIdleConns int `mapstructure:"min_idle_conns" yaml:"min_idle_conns" json:"min_idle_conns"`
MaxRetries int `mapstructure:"max_retries" yaml:"max_retries" json:"max_retries"`
TTL time.Duration `mapstructure:"ttl" yaml:"ttl" json:"ttl"`
DialTimeout time.Duration `mapstructure:"dial_timeout" yaml:"dial_timeout" json:"dial_timeout"`
ReadTimeout time.Duration `mapstructure:"read_timeout" yaml:"read_timeout" json:"read_timeout"`
WriteTimeout time.Duration `mapstructure:"write_timeout" yaml:"write_timeout" json:"write_timeout"`
}
RedisOptions Redis 配置选项
Click to show internal directories.
Click to hide internal directories.