반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- Kopf
- Kubernetes 인증
- keda
- 오퍼레이터
- Litmus
- Continuous Deployment
- gitops
- Kubeflow
- Kubernetes
- Pulumi
- opensearch
- Model Serving
- opentelemetry
- blue/green
- MLflow
- mlops
- serving
- operator
- argocd
- nginx ingress
- kubernetes operator
- CI/CD
- xdp
- Argo
- eBPF
- seldon core
- 카오스 엔지니어링
- knative
- tekton
- CANARY
Archives
- Today
- Total
Kubernetes 이야기
Viper 본문
반응형
Viper 는 Go에서 Configuration을 관리하기 위한 라이브러리이다. 응용 프로그램에 대한 기본값을 설정하거나 다른 파일 유형에서 구성 변수를 로드해야 하는 경우가 많이 있으며 이 경우 Viper를 사용하는 것이 매우 유용할 수 있다. 구성 변수를 실시간으로 읽고, 플래그로 작업하고, 마샬링 및 언마샬링할 수도 있다.
https://github.com/spf13/viper
GitHub - spf13/viper: Go configuration with fangs
Go configuration with fangs. Contribute to spf13/viper development by creating an account on GitHub.
github.com
- 기본값 설정
- JSON, TOML, YAML, HCL, envfile 및 Java 속성 구성 파일에서 읽기
- 구성 파일의 실시간 시청 및 다시 읽기 (선택 사항)
- 환경 변수에서 읽기
- 원격 구성 시스템(etcd 또는 Consul)에서 읽기 및 변경 사항 보기
- 명령줄 플래그에서 읽기
- 버퍼에서 읽기
- 명시적 값 설정
설치
# go get github.com/spf13/viper
사용법
.env 파일에서 읽기
.env내용
SERVER=example.com
PORT=80
APIKEY=1234
main.go
package main
import (
"fmt"
"github.com/spf13/viper"
)
func main() {
viper.SetConfigFile(".env")
viper.ReadInConfig()
fmt.Println(viper.Get("PORT"))
}
실행결과
# go run main.go
example.com
80
1234
JSON 파일에서 읽기
json 파일 (./configs/config )
{
"host": {
"server": "localhost",
"port": "80"
},
"apikey": "1234"
}
main.go
package main
import (
"fmt"
"github.com/spf13/viper"
)
func main() {
viper.AddConfigPath("./configs")
viper.SetConfigName("config") // Register config file name (no extension)
viper.SetConfigType("json") // Look for specific type
viper.ReadInConfig()
fmt.Println(viper.Get("host.server"))
fmt.Println(viper.Get("host.port"))
fmt.Println(viper.Get("APIKEY"))
}
실행결과
go run main.go
localhost
80
1234
WatchConfig() 사용하기
Viper는 방법을 통해 구성 파일의 변경 사항을 실시간으로 읽을 수 있는 기능을 제공한다. 다음은 플랫폼 간 시스템 알림 패키지인 fsnotify 패키지 로 구현된 간단한 예이다.
package main
import (
"fmt"
"github.com/spf13/viper"
"github.com/fsnotify/fsnotify"
)
func main() {
viper.AddConfigPath("./configs")
viper.SetConfigName("config") // Register config file name (no extension)
viper.SetConfigType("json") // Look for specific type
viper.ReadInConfig()
fmt.Println(viper.Get("host.server"))
fmt.Println(viper.Get("host.port"))
fmt.Println(viper.Get("APIKEY"))
change := make(chan int)
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config file changed:", e.Name)
change <- 1
})
for {
select {
case <-change:
viper.ReadInConfig()
fmt.Println(viper.Get("host.server"))
fmt.Println(viper.Get("host.port"))
fmt.Println(viper.Get("APIKEY"))
}
}
}
실행결과
# go run main.go
localhost
80
12345
Config file changed: D:\develop\Go\test1\configs\config
localhost
80
123456
반응형
'개발 > go' 카테고리의 다른 글
Resty ( Http & Rest Client library) (0) | 2022.08.26 |
---|---|
GoDS ( Go Data Structures ) (0) | 2022.08.24 |
cobra library 사용법 (0) | 2022.08.22 |
모둘과 패키지 (0) | 2022.08.22 |
Kubebuilder ( Kubernetes operator ) (0) | 2022.08.17 |
Comments