configs

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2025 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Mysql  MysqlConfig
	System SystemConfig
	Log    LogConfig
	Mgo    Mongodb
	JWT    JWTConfig
)

Functions

func Bootstrap added in v1.0.2

func Bootstrap(path string) (Config, *Store)

Bootstrap 仅以本地文件为入口初始化配置,随后按文件中的 etcd/consul 配置进行增量合并并挂载热更新。 返回最终 Config 以及可动态读取/更新的 Store。

func InitConfig

func InitConfig(configPath string) (err error)

Types

type CORSConfig added in v1.0.2

type CORSConfig struct {
	AllowOrigins     []string `mapstructure:"allow_origins"`
	AllowHeaders     []string `mapstructure:"allow_headers"`
	AllowMethods     []string `mapstructure:"allow_methods"`
	AllowCredentials bool     `mapstructure:"allow_credentials"`
}

type CasbinConfig added in v1.0.2

type CasbinConfig struct {
	Model     string `mapstructure:"model"`
	ModelFile string `mapstructure:"model_file"`
}

type Config

type Config struct {
	Mysql   MysqlConfig        `mapstructure:"mysql"`
	System  SystemConfig       `mapstructure:"system"`
	Log     LogConfig          `mapstructure:"log"`
	Mongodb Mongodb            `mapstructure:"mongodb"`
	Redis   RedisConfig        `mapstructure:"redis"`
	JWT     JWTConfig          `mapstructure:"jwt"`
	CORS    CORSConfig         `mapstructure:"cors"`
	Casbin  CasbinConfig       `mapstructure:"casbin"`
	Kafka   KafkaConfig        `mapstructure:"kafka"`
	Etcd    EtcdClientConfig   `mapstructure:"etcd"`
	Consul  ConsulClientConfig `mapstructure:"consul"`
}

func Merge added in v1.0.2

func Merge(dst, src Config) Config

Merge 将 src 非零值覆盖到 dst(浅合并,适合配置)

func NewCfg

func NewCfg(p string) Config

func NewCfgFrom added in v1.0.2

func NewCfgFrom(src Source) Config

NewCfgFrom 通过自定义 Source 加载 Config。

type ConfigReloadCallback added in v1.0.2

type ConfigReloadCallback func(*Config)

ConfigReloadCallback 配置重载回调函数类型

type ConsoleSinkConfig added in v1.0.2

type ConsoleSinkConfig struct {
	Format string `mapstructure:"format"`
	Output string `mapstructure:"output"`
}

type ConsulClientConfig added in v1.0.2

type ConsulClientConfig struct {
	Address string `mapstructure:"address"`
	Token   string `mapstructure:"token"`
	Key     string `mapstructure:"key"`
	Format  string `mapstructure:"format"`
}

type ConsulSource added in v1.0.2

type ConsulSource struct {
	Address string
	Token   string
	Key     string
	Format  string // json|yaml|toml
}

ConsulSource 从 Consul KV 读取配置,支持 json/yaml/toml 三种格式。

func (ConsulSource) Load added in v1.0.2

func (c ConsulSource) Load(ctx context.Context) (Config, error)

func (ConsulSource) Watch added in v1.0.2

func (c ConsulSource) Watch(ctx context.Context, onChange func(Config)) error

Watch 通过阻塞查询实现简单热更新

type ElasticsearchSinkConfig added in v1.0.2

type ElasticsearchSinkConfig struct {
	URL     string        `mapstructure:"url"`
	Index   string        `mapstructure:"index"`
	Timeout time.Duration `mapstructure:"timeout"`
}

type Environment

type Environment string
const (
	ENVIRONMENT Environment = "RUN_ENVIRONMENT"
)

type EnvironmentType

type EnvironmentType int
const (
	Dev EnvironmentType = iota
	Docker
	Test
)

func RunEnvironment

func RunEnvironment() EnvironmentType

type EtcdClientConfig added in v1.0.2

type EtcdClientConfig struct {
	Endpoints          []string `mapstructure:"endpoints"`
	Key                string   `mapstructure:"key"`
	Format             string   `mapstructure:"format"`
	Username           string   `mapstructure:"username"`
	Password           string   `mapstructure:"password"`
	DialTimeoutSeconds int      `mapstructure:"dial_timeout_seconds"`
}

type EtcdSource added in v1.0.2

type EtcdSource struct {
	Endpoints          []string
	Key                string
	Format             string // json|yaml|toml(默认 toml)
	Username           string
	Password           string
	DialTimeoutSeconds int
}

EtcdSource 从 etcd 读取配置,支持 json/yaml/toml 三种格式。 使用示例:

cfg := configs.NewCfgFrom(configs.EtcdSource{Endpoints: []string{"127.0.0.1:2379"}, Key: "/echo-admin/config", Format: "toml"})

func (EtcdSource) Load added in v1.0.2

func (e EtcdSource) Load(ctx context.Context) (Config, error)

func (EtcdSource) Watch added in v1.0.2

func (e EtcdSource) Watch(ctx context.Context, onChange func(Config)) error

Watch 支持 etcd 热更新:watch 指定 Key,变更后回调新的 Config

type FileSinkConfig added in v1.0.2

