-
Notifications
You must be signed in to change notification settings - Fork 451
/
listenaddress.go
126 lines (111 loc) · 4.27 KB
/
listenaddress.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// Package listenaddress provides a configuration struct for resolving
// a listen address from YAML.
//
// Deprecation notice:
// The environment resolution behavior of this
// class has largely been superseded
// by config environment interpolation in go.uber.org/config (see config/README.md for
// details). Instead of:
//
// listenAddress:
// type: environment
// envVarListenPort: MY_PORT_VAR
//
// one can do:
//
// listenAddress:
// value: 0.0.0.0:${MY_PORT_VAR}
package listenaddress
import (
"fmt"
"os"
"strconv"
)
const (
defaultHostname = "0.0.0.0"
)
// Resolver is a type of port resolver
type Resolver string
const (
// ConfigResolver resolves port using a value provided in config
ConfigResolver Resolver = "config"
// EnvironmentResolver resolves port using an environment variable
// of which the name is provided in config
EnvironmentResolver Resolver = "environment"
)
// Configuration is the configuration for resolving a listen address.
type Configuration struct {
// DeprecatedListenAddressType is the port type for the port
// DEPRECATED: use config interpolation with `value` (config/README.md)
DeprecatedListenAddressType Resolver `yaml:"type"`
// Value is the config specified listen address if using config port type.
Value *string `yaml:"value"`
// EnvVarListenPort specifies the environment variable name for the listen address port.
// DEPRECATED: use config interpolation with `value` (config/README.md)
DeprecatedEnvVarListenPort *string `yaml:"envVarListenPort"`
// DeprecatedEnvVarListenHost specifies the environment variable name for the listen address hostname.
// DEPRECATED: use config interpolation with `value` (config/README.md)
DeprecatedEnvVarListenHost *string `yaml:"envVarListenHost"`
}
// Resolve returns the resolved listen address given the configuration.
func (c Configuration) Resolve() (string, error) {
listenAddrType := c.DeprecatedListenAddressType
if listenAddrType == "" {
// Default to ConfigResolver
listenAddrType = ConfigResolver
}
var listenAddress string
switch listenAddrType {
case ConfigResolver:
if c.Value == nil {
err := fmt.Errorf("missing listen address value using: resolver=%s",
string(listenAddrType))
return "", err
}
listenAddress = *c.Value
case EnvironmentResolver:
// environment variable for port is required
if c.DeprecatedEnvVarListenPort == nil {
err := fmt.Errorf("missing port env var name using: resolver=%s",
string(listenAddrType))
return "", err
}
portStr := os.Getenv(*c.DeprecatedEnvVarListenPort)
port, err := strconv.Atoi(portStr)
if err != nil {
err := fmt.Errorf("invalid port env var value using: resolver=%s, name=%s",
string(listenAddrType), *c.DeprecatedEnvVarListenPort)
return "", err
}
// if environment variable for hostname is not set, use the default
if c.DeprecatedEnvVarListenHost == nil {
listenAddress = fmt.Sprintf("%s:%d", defaultHostname, port)
} else {
envHost := os.Getenv(*c.DeprecatedEnvVarListenHost)
listenAddress = fmt.Sprintf("%s:%d", envHost, port)
}
default:
return "", fmt.Errorf("unknown listen address type: resolver=%s",
string(listenAddrType))
}
return listenAddress, nil
}