https://github.com/core-go/config
https://github.com/core-go/config
conf config configuration
Last synced: 6 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/core-go/config
- Owner: core-go
- Created: 2019-11-14T02:52:59.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-09-28T11:32:25.000Z (almost 2 years ago)
- Last Synced: 2025-04-29T01:42:32.111Z (about 1 year ago)
- Topics: conf, config, configuration
- Language: Go
- Homepage:
- Size: 50.8 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# config
- Support to load the configurations
- Support to merge the environment variables to the configurations
- Support to override the configurations based on the environment (For example: SIT environment, UAT environment)
## Installation
Please make sure to initialize a Go module before installing core-go/config:
```shell
go get -u github.com/core-go/config
```
Import:
```go
import "github.com/core-go/config"
```
## Example
We have 2 config files, which are put into "configs" directory
- the default config file: config.yaml
- for SIT environment: config-sit.yaml
#### config.yaml
```yaml
ldap:
server: localhost:389
binding_format: uid=%s,ou=users,dc=sample,dc=com,dc=vn
redis_url: redis://localhost:6379
```
In the SIT environment, we just override the configuration of ldap server and redis server:
#### config-sit.yaml
```yaml
ldap:
server: sit-server:389
redis_url: redis://redis-sit:6379
```
```go
type LDAPConfig struct {
Server string `mapstructure:"server"`
BindingFormat string `mapstructure:"binding_format"`
}
type RootConfig struct {
Server app.ServerConfig `mapstructure:"server"`
Ldap LDAPConfig `mapstructure:"ldap"`
RedisClient string `mapstructure:"redis_client"`
}
func main() {
env := os.Getenv("ENV")
var cfg RootConfig
// "authentication" is the directory, which contains source code
// "configs" is the directory, which contains config.yaml and config-sit.yaml
config.LoadConfigWithEnv("authentication", "configs", env, &cfg, "config")
log.Println("config ", cfg)
var conf2 RootConfig
config.Load(&conf2, "configs/config")
log.Println("config2 ", conf2)
}
```