type FileSinkConfig struct {
	Filename   string `mapstructure:"filename"`
	MaxSize    int    `mapstructure:"max_size"`
	MaxBackups int    `mapstructure:"max_backups"`
	MaxAge     int    `mapstructure:"max_age"`
	Compress   bool   `mapstructure:"compress"`
	Format     string `mapstructure:"format"`
}

type FileSource added in v1.0.2

type FileSource struct{ Path string }

FileSource 本地文件来源,支持 json/yaml/toml(通过文件后缀识别)。

func (FileSource) Load added in v1.0.2

func (f FileSource) Load(ctx context.Context) (Config, error)

func (FileSource) Watch added in v1.0.2

func (f FileSource) Watch(ctx context.Context, onChange func(Config)) error

Watch 支持本地文件热更新,变更后回调新的 Config

type HotReloader added in v1.0.2

type HotReloader struct {
	// contains filtered or unexported fields
}

HotReloader 配置热重载器

func NewHotReloader added in v1.0.2

func NewHotReloader(store *Store, configPath string) (*HotReloader, error)

NewHotReloader 创建配置热重载器

func (*HotReloader) AddCallback added in v1.0.2

func (hr *HotReloader) AddCallback(callback func(*Config))

AddCallback 添加配置变化回调

func (*HotReloader) Close added in v1.0.2

func (hr *HotReloader) Close() error

Close 关闭热重载器

func (*HotReloader) Watch added in v1.0.2

func (hr *HotReloader) Watch(configPath string) error

Watch 开始监听配置文件变化

type JWTConfig

type JWTConfig struct {
	Secret    string   `mapstructure:"secret"`
	Expire    int      `mapstructure:"expire"`
	SkipPaths []string `mapstructure:"skip_paths"`
}

type KafkaConfig added in v1.0.2

type KafkaConfig struct {
	Brokers  []string `mapstructure:"brokers"`
	ClientID string   `mapstructure:"client_id"`
	Topic    string   `mapstructure:"topic"`
}

type Level

type Level int8
const (
	// DebugLevel logs are typically voluminous, and are usually disabled in
	// production.
	DebugLevel Level = iota + 1
	// ProsecutionLevel InfoLevel is the default logging priority.
	ProsecutionLevel
)

type LogConfig

type LogConfig struct {
	Level  string `mapstructure:"level"`
	Format string `mapstructure:"format"`

	Console ConsoleSinkConfig `mapstructure:"console"`
	File    FileSinkConfig    `mapstructure:"file"`

	Elasticsearch ElasticsearchSinkConfig `mapstructure:"elasticsearch"`
	Loki          LokiSinkConfig          `mapstructure:"loki"`
}

type LokiSinkConfig added in v1.0.2

type LokiSinkConfig struct {
	URL     string            `mapstructure:"url"`
	Labels  map[string]string `mapstructure:"labels"`
	Timeout time.Duration     `mapstructure:"timeout"`
}

type Mongodb

type Mongodb struct {
	Host     string `mapstructure:"host"`
	Port     string `mapstructure:"port"`
	User     string `mapstructure:"user"`
	Password string `mapstructure:"password"`
	DataBase string `mapstructure:"database"`
}

type MysqlConfig

type MysqlConfig struct {
	DockerHost   string `mapstructure:"docker_host"`
	Host         string `mapstructure:"host"`
	Port         string `mapstructure:"port"`
	User         string `mapstructure:"user"`
	Password     string `mapstructure:"password"`
	MaxOpenConns int    `mapstructure:"max_open_conns"`
	MaxIdleConns int    `mapstructure:"max_idle_conns"`
	Database     string `mapstructure:"database"`
}

type RedisConfig

type RedisConfig struct {
	Host     string `mapstructure:"host"`
	Port     string `mapstructure:"port"`
	Password string `mapstructure:"password"`
	Database int    `mapstructure:"database"`
}

type Source added in v1.0.2

type Source interface {
	Load(ctx context.Context) (Config, error)
}

Source 抽象:配置来源(本地文件、远程配置中心等)。 实现方直接返回完整的 Config,便于从任意介质填充(文件/etcd/http 等)。

type Store added in v1.0.2

type Store struct {
	// contains filtered or unexported fields
}

Store 提供原子读取/更新配置的能力,用于热更新场景

func NewStore added in v1.0.2

func NewStore(initial Config) *Store

func (*Store) Current added in v1.0.2

func (s *Store) Current() Config

func (*Store) RegisterConfigReloadCallback added in v1.0.2

func (s *Store) RegisterConfigReloadCallback(callback ConfigReloadCallback)

RegisterConfigReloadCallback 注册配置重载回调

func (*Store) Subscribe added in v1.0.2

func (s *Store) Subscribe(key string) <-chan Config

Subscribe 订阅配置更新;key 目前支持 "*"(全量),预留子树订阅扩展

func (*Store) Update added in v1.0.2

func (s *Store) Update(c Config)

type SystemConfig

type SystemConfig struct {
	Port  string `mapstructure:"port"`
	Level Level  `mapstructure:"level"`
	Env   string `mapstructure:"env"`
}

type WatchableSource added in v1.0.2

type WatchableSource interface {
	Source
	Watch(ctx context.Context, onChange func(Config)) error
}

WatchableSource 可选:支持热更新的配置源 Watch 应启动监听并在变更时回调返回新的 Config

Jump to

Keyboard shortcuts

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