apploader

package
v1.57.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 7 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 AdminModule added in v1.20.0

type AdminModule struct {
	Enabled bool   `mapstructure:"enabled"`
	Listen  string `mapstructure:"listen"` // 默认 ":6060"
}

AdminModule Admin 管理服务配置(pprof/metrics/日志级别)。

type AuthModule added in v1.20.0

type AuthModule struct {
	Enabled    bool          `mapstructure:"enabled"`
	AccessTTL  time.Duration `mapstructure:"access_ttl"`  // 访问 token 有效期,默认 30m
	RefreshTTL time.Duration `mapstructure:"refresh_ttl"` // 刷新 token 有效期,默认 168h
	Issuer     string        `mapstructure:"issuer"`      // 签发者
}

AuthModule JWT 认证参数(密钥仍由业务代码 apptoken.SetSecretKey 注入)。

type CacheModule added in v1.20.0

type CacheModule struct {
	Enabled  bool   `mapstructure:"enabled"`
	Addr     string `mapstructure:"addr"` // host:port
	Password string `mapstructure:"password"`
	DB       int    `mapstructure:"db"`
	PoolSize int    `mapstructure:"pool_size"`
}

CacheModule Redis 缓存模块配置。

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 CronModule added in v1.20.0

type CronModule struct {
	Enabled bool `mapstructure:"enabled"`
}

CronModule 定时任务模块配置。

type DatabaseModule added in v1.20.0

type DatabaseModule struct {
	Enabled     bool   `mapstructure:"enabled"`
	Driver      string `mapstructure:"driver"` // sqlite/postgres/mysql
	DSN         string `mapstructure:"dsn"`    // 连接串或 SQLite 文件路径
	Host        string `mapstructure:"host"`
	Port        int    `mapstructure:"port"`
	User        string `mapstructure:"user"`
	Password    string `mapstructure:"password"`
	Database    string `mapstructure:"database"`
	SSLMode     string `mapstructure:"ssl_mode"`
	AutoMigrate bool   `mapstructure:"auto_migrate"`
	MaxIdle     int    `mapstructure:"max_idle_conns"`
	MaxOpen     int    `mapstructure:"max_open_conns"`
}

DatabaseModule 关系数据库模块配置。

type Loader

type Loader interface {
	// LoadToStruct 将配置文件和环境变量映射到非 nil 结构体指针。
	LoadToStruct(config interface{}) error
	// SetConfigFileSearcher 设置项目(子级)配置文件名称和搜索目录。
	// 文件在 LoadToStruct 时读取;项目配置覆盖全局配置的同名键。
	SetConfigFileSearcher(configName string, searchPath ...string) Loader
	// SetGlobalConfigFile 设置全局(父级)配置文件名称和搜索目录。
	// 全局配置先于项目配置加载,项目配置中的同名键覆盖全局配置(子覆盖父)。
	// 全局文件缺失时仅记录错误,不阻塞加载(可选层)。
	SetGlobalConfigFile(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 LogModule added in v1.20.0

type LogModule struct {
	Enabled bool   `mapstructure:"enabled"`
	Level   string `mapstructure:"level"`   // debug/info/warn/error
	OutDir  string `mapstructure:"out_dir"` // 日志目录,默认 "."
}

LogModule 日志模块配置。

type Modules added in v1.20.0

type Modules struct {
	Log      LogModule      `mapstructure:"log"`
	Auth     AuthModule     `mapstructure:"auth"`
	Web      WebModule      `mapstructure:"web"`
	Database DatabaseModule `mapstructure:"database"`
	Cache    CacheModule    `mapstructure:"cache"`
	Mongo    MongoModule    `mapstructure:"mongo"`
	Cron     CronModule     `mapstructure:"cron"`
	Admin    AdminModule    `mapstructure:"admin"`
	Monitor  MonitorModule  `mapstructure:"monitor"`
	OpenAPI  OpenAPIModule  `mapstructure:"openapi"`
}

Modules 脚手架统一模块配置(业务内嵌)。

type AppConfig struct {
    apploader.Modules `mapstructure:",squash"`
    Business          BusinessConfig `mapstructure:"business"`
}

type MongoModule added in v1.20.0

type MongoModule struct {
	Enabled  bool   `mapstructure:"enabled"`
	Addr     string `mapstructure:"addr"` // host:port
	DB       string `mapstructure:"db"`
	User     string `mapstructure:"user"`
	Password string `mapstructure:"password"`
	Timeout  int    `mapstructure:"timeout"` // 秒,默认 20
}

MongoModule MongoDB 模块配置。

type MonitorModule added in v1.20.0

type MonitorModule struct {
	Enabled bool   `mapstructure:"enabled"`
	Path    string `mapstructure:"path"` // 默认 "/monitor"
}

MonitorModule 资源监控模块配置(路由级,Web 启用时挂载)。

type OpenAPIApp added in v1.20.0

type OpenAPIApp struct {
	Secret    string `mapstructure:"secret"`    // HMAC 密钥或 RSA 公钥(PEM)
	Algorithm string `mapstructure:"algorithm"` // HMAC-SHA256 / RSA-SHA256,默认 HMAC-SHA256
	Enabled   bool   `mapstructure:"enabled"`
}

OpenAPIApp 开放平台应用注册配置。

type OpenAPIModule added in v1.20.0

type OpenAPIModule struct {
	Enabled bool                  `mapstructure:"enabled"`
	Apps    map[string]OpenAPIApp `mapstructure:"apps"` // AppKey → 应用配置
}

OpenAPIModule 开放平台模块配置。

type Redactor

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

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

type Validator

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

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

type WebModule added in v1.20.0

type WebModule struct {
	Enabled bool   `mapstructure:"enabled"`
	Port    string `mapstructure:"port"`        // 监听地址,默认 ":8080"
	Level   string `mapstructure:"level"`       // 日志级别
	TimeFmt string `mapstructure:"time_format"` // iris 时间格式(默认 appbox.TimeFormat)
	// Baseline 是否自动挂载安全基线中间件(限流/请求体上限/超时/SQL 注入拦截/日志/安全头)。
	Baseline bool `mapstructure:"baseline"`
	// RatePerSecond 基线全局限流 QPS(默认 100)。
	RatePerSecond float64 `mapstructure:"rate_per_second"`
	// Burst 基线限流突发容量(默认 200)。
	Burst int `mapstructure:"burst"`
	// MaxBodyBytes 请求体上限字节(默认 1MB)。
	MaxBodyBytes int64 `mapstructure:"max_body_bytes"`
	// Timeout 请求超时(默认 10s)。
	Timeout time.Duration `mapstructure:"timeout"`
}

WebModule Web 服务与安全基线配置。

Jump to

Keyboard shortcuts

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