Use env lib, add PROXY_PORT ENV variable

This commit is contained in:
Sergey Bogatyrets
2018-06-13 22:34:35 +03:00
parent 803767b6f7
commit 678a8cc192
13 changed files with 1241 additions and 9 deletions
+35
View File
@@ -0,0 +1,35 @@
parsers
=======
This directory contains pre-built, custom parsers that can be used with `env.ParseWithFuncs`
to facilitate the parsing of envs that are not basic types.
Example Usage:
```golang
package main
import (
"fmt"
"log"
"net/url"
"github.com/caarlos0/env"
"github.com/caarlos0/env/parsers"
)
type config struct {
ExampleURL url.URL `env:"EXAMPLE_URL" envDefault:"https://google.com"`
}
func main() {
cfg := config{}
if err := env.ParseWithFuncs(&cfg, env.CustomParsers{
parsers.URLType: parsers.URLFunc,
}); err != nil {
log.Fatal("Unable to parse envs: ", err)
}
fmt.Printf("Scheme: %v Host: %v\n", cfg.ExampleURL.Scheme, cfg.ExampleURL.Host)
}
```
+23
View File
@@ -0,0 +1,23 @@
// Package parsers contains custom parser funcs for common, non-built-in types
package parsers
import (
"fmt"
"net/url"
"reflect"
)
var (
// URLType is a helper var that represents the `reflect.Type`` of `url.URL`
URLType = reflect.TypeOf(url.URL{})
)
// URLFunc is a basic parser for the url.URL type that should be used with `env.ParseWithFuncs()`
func URLFunc(v string) (interface{}, error) {
u, err := url.Parse(v)
if err != nil {
return nil, fmt.Errorf("Unable to complete URL parse: %v", err)
}
return *u, nil
}