config

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AppConfig

type AppConfig struct {
	Name    string `mapstructure:"name"`
	Version string `mapstructure:"version"`
	Mode    string `mapstructure:"mode"` // debug/release/test
}

AppConfig 应用配置

type Config

type Config struct {
	// 应用基础配置
	App AppConfig `mapstructure:"app"`

	// 服务器配置
	Server ServerConfig `mapstructure:"server"`

	// 数据库配置
	Database DatabaseConfig `mapstructure:"database"`

	// Redis 配置
	Redis RedisConfig `mapstructure:"redis"`

	// JWT 配置
	JWT JWTConfig `mapstructure:"jwt"`

	// 日志配置
	Log LogConfig `mapstructure:"log"`
}

Config 通用配置结构

func Load

func Load(configPath string) (*Config, error)

Load 加载配置文件

func (*Config) Validate

func (c *Config) Validate() error

Validate 验证配置的有效性,并填充默认值

type DatabaseConfig

type DatabaseConfig struct {
	MySQL MySQLOptions `mapstructure:"mysql"`
}

DatabaseConfig 数据库配置

type JWTConfig

type JWTConfig struct {
	Secret              string `mapstructure:"secret"`
	Expire              int    `mapstructure:"expire"`
	TokenID             string `mapstructure:"token-id"`
	BlacklistPrefix     string `mapstructure:"blacklist-prefix"`
	BlacklistFailClosed bool   `mapstructure:"blacklist-fail-closed"`
}

JWTConfig JWT 配置

type LogConfig

type LogConfig struct {
	DisableCaller     bool   `mapstructure:"disable-caller"`
	DisableStacktrace bool   `mapstructure:"disable-stacktrace"`
	Level             string `mapstructure:"level"`
	Format            string `mapstructure:"format"`
	Dir               string `mapstructure:"dir"`
}

LogConfig 日志配置

type MySQLOptions

type MySQLOptions struct {
	Host                  string        `mapstructure:"host"`
	Username              string        `mapstructure:"username"`
	Password              string        `mapstructure:"password"`
	Database              string        `mapstructure:"database"`
	MaxIdleConnections    int           `mapstructure:"max-idle-connections"`
	MaxOpenConnections    int           `mapstructure:"max-open-connections"`
	MaxConnectionLifeTime time.Duration `mapstructure:"max-connection-life-time"`
	LogLevel              int           `mapstructure:"log-level"`
}

MySQLOptions MySQL 配置选项

type RedisConfig

type RedisConfig struct {
	Addr     string `mapstructure:"addr"`
	Port     string `mapstructure:"port"`
	Password string `mapstructure:"password"`
	DB       int    `mapstructure:"db"`

	// 连接池与超时配置,默认 10/5s/3s/3s
	PoolSize     int           `mapstructure:"pool-size"`
	DialTimeout  time.Duration `mapstructure:"dial-timeout"`
	ReadTimeout  time.Duration `mapstructure:"read-timeout"`
	WriteTimeout time.Duration `mapstructure:"write-timeout"`
}

RedisConfig Redis 配置

type ServerConfig

type ServerConfig struct {
	HTTPAddr  string `mapstructure:"http-addr"`
	HTTPSAddr string `mapstructure:"https-addr"`
	TLSCert   string `mapstructure:"tls-cert"`
	TLSKey    string `mapstructure:"tls-key"`

	// ShutdownTimeout 优雅关闭的最长等待时间
	ShutdownTimeout time.Duration `mapstructure:"shutdown-timeout"`

	// HTTP 服务器超时与请求头大小限制,默认 10s/30s/60s/120s/1MB
	ReadHeaderTimeout time.Duration `mapstructure:"read-header-timeout"`
	ReadTimeout       time.Duration `mapstructure:"read-timeout"`
	WriteTimeout      time.Duration `mapstructure:"write-timeout"`
	IdleTimeout       time.Duration `mapstructure:"idle-timeout"`
	MaxHeaderBytes    int           `mapstructure:"max-header-bytes"`

	// MaxBodyBytes 请求体大小上限(字节),默认 4MB,超限返回 HTTP 413
	MaxBodyBytes int64 `mapstructure:"max-body-bytes"`

	// TrustedProxies 可信代理列表(IP 或 CIDR)。
	// 默认为空表示不信任任何代理,ClientIP() 直接取 RemoteAddr,避免 X-Forwarded-For 伪造;
	// 部署在反向代理之后时按需配置代理地址。
	TrustedProxies []string `mapstructure:"trusted-proxies"`
}

ServerConfig 服务器配置

Jump to

Keyboard shortcuts

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