apploader

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnvFile

func EnvFile(baseName, env string) string

EnvFile 返回按环境约定的配置文件名: env 为空时返回 baseName,否则返回 baseName.{env}(如 config.prod)。 配合 SetConfigFileSearcher 使用:loader.SetConfigFileSearcher(EnvFile("config", env), ".")

func Redact

func Redact(config interface{}) interface{}

Redact 返回配置的安全快照;配置未实现 Redactor 时返回类型名占位。

Types

type Configuration

type Configuration struct {
	// Name 是应用名称。
	Name string `mapstructure:"name" toml:"name"`
	// Version 是应用配置声明的版本。
	Version string `mapstructure:"version" toml:"version"`
	// Web 保存 Web 监听配置。
	Web web `mapstructure:"web" toml:"web"`
	// Db 保存 PostgreSQL 配置;字段名为兼容既有调用保留。
	Db db `mapstructure:"db" toml:"db"`
	// Redis 保存 Redis 配置。
	Redis redis `mapstructure:"redis" toml:"redis"`
	// LogConf 保存日志输出配置。
	LogConf logConf `mapstructure:"logConf" toml:"logConf"`
}

Configuration 定义脚手架内置的基础配置示例。 字段标签同时支持 Viper 的 mapstructure 和直接 TOML 解码。

var Config Configuration

Config 保留历史全局配置变量,供尚未迁移到业务自定义配置结构的调用方使用。 新项目应优先声明自己的配置实例,避免多个应用共享可变全局状态。

func (Configuration) Redact

func (c Configuration) Redact() map[string]interface{}

Redact 输出不含密码的配置快照,供日志与诊断使用。

type Loader

type Loader interface {
	// LoadToStruct 将配置文件和环境变量映射到非 nil 结构体指针。
	LoadToStruct(config interface{}) error
	// SetConfigFileSearcher 设置配置文件名称和搜索目录,并立即尝试读取配置文件。
	SetConfigFileSearcher(configName string, searchPath ...string) Loader
	// EnableEnvSearcher 开启环境变量覆盖;非空前缀会生成 PREFIX_SECTION_FIELD 格式的变量名。
	EnableEnvSearcher(envPrefix string) Loader
	// Watch 监听配置文件变更,重载成功(含业务校验)后同步调用 handler。
	// 必须在 LoadToStruct 之后调用;仅支持文件源的变更监听。
	Watch(handler func()) error
}

Loader 定义配置文件和环境变量的链式加载能力。 链式配置阶段发生的错误会被暂存,并在 LoadToStruct 时统一返回给调用方。

func NewLoader

func NewLoader() Loader

NewLoader 创建互不共享状态的配置加载器。

type Redactor

type Redactor interface {
	// Redact 返回可安全输出的配置快照;密码、Token 等敏感字段必须打码。
	Redact() map[string]interface{}
}

Redactor 由业务配置结构实现,输出不包含敏感信息的配置快照。 典型用途:启动或热更新时打印脱敏后的配置摘要。

type Validator

type Validator interface {
	// Validate 返回配置不可用于启动的具体原因,不得在错误中包含密码或完整连接串。
	Validate() error
}

Validator 可由业务配置结构实现,用于在反序列化完成后执行必填项和取值范围校验。

Jump to

Keyboard shortcuts

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