![viper logo](https://cloud.githubusercontent.com/assets/173412/10886745/998df88a-8151-11e5-9448-4736db51020d.png)
Go configuration with fangs!
Many Go projects are built using Viper including:
* [Hugo](http://gohugo.io)
* [EMC RexRay](http://rexray.readthedocs.org/en/stable/)
* [Imgur’s Incus](https://github.com/Imgur/incus)
* [Nanobox](https://github.com/nanobox-io/nanobox)/[Nanopack](https://github.com/nanopack)
* [Docker Notary](https://github.com/docker/Notary)
* [BloomApi](https://www.bloomapi.com/)
* [doctl](https://github.com/digitalocean/doctl)
* [Clairctl](https://github.com/jgsqware/clairctl)
[![Build Status](https://travis-ci.org/spf13/viper.svg)](https://travis-ci.org/spf13/viper) [![Join the chat at https://gitter.im/spf13/viper](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/spf13/viper?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![GoDoc](https://godoc.org/github.com/spf13/viper?status.svg)](https://godoc.org/github.com/spf13/viper)
## What is Viper?
Viper is a complete configuration solution for Go applications including 12-Factor apps. It is designed
to work within an application, and can handle all types of configuration needs
and formats. It supports:
* setting defaults
* reading from JSON, TOML, YAML, HCL, and Java properties config files
* live watching and re-reading of config files (optional)
* reading from environment variables
* reading from remote config systems (etcd or Consul), and watching changes
* reading from command line flags
* reading from buffer
* setting explicit values
Viper can be thought of as a registry for all of your applications
configuration needs.
## Why Viper?
When building a modern application, you don’t want to worry about
configuration file formats; you want to focus on building awesome software.
Viper is here to help with that.
Viper does the following for you:
1. Find, load, and unmarshal a configuration file in JSON, TOML, YAML, HCL, or Java properties formats.
2. Provide a mechanism to set default values for your different
configuration options.
3. Provide a mechanism to set override values for options specified through
command line flags.
4. Provide an alias system to easily rename parameters without breaking existing
code.
5. Make it easy to tell the difference between when a user has provided a
command line or config file which is the same as the default.
Viper uses the following precedence order. Each item takes precedence over the
item below it:
* explicit call to Set
* flag
* env
* config
* key/value store
* default
Viper configuration keys are case insensitive.
## Putting Values into Viper
### Establishing Defaults
A good configuration system will support default values. A default value is not
required for a key, but it’s useful in the event that a key hasn’t been set via
config file, environment variable, remote configuration or flag.
Examples:
```go
viper.SetDefault("ContentDir", "content")
viper.SetDefault("LayoutDir", "layouts")
viper.SetDefault("Taxonomies", map[string]string{"tag": "tags", "category": "categories"})
```
### Reading Config Files
Viper requires minimal configuration so it knows where to look for config files.
Viper supports JSON, TOML, YAML, HCL, and Java Properties files. Viper can search multiple paths, but
currently a single Viper instance only supports a single configuration file.
Viper does not default to any configuration search paths leaving defaults decision
to an application.
Here is an example of how to use Viper to search for and read a configuration file.
None of the specific paths are required, but at least one path should be provided
where a configuration file is expected.
```go
viper.SetConfigName("config") // name of config file (without extension)
viper.AddConfigPath("/etc/appname/") // path to look for the config file in
viper.AddConfigPath("$HOME/.appname") // call multiple times to add many search paths
viper.AddConfigPath(".") // optionally look for config in the working directory
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
```
### Watching and re-reading config files
Viper supports the ability to have your application live read a config file while running.
Gone are the days of needing to restart a server to have a config take effect,
viper powered applications can read an update to a config file while running and
not miss a beat.
Simply tell the viper instance to watchConfig.
Optionally you can provide a function for Viper to run each time a change occurs.
**Make sure you add all of the configPaths prior to calling `WatchConfig()`**
```go
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config file changed:", e.Name)
})
```
### Reading Config from io.Reader
Viper predefines many configuration sources such as files, environment
variables, flags, and remote K/V store, but you are not bound to them. You can
also implement your own required configuration source and feed it to viper.
```go
viper.SetConfigType("yaml") // or viper.SetConfigType("YAML")
// any approach to require this configuration into your program.
var yamlExample = []byte(`
Hacker: true
name: steve
hobbies:
- skateboarding
- snowboarding
- go
clothing:
jacket: leather
trousers: denim
age: 35
eyes : brown
beard: true
`)
viper.ReadConfig(bytes.NewBuffer(yamlExample))
viper.Get("name") // this would be "steve"
```
### Setting Overrides
These could be from a command line flag, or from your own application logic.
```go
viper.Set("Verbose", true)
viper.Set("LogFile", LogFile)
```
### Registering and Using Aliases
Aliases permit a single value to be referenced by multiple keys
```go
viper.RegisterAlias("loud", "Verbose")
viper.Set("verbose", true) // same result as next line
viper.Set("loud", true) // same result as prior line
viper.GetBool("loud") // true
viper.GetBool("verbose") // true
```
### Working with Environment Variables
Viper has full support for environment variables. This enables 12 factor
applications out of the box. There are four methods that exist to aid working
with ENV:
* `AutomaticEnv()`
* `BindEnv(string...) : error`
* `SetEnvPrefix(string)`
* `SetEnvKeyReplacer(string...) *strings.Replacer`
_When working with ENV variables, it’s important to recognize that Viper
treats ENV variables as case sensitive._
Viper provides a mechanism to try to ensure that ENV variables are unique. By
using `SetEnvPrefix`, you can tell Viper to use a prefix while reading from
the environment variables. Both `BindEnv` and `AutomaticEnv` will use this
prefix.
`BindEnv` takes one or two parameters. The first parameter is the key name, the
second is the name of the environment variable. The name of the environment
variable is case sensitive. If the ENV variable name is not provided, then
Viper will automatically assume that the key name matches the ENV variable name,
but the ENV variable is IN ALL CAPS. When you explicitly provide the ENV
variable name, it **does not** automatically add the prefix.
One important thing to recognize when working with ENV variables is that the
value will be read each time it is accessed. Viper does not fix the value when
the `BindEnv` is called.
`AutomaticEnv` is a powerful helper especially when combined with
`SetEnvPrefix`. When called, Viper will check for an environment variable any
time a `viper.Get` request is made. It will apply the following rules. It will
check for a environment variable with a name matching the key uppercased and
prefixed with the `EnvPrefix` if set.
`SetEnvKeyReplacer` allows you to use a `strings.Replacer` object to rewrite Env
keys to an extent. This is useful if you want to use `-` or something in your
`Get()` calls, but want your environmental variables to use `_` delimiters. An
example of using it can be found in `viper_
没有合适的资源?快使用搜索试试~ 我知道了~
蓝鲸智云PaaS平台是一个开放式的开发平台,让开发者可以方便快捷地创建、开发、部署和管理SaaS应用
共2000个文件
py:660个
go:457个
js:370个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 127 浏览量
2023-12-18
13:12:09
上传
评论
收藏 14.44MB ZIP 举报
温馨提示
蓝鲸智云PaaS平台是一个开放式的开发平台,让开发者可以方便快捷地创建、开发、部署和管理SaaS应用。本次开源的是蓝鲸智云PaaS平台社区版(BlueKing PaaS Community Edition),它提供了应用引擎、前后台开发框架、API网关、调度引擎、统一登录、公共组件等模块,帮助用户快速、低成本、免运维地构建支撑工具和运营系统(统称为SaaS应用),它为一个SaaS应用从创建到部署,再到后续的维护管理提供了完善的自动化和自助化服务,从而使开发者可以集中精力关注SaaS应用的逻辑开发
资源推荐
资源详情
资源评论
收起资源包目录
蓝鲸智云PaaS平台是一个开放式的开发平台,让开发者可以方便快捷地创建、开发、部署和管理SaaS应用 (2000个子文件)
gccgo_c.c 1KB
bk.css 141KB
base.css 125KB
base.css 123KB
bk.min.css 122KB
bootstrap.css 118KB
bootstrap.css 118KB
bootstrap.min.css 118KB
bootstrap3.0.0.css 116KB
bootstrap3.0.0.css 116KB
base.min.css 102KB
base.min.css 101KB
bootstrap.min.css 98KB
bootstrap.min.css 98KB
button.css 65KB
button.css 65KB
jquery-ui.css 32KB
jquery-ui.min.css 27KB
docs.css 23KB
docs.css 23KB
extend.css 21KB
extend.css 21KB
select2.css 19KB
select2.css 19KB
jquery-ui.theme.css 17KB
bootstrap-responsive.min.css 16KB
jquery-ui.structure.css 15KB
select2.css 15KB
bootstrap-theme.css 15KB
bootstrap-theme.css 15KB
base.css 14KB
jquery-ui.theme.min.css 14KB
md2html.css 14KB
md_render.css 13KB
bootstrap-theme.min.css 13KB
bootstrap-theme.min.css 13KB
login.css 13KB
jquery-ui.structure.min.css 12KB
paas_guide.css 12KB
style.css 11KB
iconfont.css 11KB
iconfont.css 11KB
paas_guide.min.css 11KB
style.css 10KB
widgets.css 10KB
login.min.css 9KB
md_render.min.css 9KB
md2html.min.css 9KB
bootstrap-overrides.css 8KB
bootstrap-overrides.css 8KB
layout_head.css 8KB
layout_head.css 8KB
index.css 7KB
dialog.css 7KB
style.min.css 7KB
layout_head.min.css 6KB
layout_head.min.css 6KB
forms.css 6KB
paas_monitor.css 6KB
base.css 6KB
guide_newbie.css 6KB
index.min.css 6KB
paas_release.css 5KB
changelists.css 5KB
guide_newbie.min.css 5KB
base.min.css 5KB
paas_release.min.css 5KB
index.css 5KB
paas_profile.css 4KB
paas_monitor.min.css 4KB
sFlow.css 4KB
paas_profile.min.css 4KB
index.min.css 4KB
rtl.css 4KB
paas_app_env.css 4KB
sFlow.min.css 3KB
select2-bootstrap.css 3KB
select2-bootstrap.css 3KB
select2-bootstrap.css 3KB
login.css 3KB
metisMenu.css 3KB
paas_app_env.min.css 3KB
login.min.css 3KB
metisMenu.min.css 3KB
ui-dialog.css 2KB
ui-dialog.css 2KB
search.css 2KB
ui-dialog.css 2KB
prettify.css 2KB
search.min.css 2KB
bootstrap-duallistbox.min.css 2KB
bootstrap-duallistbox.min.css 2KB
weixin.min.css 2KB
paas_app.css 2KB
select2-2.0.css 2KB
helper.css 1KB
paas_app.min.css 1KB
paas_saas.css 1KB
helper.min.css 1KB
paas_saas.min.css 1KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
Java程序员-张凯
- 粉丝: 1w+
- 资源: 6649
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功