chore: Add ConfigX library for managing application configurations
This commit is contained in:
parent
448f92a38c
commit
e1de6c5c26
38
README.md
38
README.md
@ -1,2 +1,38 @@
|
||||
# configx
|
||||
# ConfigX
|
||||
|
||||
ConfigX 是一个 Go 语言的配置库,提供了一种简单的方式来读取和管理应用程序的配置信息。
|
||||
|
||||
## 特性
|
||||
|
||||
- 支持多种配置文件格式(YAML、JSON、TOML 等)
|
||||
- 环境变量支持和集成
|
||||
- 配置热重载
|
||||
- 类型安全的配置访问
|
||||
- 支持不同环境的配置文件
|
||||
- 灵活的初始化选项
|
||||
- 线程安全
|
||||
|
||||
## 安装
|
||||
|
||||
使用 go get 安装 ConfigX:
|
||||
|
||||
```bash
|
||||
go get gitea.cccvno1.com/chenchi/configx
|
||||
|
||||
```
|
||||
|
||||
## 快速开始
|
||||
|
||||
基本使用方法参考 [examples](https://gitea.cccvno1.com/chenchi/configx/tree/master/examples) 目录。
|
||||
|
||||
## 部分特性说明
|
||||
|
||||
### 环境变量支持
|
||||
可以通过环境变量来覆盖配置文件中的配置项,环境变量的命名规则为 `PREFIX_KEY`,其中 `PREFIX` 为环境变量的前缀,默认值为 `APP`,`KEY` 为配置项的名称。例如 `APP_SERVER_PORT=9000` 可以覆盖配置文件中的 `server.port` 配置项。
|
||||
|
||||
### 不同环境的配置文件
|
||||
支持根据不同的环境加载不同的配置文件,加载规则为 `config.{env}.yaml`,其中 `{env}` 为环境名称,例如 `config.dev.yaml`。`env` 的值可以通过环境变量 `APP_ENV` 来指定,默认值为 `dev`。
|
||||
|
||||
### 配置热重载
|
||||
通过 `WithAutoReload` 方法可以开启配置热重载功能,当配置文件发生变化时,会自动重新加载配置。
|
||||
启动热重载功能后,可以为某一个配置项设置回调函数,当配置项发生变化时,回调函数会被调用。
|
||||
|
213
configx.go
Normal file
213
configx.go
Normal file
@ -0,0 +1,213 @@
|
||||
// Package configx 提供了一个灵活且健壮的配置管理解决方案
|
||||
// 它支持多种配置文件格式、环境变量、热重载和类型安全的配置值访问
|
||||
package configx
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// Config 表示配置管理器
|
||||
type Config struct {
|
||||
v *viper.Viper
|
||||
data interface{}
|
||||
configFile string
|
||||
environment string
|
||||
autoReload bool
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// Option 定义了配置选项的函数类型
|
||||
type Option func(*Config) error
|
||||
|
||||
// New 创建并初始化一个新的 Config 实例
|
||||
func New(data interface{}, opts ...Option) (*Config, error) {
|
||||
c := &Config{
|
||||
v: viper.New(),
|
||||
data: data,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
if err := opt(c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if err := c.initialize(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// WithConfigFile 设置配置文件路径
|
||||
func WithConfigFile(file string) Option {
|
||||
return func(c *Config) error {
|
||||
c.configFile = file
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithEnvFile 设置 .env 文件路径
|
||||
func WithEnvFile(file string) Option {
|
||||
return func(c *Config) error {
|
||||
return godotenv.Load(file)
|
||||
}
|
||||
}
|
||||
|
||||
// WithAutoReload 设置是否自动重新加载配置文件
|
||||
func WithAutoReload(enabled bool) Option {
|
||||
return func(c *Config) error {
|
||||
c.autoReload = enabled
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithEnvPrefix 设置环境变量前缀
|
||||
func WithEnvPrefix(prefix string) Option {
|
||||
return func(c *Config) error {
|
||||
c.v.SetEnvPrefix(prefix)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// initialize 初始化 Config 实例
|
||||
func (c *Config) initialize() error {
|
||||
// 从 .env 文件中获取环境变量
|
||||
if c.environment == "" {
|
||||
c.environment = os.Getenv("APP_ENV")
|
||||
if c.environment == "" {
|
||||
c.environment = "dev"
|
||||
}
|
||||
}
|
||||
c.v.Set("environment", c.environment)
|
||||
|
||||
// 如果没有设置配置文件,根据环境选择默认配置文件
|
||||
if c.configFile == "" {
|
||||
c.configFile = fmt.Sprintf("config.%s.yaml", c.environment)
|
||||
}
|
||||
|
||||
c.v.SetConfigType(filepath.Ext(c.configFile)[1:])
|
||||
c.v.SetConfigFile(c.configFile)
|
||||
|
||||
// 首先读取配置文件
|
||||
if err := c.v.ReadInConfig(); err != nil {
|
||||
return fmt.Errorf("failed to read config file: %w", err)
|
||||
}
|
||||
|
||||
// 然后设置从环境变量读取配置
|
||||
c.v.AutomaticEnv()
|
||||
|
||||
// 如果没有设置环境变量前缀,使用默认前缀
|
||||
if c.v.GetEnvPrefix() == "" {
|
||||
c.v.SetEnvPrefix("APP")
|
||||
}
|
||||
|
||||
c.v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
|
||||
if err := c.unmarshal(); err != nil {
|
||||
return fmt.Errorf("failed to unmarshal config: %w", err)
|
||||
}
|
||||
|
||||
// 监听配置文件变化
|
||||
if c.autoReload {
|
||||
c.v.WatchConfig()
|
||||
c.v.OnConfigChange(func(in fsnotify.Event) {
|
||||
c.Reload()
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// unmarshal 将配置解析到数据结构中
|
||||
func (c *Config) unmarshal() error {
|
||||
return c.v.Unmarshal(c.data, func(dc *mapstructure.DecoderConfig) {
|
||||
dc.TagName = "config"
|
||||
})
|
||||
}
|
||||
|
||||
// Get 返回整个配置结构
|
||||
func (c *Config) Get() interface{} {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
return c.data
|
||||
}
|
||||
|
||||
// GetEnvironment 返回当前环境
|
||||
func (c *Config) GetEnvironment() string {
|
||||
return c.environment
|
||||
}
|
||||
|
||||
// GetString 返回与键关联的值作为字符串
|
||||
func (c *Config) GetString(key string) string {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
return c.v.GetString(key)
|
||||
}
|
||||
|
||||
// GetInt 返回与键关联的值作为整数
|
||||
func (c *Config) GetInt(key string) int {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
return c.v.GetInt(key)
|
||||
}
|
||||
|
||||
// GetBool 返回与键关联的值作为布尔值
|
||||
func (c *Config) GetBool(key string) bool {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
return c.v.GetBool(key)
|
||||
}
|
||||
|
||||
// GetFloat64 返回与键关联的值作为 float64
|
||||
func (c *Config) GetFloat64(key string) float64 {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
return c.v.GetFloat64(key)
|
||||
}
|
||||
|
||||
// GetStringSlice 返回与键关联的值作为字符串切片
|
||||
func (c *Config) GetStringSlice(key string) []string {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
return c.v.GetStringSlice(key)
|
||||
}
|
||||
|
||||
// Reload 重新加载配置
|
||||
func (c *Config) Reload() error {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
err := c.v.ReadInConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.unmarshal()
|
||||
}
|
||||
|
||||
// Watchfield 监听配置字段变化
|
||||
func (c *Config) WatchField(key string, callback func(newValue interface{})) {
|
||||
if !c.autoReload {
|
||||
return
|
||||
}
|
||||
|
||||
old := c.v.Get(key)
|
||||
|
||||
c.v.OnConfigChange(func(in fsnotify.Event) {
|
||||
newValue := c.v.Get(key)
|
||||
if reflect.DeepEqual(old, newValue) || newValue == nil {
|
||||
return
|
||||
}
|
||||
|
||||
callback(newValue)
|
||||
})
|
||||
}
|
2
example/.env
Normal file
2
example/.env
Normal file
@ -0,0 +1,2 @@
|
||||
APP_SERVER_PORT=9000
|
||||
|
8
example/config.yaml
Normal file
8
example/config.yaml
Normal file
@ -0,0 +1,8 @@
|
||||
server:
|
||||
port: 8080
|
||||
host: localhost
|
||||
database:
|
||||
dsn: "user:password@tcp(localhost:3306)/dbname"
|
||||
features:
|
||||
enable_cache: false
|
||||
log_level: debug
|
51
example/example.go
Normal file
51
example/example.go
Normal file
@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"gitea.cccvno1.com/chenchi/configx"
|
||||
)
|
||||
|
||||
type AppConfig struct {
|
||||
Server struct {
|
||||
Port int `config:"port"`
|
||||
Host string `config:"host"`
|
||||
} `config:"server"`
|
||||
Database struct {
|
||||
DSN string `config:"dsn"`
|
||||
} `config:"database"`
|
||||
Features struct {
|
||||
EnableCache bool `config:"enable_cache"`
|
||||
} `config:"features"`
|
||||
LogLevel string `config:"log_level"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
var cfg AppConfig
|
||||
c, err := configx.New(&cfg,
|
||||
configx.WithEnvFile(".env"),
|
||||
configx.WithConfigFile("config.yaml"),
|
||||
configx.WithAutoReload(true),
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to load config: %v", err)
|
||||
}
|
||||
|
||||
printConfig(c)
|
||||
|
||||
// Watch for changes in the log level
|
||||
c.WatchField("log_level", func(newValue interface{}) {
|
||||
fmt.Printf("Log level changed to: %v\n", newValue)
|
||||
printConfig(c)
|
||||
})
|
||||
|
||||
select {}
|
||||
}
|
||||
|
||||
func printConfig(cfg *configx.Config) {
|
||||
fmt.Printf("Server: %s:%d\n", cfg.GetString("server.host"), cfg.GetInt("server.port"))
|
||||
fmt.Printf("Database DSN: %s\n", cfg.GetString("database.dsn"))
|
||||
fmt.Printf("Features: EnableCache=%v\n", cfg.GetBool("features.enable_cache"))
|
||||
fmt.Printf("Log level: %s\n", cfg.GetString("log_level"))
|
||||
}
|
31
go.mod
Normal file
31
go.mod
Normal file
@ -0,0 +1,31 @@
|
||||
module gitea.cccvno1.com/chenchi/configx
|
||||
|
||||
go 1.23.0
|
||||
|
||||
require github.com/fsnotify/fsnotify v1.7.0
|
||||
|
||||
require (
|
||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||
github.com/magiconair/properties v1.8.7 // indirect
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
|
||||
github.com/sagikazarmark/locafero v0.4.0 // indirect
|
||||
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
|
||||
github.com/sourcegraph/conc v0.3.0 // indirect
|
||||
github.com/spf13/afero v1.11.0 // indirect
|
||||
github.com/spf13/cast v1.6.0 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/subosito/gotenv v1.6.0 // indirect
|
||||
go.uber.org/atomic v1.9.0 // indirect
|
||||
go.uber.org/multierr v1.9.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/spf13/viper v1.19.0
|
||||
golang.org/x/sys v0.18.0 // indirect
|
||||
)
|
58
go.sum
Normal file
58
go.sum
Normal file
@ -0,0 +1,58 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
|
||||
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
|
||||
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
|
||||
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
|
||||
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
|
||||
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
|
||||
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
|
||||
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
|
||||
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
|
||||
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
|
||||
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
|
||||
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
|
||||
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
|
||||
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
|
||||
github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
|
||||
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
|
||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI=
|
||||
github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
|
||||
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
|
||||
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
|
||||
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
|
||||
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
|
||||
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
|
||||
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
|
||||
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
|
||||
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
Loading…
Reference in New Issue
Block a user