options

package
v0.6.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 6, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

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 配置选项

func NewMySQLOptions

func NewMySQLOptions() *MySQLOptions

NewMySQLOptions 创建默认 MySQL 配置

func (*MySQLOptions) AddFlags

func (o *MySQLOptions) AddFlags(fs *pflag.FlagSet)

AddFlags 添加命令行标志

func (*MySQLOptions) Complete

func (o *MySQLOptions) Complete() error

Complete 补充默认值

func (*MySQLOptions) DSN

func (o *MySQLOptions) DSN() string

DSN 生成 MySQL 连接字符串

func (*MySQLOptions) String

func (o *MySQLOptions) String() string

String 返回安全的字符串表示(隐藏密码)

func (*MySQLOptions) Validate

func (o *MySQLOptions) Validate() error

Validate 验证 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) Complete

func (o *PostgresOptions) Complete() error

Complete 补充默认值

func (*PostgresOptions) DSN

func (o *PostgresOptions) DSN() string

DSN 生成 PostgreSQL 连接字符串

func (*PostgresOptions) String

func (o *PostgresOptions) String() string

String 返回安全的字符串表示(隐藏密码)

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 配置选项

func NewRedisOptions

func NewRedisOptions() *RedisOptions

NewRedisOptions 创建默认 Redis 配置

func (*RedisOptions) AddFlags

func (o *RedisOptions) AddFlags(fs *pflag.FlagSet)

AddFlags 添加命令行标志

func (*RedisOptions) Complete

func (o *RedisOptions) Complete() error

Complete 补充默认值

func (*RedisOptions) String

func (o *RedisOptions) String() string

String 返回安全的字符串表示(隐藏密码)

func (*RedisOptions) Validate

func (o *RedisOptions) Validate() error

Validate 验证 Redis 配置

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL