Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Parse config values from environment variables.

License

NotificationsYou must be signed in to change notification settings

tike/envcnf

Repository files navigation

Parse values/settings from environment variables into arbitrarily nested/complexgolang data structures of any type with a single function call.

It is built to streamline the usage ofdirenvfor local/development needs and unix startup scripts for your webservice inthe production environment, while keeping your time/coding footprints small.:rocket:

The problem

Passing configuration values to a daemon, e.g. a webserver, via environementvariables can be tedious with go's onboard means and available libraries arelimited in the types they support and in their handling of compositetypes, like structs, map and/or slices and especially arbitrarily nestedcombinations of these.

The solution

Package envcnf allows you to pass your configuration to your application viaenvironment variables comfortably, reliably and without (practical) limitations.

How

  1. Name your env vars to match your config type's field names,case conversion to match all lower or all upper case is supported.You can of course use a common prefix to set them apart from"regular" env vars and thous even have different configuration sets loaded atthe same time, designating each set by a different prefix.

  2. Usedirenv to maintain and organiseyour configuration in shell script like files or just source them by hand.

  3. Parse the configuration from the environment variables into your configstruct, map, slice or any (however deeply nested and complex) combinationthereof with1 line of code. See example right below ;-)

Example:

Let the following be the data structs that hold your app's config:

package configtype NetCnf struct {  Addr  string  HTTPS bool}type MySection struct {  Values []uint64}type MyCnf struct {  Environment string  Listen      map[string]NetCnf  ChRoot      string  MyFoo       MySection}

Your configuration file might then look like this:

# put this into your `.envrc`, if you're using `direnv`# or add an invocation of your exe at the end and use it as a run script# or `source` it into your shell and run your exe manually# config values...export ACME-CORP_Environment=productionexport ACME-CORP_Listen_internal_Addr=127.0.0.1:80export ACME-CORP_Listen_internal_HTTPS=falseexport ACME-CORP_Listen_public_Addr=1.2.3.4:443export ACME-CORP_Listen_public_HTTPS=trueexport ACME-CORP_ChRoot=/var/emptyexport ACME-CORP_MyFoo_Values_0=3export ACME-CORP_MyFoo_Values_1=2export ACME-CORP_MyFoo_Values_2=1export ACME-CORP_MyFoo_Values_3=0# to make this a run script, rather than a mere config, just add this one line# /path/to/myapp

Now how do you parse those env vars into those structs?

package mainimport (  "github.com/tike/envcnf"  "path/to/config")func main(){  var cnf config.MyCnf  if err := envcnf.Parse(&cnf, "ACME-CORP", "_", envcnf.NoConv); err != nil {    fmt.Println("parsing config:", err)    return  }  fmt.Printf("%#v\n",cnf)}

Output will look like this:

config.MyCnf{  Environment:"production",  Listen:map[string]config.NetCnf{    "internal":config.NetCnf{      Addr:"127.0.0.1:80",      HTTPS:false    },    "public":config.NetCnf{      Addr:"1.2.3.4:443",      HTTPS:true    }  },  ChRoot:"/var/empty",  MyFoo:config.MySection{    Values:[]uint64{0x3, 0x2, 0x1, 0x0}  }}

[8]ページ先頭

©2009-2025 Movatter.jp