zoukankan      html  css  js  c++  java
  • 使用viper 进行golang 应用的配置管理

    viper 是一个强大的golang 配置管理包,支持多种配置格式内容的读取,同时支持读取key/value 存储的数据
    而且不只是读取内容 ,同时也包含了,配置的写入操作。
    以下是一个简单的demo,同时使用docker 运行

    项目结构

    项目使用go mod 进行包的管理

    • 目录结构
     
    ├── Dockerfile
    ├── Makefile
    ├── README.md
    ├── cmd
    └── yaml
    └── main.go
    ├── docker-compose.yaml
    ├── go.mod
    └── go.sum

    代码说明

    • go.mod
    module github.com/rongfengliang/viper-app
    go 1.13
    require github.com/spf13/viper v1.4.0
     
    • 代码
      cmd/yaml/mai.go
    package main
    import (
     "bytes"
     "log"
     "github.com/spf13/viper"
    )
    func main() {
     viper.SetConfigType("yaml")
     var yamlExample = []byte(`
    Hacker: true
    name: steve
    hobbies:
    - skateboarding
    - snowboarding
    - go
    clothing:
    - jacket: leather
    - trousers: denim
    age: 35
    eyes: brown
    beard: true
    `)
     viper.AutomaticEnv()
     if err := viper.ReadConfig(bytes.NewBuffer(yamlExample)); err != nil {
      log.Fatal("some error", err)
     }
     log.Print(viper.GetString("name"))
     viper.Set("name", "demoapp rong")
     log.Print(viper.GetString("name"))
     var appname string = viper.GetString("APIKEY")
     log.Print(appname)
    }
    • Makefile
    build-app: clean make-dir build-mac build-linux build-windows
    clean:
     rm -rf build/*
    make-dir:
     mkdir -p build/{mac,linux,windows}
    build-mac:
     CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o build/mac ./cmd/...
    build-linux:
     CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/linux ./cmd/...
    build-windows:
     CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o build/windows ./cmd/...
    • dockerfile
    FROM alpine
    LABEL EMAIL="1141591465@qq.com"
    LABEL AUTHOR="dalongrong"
    WORKDIR /app
    COPY build/linux/yaml /app/yaml
    RUN chmod +x /app/yaml
    CMD [ "./yaml" ]
    • docker-compose 文件
    version: "3"
    services: 
       app:
         build: ./
         image: dalongrong/viper-app
         env_file: 
         - .env
     
     

    运行&&构建

    • 本地运行
    不包含环境变量的
    go run cmd/yam/main.go
    支持环境变量的
    APIKEY=rongfengliang go run cmd/yaml/main.go
    • 容器运行
      构建镜像
     
    docker-compose build

    启动

    docker-compose up

    效果

    Attaching to viper-golang_app_1
    app_1 | 2019/10/30 08:56:31 steve
    app_1 | 2019/10/30 08:56:31 demoapp rong
    app_1 | 2019/10/30 08:56:31 rongfengliang
    viper-golang_app_1 exited with code 0

    说明

    以上是一个简单的demo,实际功能很强大,还需要再学习官方文档了解

    参考资料

    https://github.com/spf13/viper
    https://github.com/rongfengliang/viper-learning

  • 相关阅读:
    209. Minimum Size Subarray Sum
    208. Implement Trie (Prefix Tree)
    207. Course Schedule
    206. Reverse Linked List
    205. Isomorphic Strings
    204. Count Primes
    203. Remove Linked List Elements
    201. Bitwise AND of Numbers Range
    199. Binary Tree Right Side View
    ArcGIS API for JavaScript 4.2学习笔记[8] 2D与3D视图同步
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/11766356.html
Copyright © 2011-2022 走看看