config

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBool

func GetBool(key string) bool

GetBool 获取指定 key 的布尔配置值,支持自定义配置

func GetDuration

func GetDuration(key string) time.Duration

GetDuration 获取指定 key 的 Duration 配置值,支持自定义配置

func GetFloat64

func GetFloat64(key string) float64

GetFloat64 获取指定 key 的浮点数配置值,支持自定义配置

func GetInt

func GetInt(key string) int

GetInt 获取指定 key 的整数配置值,支持自定义配置

func GetString

func GetString(key string) string

GetString 获取指定 key 的字符串配置值,支持自定义配置 key 支持点号分隔的路径,如 "myapp.timeout"

func GetStringMap

func GetStringMap(key string) map[string]any

GetStringMap 获取指定 key 的 map 配置值,支持自定义配置

func GetStringMapString

func GetStringMapString(key string) map[string]string

GetStringMapString 获取指定 key 的 map[string]string 配置值,支持自定义配置

func GetStringSlice

func GetStringSlice(key string) []string

GetStringSlice 获取指定 key 的字符串切片配置值,支持自定义配置

func GetViper

func GetViper() *viper.Viper

GetViper 返回 viper 实例,用于自定义配置查询

func Init

func Init() *viper.Viper

Init 函数负责初始化配置 它会解析命令行参数,读取配置文件,并反序列化到 Config 结构体中

func InitConfig

func InitConfig()

func IsSet

func IsSet(key string) bool

IsSet 检查指定 key 是否已设置,支持自定义配置

func SetViper

func SetViper(viperInstance *viper.Viper)

SetViper 允许外部设置 viper 实例(主要用于测试)

func Unmarshal

func Unmarshal(rawVal any) error

Unmarshal 将整个配置反序列化到用户自定义结构体

func UnmarshalKey

func UnmarshalKey(key string, rawVal any) error

UnmarshalKey 将指定 key 的配置反序列化到用户自定义结构体 例如: config.UnmarshalKey("myapp", &myConfig)

Types

type Aliyun

type Aliyun struct {
	AccessKeyID     *string `mapstructure:"accessKeyId"`
	AccessKeySecret *string `mapstructure:"accessKeySecret"`
	Endpoint        *string `mapstructure:"endpoint"`
	Bucket          *string `mapstructure:"bucket"`
}

Aliyun 阿里云配置

func (*Aliyun) GetAccessKeyID

func (a *Aliyun) GetAccessKeyID() string

func (*Aliyun) GetAccessKeySecret

func (a *Aliyun) GetAccessKeySecret() string

func (*Aliyun) GetBucket

func (a *Aliyun) GetBucket() string

func (*Aliyun) GetEndpoint

func (a *Aliyun) GetEndpoint() string

type Auth

type Auth struct {
	IsAuth     *bool    `mapstructure:"isAuth"`
	Ignores    []string `mapstructure:"ignores"`
	NeedLogins []string `mapstructure:"needLogins"`
}

func (*Auth) GetIgnores

func (a *Auth) GetIgnores() []string

func (*Auth) GetIsAuth

func (a *Auth) GetIsAuth() bool

type Cache

type Cache struct {
	NeedCache []string `mapstructure:"needCache"`
	Expire    *int64   `mapstructure:"expire"` //单位秒
}

func (*Cache) GetExpire

func (c *Cache) GetExpire() int64

func (*Cache) GetNeedCache

func (c *Cache) GetNeedCache() []string

type Config

type Config struct {
	Pay           *Pay           `mapstructure:"pay"`
	Server        *Server        `mapstructure:"server"`
	Cache         *Cache         `mapstructure:"cache"`
	Upload        *Upload        `mapstructure:"upload"`
	Qiniu         *Qiniu         `mapstructure:"qiniu"`
	Aliyun        *Aliyun        `mapstructure:"aliyun"`
	DB            *DB            `mapstructure:"db"`
	Auth          *Auth          `mapstructure:"auth"`
	Wx            *Wx            `mapstructure:"wx"`
	Jwt           *Jwt           `mapstructure:"jwt"`
	Email         *Email         `mapstructure:"email"`
	Log           *LogConfig     `mapstructure:"log"`
	Elasticsearch *Elasticsearch `mapstructure:"elasticsearch"`
	Milvus        *Milvus        `mapstructure:"milvus"`
	Skill         *Skill         `mapstructure:"skill"`
}

func GetConfig

func GetConfig() *Config

