From ffcd09c89cca54be300694c8e5a7adc8f27c7a3c Mon Sep 17 00:00:00 2001 From: bvbej Date: Wed, 25 Feb 2026 17:31:28 +0800 Subject: [PATCH] =?UTF-8?q?[=F0=9F=9A=80]=20mapstructure?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/cache/redis.go | 37 ++++++++++--------------- pkg/crypto/interface.go | 14 +++++----- pkg/database/mysql.go | 54 +++++++++++++++++-------------------- pkg/storage/minio_client.go | 16 +++++------ pkg/storage/s3_client.go | 16 +++++------ 5 files changed, 62 insertions(+), 75 deletions(-) diff --git a/pkg/cache/redis.go b/pkg/cache/redis.go index cfbb38c..a87c2c7 100644 --- a/pkg/cache/redis.go +++ b/pkg/cache/redis.go @@ -93,29 +93,20 @@ type RedisRepo interface { // RedisConfig Redis配置 type RedisConfig struct { - // 基础配置 - Addr string `yaml:"addr" json:"addr"` // 地址,如: localhost:6379 - Password string `yaml:"password" json:"password"` // 密码 - DB int `yaml:"db" json:"db"` // 数据库编号 - - // 连接池配置 - PoolSize int `yaml:"pool_size" json:"pool_size"` // 最大连接数 - MinIdleConns int `yaml:"min_idle_conns" json:"min_idle_conns"` // 最小空闲连接数 - - // 超时配置 - DialTimeout time.Duration `yaml:"dial_timeout" json:"dial_timeout"` // 连接超时 - ReadTimeout time.Duration `yaml:"read_timeout" json:"read_timeout"` // 读超时 - WriteTimeout time.Duration `yaml:"write_timeout" json:"write_timeout"` // 写超时 - PoolTimeout time.Duration `yaml:"pool_timeout" json:"pool_timeout"` // 连接池超时 - - // 重试配置 - MaxRetries int `yaml:"max_retries" json:"max_retries"` // 最大重试次数 - MinRetryBackoff time.Duration `yaml:"min_retry_backoff" json:"min_retry_backoff"` // 最小重试间隔 - MaxRetryBackoff time.Duration `yaml:"max_retry_backoff" json:"max_retry_backoff"` // 最大重试间隔 - - // 连接存活 - ConnMaxIdleTime time.Duration `yaml:"conn_max_idle_time" json:"conn_max_idle_time"` // 最大空闲时间 - ConnMaxLifetime time.Duration `yaml:"conn_max_lifetime" json:"conn_max_lifetime"` // 最大生命周期 + Addr string `yaml:"addr" json:"addr" mapstructure:"addr"` + Password string `yaml:"password" json:"password" mapstructure:"password"` + DB int `yaml:"db" json:"db" mapstructure:"db"` + PoolSize int `yaml:"pool_size" json:"pool_size" mapstructure:"pool_size"` + MinIdleConns int `yaml:"min_idle_conns" json:"min_idle_conns" mapstructure:"min_idle_conns"` + DialTimeout time.Duration `yaml:"dial_timeout" json:"dial_timeout" mapstructure:"dial_timeout"` + ReadTimeout time.Duration `yaml:"read_timeout" json:"read_timeout" mapstructure:"read_timeout"` + WriteTimeout time.Duration `yaml:"write_timeout" json:"write_timeout" mapstructure:"write_timeout"` + PoolTimeout time.Duration `yaml:"pool_timeout" json:"pool_timeout" mapstructure:"pool_timeout"` + MaxRetries int `yaml:"max_retries" json:"max_retries" mapstructure:"max_retries"` + MinRetryBackoff time.Duration `yaml:"min_retry_backoff" json:"min_retry_backoff" mapstructure:"min_retry_backoff"` + MaxRetryBackoff time.Duration `yaml:"max_retry_backoff" json:"max_retry_backoff" mapstructure:"max_retry_backoff"` + ConnMaxIdleTime time.Duration `yaml:"conn_max_idle_time" json:"conn_max_idle_time" mapstructure:"conn_max_idle_time"` + ConnMaxLifetime time.Duration `yaml:"conn_max_lifetime" json:"conn_max_lifetime" mapstructure:"conn_max_lifetime"` } // DefaultRedisConfig 默认配置 diff --git a/pkg/crypto/interface.go b/pkg/crypto/interface.go index b140a0b..34ce6ec 100644 --- a/pkg/crypto/interface.go +++ b/pkg/crypto/interface.go @@ -24,13 +24,13 @@ type Signer interface { } type Config struct { - SecretKey string `yaml:"secret_key" json:"secret_key"` // AES对称加密密钥 - SignKey string `yaml:"sign_key" json:"sign_key"` // 签名密钥 - PublicKey string `yaml:"public_key" json:"public_key"` // RSA公钥 - PrivateKey string `yaml:"private_key" json:"private_key"` // RSA私钥 - TimestampWindow time.Duration `yaml:"timestamp_window" json:"timestamp_window"` // 时间戳允许的时间窗口 - EnableTimestamp bool `yaml:"enable_timestamp" json:"enable_timestamp"` // 是否启用时间戳验证 - EnableSignature bool `yaml:"enable_signature" json:"enable_signature"` // 是否启用签名 + SecretKey string `yaml:"secret_key" json:"secret_key" mapstructure:"secret_key"` + SignKey string `yaml:"sign_key" json:"sign_key" mapstructure:"sign_key"` + PublicKey string `yaml:"public_key" json:"public_key" mapstructure:"public_key"` + PrivateKey string `yaml:"private_key" json:"private_key" mapstructure:"private_key"` + TimestampWindow time.Duration `yaml:"timestamp_window" json:"timestamp_window" mapstructure:"timestamp_window"` + EnableTimestamp bool `yaml:"enable_timestamp" json:"enable_timestamp" mapstructure:"enable_timestamp"` + EnableSignature bool `yaml:"enable_signature" json:"enable_signature" mapstructure:"enable_signature"` } type EncryptedRequest struct { diff --git a/pkg/database/mysql.go b/pkg/database/mysql.go index ba2046f..507d465 100644 --- a/pkg/database/mysql.go +++ b/pkg/database/mysql.go @@ -49,45 +49,41 @@ type DBStats struct { Write sql.DBStats `json:"write"` } -// MySQLConfig MySQL配置 type MySQLConfig struct { - Read DBConnConfig `yaml:"read" json:"read"` - Write DBConnConfig `yaml:"write" json:"write"` - Base BaseConfig `yaml:"base" json:"base"` - Logger LoggerConfig `yaml:"logger" json:"logger"` + Read DBConnConfig `yaml:"read" json:"read" mapstructure:"read"` + Write DBConnConfig `yaml:"write" json:"write" mapstructure:"write"` + Base BaseConfig `yaml:"base" json:"base" mapstructure:"base"` + Logger LoggerConfig `yaml:"logger" json:"logger" mapstructure:"logger"` } -// DBConnConfig 数据库连接配置 type DBConnConfig struct { - Addr string `yaml:"addr" json:"addr"` - User string `yaml:"user" json:"user"` - Pass string `yaml:"pass" json:"pass"` - Name string `yaml:"name" json:"name"` - Charset string `yaml:"charset" json:"charset"` // 默认 utf8mb4 - Collation string `yaml:"collation" json:"collation"` // 默认 utf8mb4_unicode_ci - Loc string `yaml:"loc" json:"loc"` // 默认 Local - ParseTime bool `yaml:"parse_time" json:"parse_time"` // 默认 true - Timeout time.Duration `yaml:"timeout" json:"timeout"` // 连接超时(秒) - ReadTimeout time.Duration `yaml:"read_timeout" json:"read_timeout"` // 读超时(秒) - WriteTimeout time.Duration `yaml:"write_timeout" json:"write_timeout"` // 写超时(秒) + Addr string `yaml:"addr" json:"addr" mapstructure:"addr"` + User string `yaml:"user" json:"user" mapstructure:"user"` + Pass string `yaml:"pass" json:"pass" mapstructure:"pass"` + Name string `yaml:"name" json:"name" mapstructure:"name"` + Charset string `yaml:"charset" json:"charset" mapstructure:"charset"` + Collation string `yaml:"collation" json:"collation" mapstructure:"collation"` + Loc string `yaml:"loc" json:"loc" mapstructure:"loc"` + ParseTime bool `yaml:"parse_time" json:"parse_time" mapstructure:"parse_time"` + Timeout time.Duration `yaml:"timeout" json:"timeout" mapstructure:"timeout"` + ReadTimeout time.Duration `yaml:"read_timeout" json:"read_timeout" mapstructure:"read_timeout"` + WriteTimeout time.Duration `yaml:"write_timeout" json:"write_timeout" mapstructure:"write_timeout"` } -// BaseConfig 基础配置 type BaseConfig struct { - MaxOpenConn int `yaml:"max_open_conn" json:"max_open_conn"` - MaxIdleConn int `yaml:"max_idle_conn" json:"max_idle_conn"` - ConnMaxLifetime time.Duration `yaml:"conn_max_lifetime" json:"conn_max_lifetime"` // 秒 - ConnMaxIdleTime time.Duration `yaml:"conn_max_idle_time" json:"conn_max_idle_time"` // 秒 + MaxOpenConn int `yaml:"max_open_conn" json:"max_open_conn" mapstructure:"max_open_conn"` + MaxIdleConn int `yaml:"max_idle_conn" json:"max_idle_conn" mapstructure:"max_idle_conn"` + ConnMaxLifetime time.Duration `yaml:"conn_max_lifetime" json:"conn_max_lifetime" mapstructure:"conn_max_lifetime"` + ConnMaxIdleTime time.Duration `yaml:"conn_max_idle_time" json:"conn_max_idle_time" mapstructure:"conn_max_idle_time"` } -// LoggerConfig 日志配置 type LoggerConfig struct { - SlowThreshold time.Duration `yaml:"slow_threshold" json:"slow_threshold"` - Colorful bool `yaml:"colorful" json:"colorful"` - IgnoreRecordNotFoundError bool `yaml:"ignore_record_not_found_error" json:"ignore_record_not_found_error"` - LogLevel string `yaml:"log_level" json:"log_level"` - LogOutput string `yaml:"log_output" json:"log_output"` // stdout, file - LogFile string `yaml:"log_file" json:"log_file"` // 日志文件路径 + SlowThreshold time.Duration `yaml:"slow_threshold" json:"slow_threshold" mapstructure:"slow_threshold"` + Colorful bool `yaml:"colorful" json:"colorful" mapstructure:"colorful"` + IgnoreRecordNotFoundError bool `yaml:"ignore_record_not_found_error" json:"ignore_record_not_found_error" mapstructure:"ignore_record_not_found_error"` + LogLevel string `yaml:"log_level" json:"log_level" mapstructure:"log_level"` + LogOutput string `yaml:"log_output" json:"log_output" mapstructure:"log_output"` + LogFile string `yaml:"log_file" json:"log_file" mapstructure:"log_file"` } // DefaultMySQLConfig 默认配置 diff --git a/pkg/storage/minio_client.go b/pkg/storage/minio_client.go index af559b1..c046b9d 100644 --- a/pkg/storage/minio_client.go +++ b/pkg/storage/minio_client.go @@ -18,14 +18,14 @@ import ( // Config MinIO配置 type Config struct { - Endpoint string `yaml:"endpoint" json:"endpoint"` // MinIO地址 - AccessKeyID string `yaml:"access_key_id" json:"access_key_id"` // AccessKey - SecretAccessKey string `yaml:"secret_access_key" json:"secret_access_key"` // SecretKey - UseSSL bool `yaml:"use_ssl" json:"use_ssl"` // 是否使用SSL - BucketName string `yaml:"bucket_name" json:"bucket_name"` // 默认桶名称 - Region string `yaml:"region" json:"region"` // 区域 - CDNDomain string `yaml:"cdn_domain" json:"cdn_domain"` // CDN域名(可选) - PresignExpires time.Duration `yaml:"presign_expires" json:"presign_expires"` // 预签名URL过期时间,默认15分钟 + Endpoint string `yaml:"endpoint" json:"endpoint" mapstructure:"endpoint"` + AccessKeyID string `yaml:"access_key_id" json:"access_key_id" mapstructure:"access_key_id"` + SecretAccessKey string `yaml:"secret_access_key" json:"secret_access_key" mapstructure:"secret_access_key"` + UseSSL bool `yaml:"use_ssl" json:"use_ssl" mapstructure:"use_ssl"` + BucketName string `yaml:"bucket_name" json:"bucket_name" mapstructure:"bucket_name"` + Region string `yaml:"region" json:"region" mapstructure:"region"` + CDNDomain string `yaml:"cdn_domain" json:"cdn_domain" mapstructure:"cdn_domain"` + PresignExpires time.Duration `yaml:"presign_expires" json:"presign_expires" mapstructure:"presign_expires"` } // Client MinIO客户端 diff --git a/pkg/storage/s3_client.go b/pkg/storage/s3_client.go index 74e2ec7..bcdf2ab 100644 --- a/pkg/storage/s3_client.go +++ b/pkg/storage/s3_client.go @@ -23,14 +23,14 @@ import ( // Config S3配置 type Config struct { - Endpoint string `yaml:"endpoint" json:"endpoint"` // S3地址(可选,留空使用AWS) - AccessKeyID string `yaml:"access_key_id" json:"access_key_id"` // AccessKey - SecretAccessKey string `yaml:"secret_access_key" json:"secret_access_key"` // SecretKey - UseSSL bool `yaml:"use_ssl" json:"use_ssl"` // 是否使用SSL - BucketName string `yaml:"bucket_name" json:"bucket_name"` // 默认桶名称 - Region string `yaml:"region" json:"region"` // 区域 - CDNDomain string `yaml:"cdn_domain" json:"cdn_domain"` // CDN域名(可选) - PresignExpires time.Duration `yaml:"presign_expires" json:"presign_expires"` // 预签名URL过期时间,默认15分钟 + Endpoint string `yaml:"endpoint" json:"endpoint" mapstructure:"endpoint"` + AccessKeyID string `yaml:"access_key_id" json:"access_key_id" mapstructure:"access_key_id"` + SecretAccessKey string `yaml:"secret_access_key" json:"secret_access_key" mapstructure:"secret_access_key"` + UseSSL bool `yaml:"use_ssl" json:"use_ssl" mapstructure:"use_ssl"` + BucketName string `yaml:"bucket_name" json:"bucket_name" mapstructure:"bucket_name"` + Region string `yaml:"region" json:"region" mapstructure:"region"` + CDNDomain string `yaml:"cdn_domain" json:"cdn_domain" mapstructure:"cdn_domain"` + PresignExpires time.Duration `yaml:"presign_expires" json:"presign_expires" mapstructure:"presign_expires"` } // Client S3客户端 -- 2.34.1