1 Star 0 Fork 0

qp84/aiui-iot-core

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
config
tool
encoder
loader
reader
secrets
source
env
README.md
env.go
env_test.go
options.go
watcher.go
file
flag
memory
changeset.go
noop.go
options.go
source.go
config.go
default.go
default_test.go
options.go
value.go
README.md
cache.go
config.go
database.go
gen.go
http.go
i18n.go
jwt.go
locker.go
logger.go
mqtt.go
option_nsq.go
option_redis.go
queue.go
ssl.go
facade
httpmvc
logger
msg
serrors
storage
utils
ws
.gitignore
LICENSE
go.mod
main_test.go
settings.template.yml
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
1个月前
Loading...
README

Env Source

The env source reads config from environment variables

Format

We expect environment variables to be in the standard format of FOO=bar

Keys are converted to lowercase and split on underscore.

Example

DATABASE_ADDRESS=127.0.0.1
DATABASE_PORT=3306

Becomes

{
    "database": {
        "address": "127.0.0.1",
        "port": 3306
    }
}

Prefixes

Environment variables can be namespaced so we only have access to a subset. Two options are available:

WithPrefix(p ...string)
WithStrippedPrefix(p ...string)

The former will preserve the prefix and make it a top level key in the config. The latter eliminates the prefix, reducing the nesting by one.

Example:

Given ENVs of:

APP_DATABASE_ADDRESS=127.0.0.1
APP_DATABASE_PORT=3306
VAULT_ADDR=vault:1337

and a source initialized as follows:

src := env.NewSource(
    env.WithPrefix("VAULT"),
    env.WithStrippedPrefix("APP"),
)

The resulting config will be:

{
    "database": {
        "address": "127.0.0.1",
        "port": 3306
    },
    "vault": {
        "addr": "vault:1337"
    }
}

New Source

Specify source with data

src := env.NewSource(
	// optionally specify prefix
	env.WithPrefix("GO_ADMIN"),
)

Load Source

Load the source into config

// Create new config
conf := config.NewConfig()

// Load env source
conf.Load(src)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/qp84/aiui-iot-core.git
git@gitee.com:qp84/aiui-iot-core.git
qp84
aiui-iot-core
aiui-iot-core
v1.0.2

搜索帮助

371d5123 14472233 46e8bd33 14472233