GetConfig 返回已加载的配置单例 在调用此函数前,必须先调用 Init()

func (*Config) GetAliyun

func (c *Config) GetAliyun() *Aliyun

func (*Config) GetJwt

func (c *Config) GetJwt() *Jwt

type DB

type DB struct {
	Redis    *Redis    `mapstructure:"redis"`
	Mysql    *Mysql    `mapstructure:"mysql"`
	Postgres *Postgres `mapstructure:"postgres"`
}

type Elasticsearch

type Elasticsearch struct {
	Addresses []string `mapstructure:"addresses"`
	Username  *string  `mapstructure:"username"`
	Password  *string  `mapstructure:"password"`
	APIKey    *string  `mapstructure:"apiKey"`
}

Elasticsearch 配置结构体

func (*Elasticsearch) GetAPIKey

func (e *Elasticsearch) GetAPIKey() string

GetAPIKey 获取 ES API Key

func (*Elasticsearch) GetAddresses

func (e *Elasticsearch) GetAddresses() []string

GetAddresses 获取 ES 地址列表

func (*Elasticsearch) GetPassword

func (e *Elasticsearch) GetPassword() string

GetPassword 获取 ES 密码

func (*Elasticsearch) GetUsername

func (e *Elasticsearch) GetUsername() string

GetUsername 获取 ES 用户名

type Email

type Email struct {
	Host     *string `mapstructure:"host"`
	Port     *int    `mapstructure:"port"`
	Username *string `mapstructure:"username"`
	Password *string `mapstructure:"password"`
	Identity *string `mapstructure:"identity"`
	From     *string `mapstructure:"from"`
	BaseURL  *string `mapstructure:"baseUrl"`
}

func (*Email) GetBaseURL

func (e *Email) GetBaseURL() string

func (*Email) GetFrom

func (e *Email) GetFrom() string

func (*Email) GetHost

func (e *Email) GetHost() string

func (*Email) GetIdentity

func (e *Email) GetIdentity() string

func (*Email) GetPassword

func (e *Email) GetPassword() string

func (*Email) GetPort

func (e *Email) GetPort() int

func (*Email) GetUsername

func (e *Email) GetUsername() string

type Jwt

type Jwt struct {
	Secret  *string        `mapstructure:"secret"`
	Expire  *time.Duration `mapstructure:"expire"`
	Refresh *time.Duration `mapstructure:"refresh"`
}

func (*Jwt) GetExpire

func (j *Jwt) GetExpire() time.Duration

func (*Jwt) GetRefresh

func (j *Jwt) GetRefresh() time.Duration

func (*Jwt) GetSecret

func (j *Jwt) GetSecret() string

type LogConfig

type LogConfig struct {
	Level      *string   `mapstructure:"level"`
	Format     *string   `mapstructure:"format"`
	AddSource  *bool     `mapstructure:"addSource"`
	Filename   *string   `mapstructure:"filename"`
	MaxSize    *int      `mapstructure:"maxSize"`
	MaxAge     *int      `mapstructure:"maxAge"`
	MaxBackups *int      `mapstructure:"maxBackups"`
	Output     io.Writer `mapstructure:"output"`
}

func (*LogConfig) GetAddSource

func (l *LogConfig) GetAddSource() bool

func (*LogConfig) GetFilename

func (l *LogConfig) GetFilename() string

func (*LogConfig) GetFormat

func (l *LogConfig) GetFormat() string

func (*LogConfig) GetLevel

func (l *LogConfig) GetLevel() string

func (*LogConfig) GetMaxAge

func (l *LogConfig) GetMaxAge() int

func (*LogConfig) GetMaxBackups

func (l *LogConfig) GetMaxBackups() int

func (*LogConfig) GetMaxSize

func (l *LogConfig) GetMaxSize() int

type Milvus

type Milvus struct {
	Address  *string `mapstructure:"address"`
	DBName   *string `mapstructure:"dbName"`
	Username *string `mapstructure:"username"`
	Password *string `mapstructure:"password"`
}

Milvus 配置结构体

func (*Milvus) GetAddress

func (m *Milvus) GetAddress() string

GetAddress 获取 Milvus 地址

func (*Milvus) GetDBName

func (m *Milvus) GetDBName() string

GetDBName 获取 Milvus 数据库名称

func (*Milvus) GetPassword

func (m *Milvus) GetPassword() string

GetPassword 获取 Milvus 密码

func (*Milvus) GetUsername

func (m *Milvus) GetUsername() string

