-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_example_test.go
50 lines (41 loc) · 1.08 KB
/
config_example_test.go
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"fmt"
"os"
"strings"
"github.com/iver-wharf/wharf-core/pkg/config"
)
func buildTestConfig(configYAML string) (Config, error) {
var builder = config.NewBuilder(DefaultConfig)
builder.AddEnvironmentVariables("WHARF")
builder.AddConfigYAML(strings.NewReader(configYAML))
var config Config
err := builder.Unmarshal(&config)
return config, err
}
func ExampleConfig() {
var configYAML = `
http:
cors:
allowAllOrigins: true
db:
username: postgres
password: secretpass
`
// Prefix of WHARF_ must be prepended to all environment variables
os.Setenv("WHARF_HTTP_BINDADDRESS", "0.0.0.0:8123")
var config, err = buildTestConfig(configYAML)
if err != nil {
fmt.Println("Error reading config:", err)
return
}
fmt.Println("Allow any CORS?", config.HTTP.CORS.AllowAllOrigins)
fmt.Println("HTTP bind address:", config.HTTP.BindAddress)
fmt.Println("DB username:", config.DB.Username)
fmt.Println("DB password:", config.DB.Password)
// Output:
// Allow any CORS? true
// HTTP bind address: 0.0.0.0:8123
// DB username: postgres
// DB password: secretpass
}