GetUsername 获取 Milvus 用户名

type Mysql

type Mysql struct {
	Host         *string        `mapstructure:"host"`
	Port         *int           `mapstructure:"port"`
	User         *string        `mapstructure:"user"`
	Password     *string        `mapstructure:"password"`
	Database     *string        `mapstructure:"database"`
	MaxIdleConns *int           `mapstructure:"maxIdleConns"`
	PingTimeout  *time.Duration `mapstructure:"pingTimeout"`
	MaxOpenConns *int           `mapstructure:"maxOpenConns"`
	Log          *MysqlLog      `mapstructure:"log"`
}

func (*Mysql) GetDatabase

func (m *Mysql) GetDatabase() string

func (*Mysql) GetHost

func (m *Mysql) GetHost() string

func (*Mysql) GetLog

func (m *Mysql) GetLog() *MysqlLog

func (*Mysql) GetMaxIdleConns

func (m *Mysql) GetMaxIdleConns() int

func (*Mysql) GetMaxOpenConns

func (m *Mysql) GetMaxOpenConns() int

func (*Mysql) GetPassword

func (m *Mysql) GetPassword() string

func (*Mysql) GetPingTimeout

func (m *Mysql) GetPingTimeout() time.Duration

func (*Mysql) GetPort

func (m *Mysql) GetPort() int

func (*Mysql) GetUser

func (m *Mysql) GetUser() string

type MysqlLog

type MysqlLog struct {
	SlowThreshold             *time.Duration `mapstructure:"slowThreshold"`
	LogLevel                  *string        `mapstructure:"level"`
	IgnoreRecordNotFoundError *bool          `mapstructure:"ignoreRecordNotFoundError"`
	ParameterizedQueries      *bool          `mapstructure:"parameterizedQueries"`
	Colorful                  *bool          `mapstructure:"colorful"`
}

func (*MysqlLog) GetColorful

func (ml *MysqlLog) GetColorful() bool

func (*MysqlLog) GetIgnoreRecordNotFoundError

func (ml *MysqlLog) GetIgnoreRecordNotFoundError() bool

func (*MysqlLog) GetLogLevel

func (ml *MysqlLog) GetLogLevel() logger.LogLevel

func (*MysqlLog) GetParameterizedQueries

func (ml *MysqlLog) GetParameterizedQueries() bool

func (*MysqlLog) GetSlowThreshold

func (ml *MysqlLog) GetSlowThreshold() time.Duration

type Pay

type Pay struct {
	WxPay *WxPay `mapstructure:"wxPay"`
}

type Postgres

type Postgres struct {
	Host         *string        `mapstructure:"host"`
	Port         *int           `mapstructure:"port"`
	User         *string        `mapstructure:"user"`
	Password     *string        `mapstructure:"password"`
	Database     *string        `mapstructure:"database"`
	SSLMode      *string        `mapstructure:"sslmode"`
	MaxIdleConns *int           `mapstructure:"maxIdleConns"`
	PingTimeout  *time.Duration `mapstructure:"pingTimeout"`
	MaxOpenConns *int           `mapstructure:"maxOpenConns"`
	Log          *PostgresLog   `mapstructure:"log"`
}

func (*Postgres) GetDatabase

func (p *Postgres) GetDatabase() string

func (*Postgres) GetHost

func (p *Postgres) GetHost() string

func (*Postgres) GetLog

func (p *Postgres) GetLog() *PostgresLog

func (*Postgres) GetMaxIdleConns

func (p *Postgres) GetMaxIdleConns() int

func (*Postgres) GetMaxOpenConns

func (p *Postgres) GetMaxOpenConns() int

func (*Postgres) GetPassword

func (p *Postgres) GetPassword() string

func (*Postgres) GetPingTimeout

func (p *Postgres) GetPingTimeout() time.Duration

func (*Postgres) GetPort

func (p *Postgres) GetPort() int

func (*Postgres) GetSSLMode

func (p *Postgres) GetSSLMode() string

func (*Postgres) GetUser

func (p *Postgres) GetUser() string

type PostgresLog

type PostgresLog struct {
	SlowThreshold             *time.Duration `mapstructure:"slowThreshold"`
	LogLevel                  *string        `mapstructure:"level"`
	IgnoreRecordNotFoundError *bool          `mapstructure:"ignoreRecordNotFoundError"`
	ParameterizedQueries      *bool          `mapstructure:"parameterizedQueries"`
	Colorful                  *bool          `mapstructure:"colorful"`
}

func (*PostgresLog) GetColorful

func (pl *PostgresLog) GetColorful() bool

func (*PostgresLog) GetIgnoreRecordNotFoundError

func (pl *PostgresLog) GetIgnoreRecordNotFoundError() bool

func (*PostgresLog) GetLogLevel

func (pl *PostgresLog) GetLogLevel() logger.LogLevel

func (*PostgresLog) GetParameterizedQueries

func (pl *PostgresLog) GetParameterizedQueries() bool

func (*PostgresLog) GetSlowThreshold

func (pl *PostgresLog) GetSlowThreshold() time.Duration

type Qiniu

type Qiniu struct {
	Bucket    *string `mapstructure:"bucket"`
	AccessKey *string `mapstructure:"accessKey"`
	SecretKey *string `mapstructure:"secretKey"`
	Region    *string `mapstructure:"region"`
}

type Redis

type Redis struct {
	Addr         *string `mapstructure:"addr"`
	Password     *string `mapstructure:"password"`
	DB           *int    `mapstructure:"db"`
	PoolSize     *int    `mapstructure:"poolSize"`
	IdleTimeout  *int    `mapstructure:"idleTimeout"`
	MaxOpenConns *int    `mapstructure:"maxOpenConns"`
	MaxIdleConns *int    `mapstructure:"maxIdleConns"`
}

func (*Redis) GetAddr

func (r *Redis) GetAddr() string

func (*Redis) GetDB

func (r *Redis) GetDB() int

func (*Redis) GetMaxIdleConns

func (r *Redis) GetMaxIdleConns() int

func (*Redis) GetMaxOpenConns

func (r *Redis) GetMaxOpenConns() int

func (*Redis) GetPassword

func (r *Redis) GetPassword() string

func (*Redis) GetPoolSize

func (r *Redis) GetPoolSize() int

type Server

type Server struct {
	Port         *int           `mapstructure:"port"`
	Cros         []string       `mapstructure:"cros"`
	AllowOrigins []string       `mapstructure:"allowOrigins"`
	Mode         *string        `mapstructure:"mode"`
	Name         *string        `mapstructure:"name"`
	Version      *string        `mapstructure:"version"`
	Host         *string        `mapstructure:"host"`
	ReadTimeout  *time.Duration `mapstructure:"readTimeout"`
	WriteTimeout *time.Duration `mapstructure:"writeTimeout"`
}

func (*Server) GetCros

func (s *Server) GetCros() []string

func (*Server) GetHost

func (s *Server) GetHost() string

func (*Server) GetMode

func (s *Server) GetMode() string

func (*Server) GetPort

func (s *Server) GetPort() int

func (*Server) GetReadTimeout

func (s *Server) GetReadTimeout() time.Duration

func (*Server) GetWriteTimeout

func (s *Server) GetWriteTimeout() time.Duration

type Skill

type Skill struct {
	BaseDir *string `mapstructure:"baseDir"`
}

func (*Skill) GetBaseDir

func (s *Skill) GetBaseDir() string

type Upload

type Upload struct {
	Prefix *string `mapstructure:"prefix"`
}

type Wx

type Wx struct {
	AppId  *string `mapstructure:"appId"`
	Secret *string `mapstructure:"secret"`
	Token  *string `mapstructure:"token"`
	AesKey *string `mapstructure:"aesKey"`
}

type WxPay

type WxPay struct {
	AppId       *string `mapstructure:"appId"`
	MchId       *string `mapstructure:"mchId"`       //商户证书的证书序列号
	MchSerialNo *string `mapstructure:"mchSerialNo"` //商户证书的证书序列号
	ApiV3Key    *string `mapstructure:"apiV3Key"`    //apiV3Key,商户平台获取
	PrivateKey  *string `mapstructure:"privateKey"`  //私钥 apiclient_key.pem 读取后的内容
	AppSecret   *string `mapstructure:"appSecret"`
	NotifyUrl   *string `mapstructure:"notifyUrl"`
	MchCertPath *string `mapstructure:"mchCertPath"`
	MchKeyPath  *string `mapstructure:"mchKeyPath"`
}

func (*WxPay) GetAppId

func (w *WxPay) GetAppId() string

func (*WxPay) GetMchId

func (w *WxPay) GetMchId() string

func (*WxPay) GetNotifyUrl

func (w *WxPay) GetNotifyUrl() string

Jump to

Keyboard shortcuts

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