/
envconf.go
121 lines (108 loc) · 3.52 KB
/
envconf.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
package gas
import (
"fmt"
"log"
"os"
"reflect"
"strconv"
"strings"
"time"
)
// Env is a singleton holding configuration parameters specified as environment
// variables. Field names in CamelCase correspond to the environment variables
// in SHOUTING_SNAKE_CASE. They may be overridden during runtime, but note that
// some are only used on startup (after init() and before Ignition).
var Env struct {
// The port for the server to listen on.
//
// PORT and TLS_PORT determine whether to use normal HTTP and/or HTTPS via
// TLS. If a port number is set greater than 0, then it will be used. At
// least one must be chosen; it is a fatal error to make both zero. If both
// are enabled, then the server will listen on both in separate goroutines.
//
// TLS will not be used if FastCGI is used.
Port int `default:"80"`
TLSPort int `default:"-1"`
// LISTEN should contain a comma-separated list of network!address pairs
// for the server to listen on. If ";tls" is appended to the end of a
// network:address pair, use TLS on that listener using GAS_TLS_CERT and
// GAS_TLS_KEY. If the network isn't given, it defaults to "tcp". Example:
//
// GAS_LISTEN=":80, tcp![::1]:8080, unix!/var/run/website.sock, tcp!:https;tls"
//
// The server will listen concurrently on all listed interfaces. LISTEN
// supercedes PORT and TLS_PORT, which are now deprecated.
Listen string
// When set, the server will listen using FastCGI on the given network.
//
// Deprecated.
FastCGI string
// Paths to the TLS certificate and key files, if TLS is enabled. Same
// rules as net/http.(*Server).ListenAndServeTLS.
TLSCert string
TLSKey string
// The hostname to send in the TLS handshake
TLSHost string
}
// EnvPrefix is the prefix append to the field name in Env, e.g. Env.DBName
// would be populated by the environment variable GAS_DB_NAME.
const EnvPrefix = "GAS_"
// EnvConf will populate a pointer to a struct that looks like Env and the
// fields will be filled in with the corresponding environment variables. Struct
// tag meanings:
//
// envconf:"required" // an error will be returned if this var isn't given
// default:"<default value>" // provide a default if this var isn't given
func EnvConf(conf interface{}, prefix string) error {
val := reflect.ValueOf(conf).Elem()
typ := val.Type()
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
fieldVal := val.Field(i)
name := prefix + strings.ToUpper(ToSnake(field.Name))
v := os.Getenv(name)
log.Printf("[envconf] %s = '%s'", name, v)
if v == "" {
if field.Tag.Get("envconf") == "required" {
return fmt.Errorf("%s: required parameter not specified", name)
} else if def := field.Tag.Get("default"); def != "" {
if err := stringValue(def, fieldVal.Addr().Interface()); err != nil {
return err
}
continue
}
}
if err := stringValue(v, fieldVal.Addr().Interface()); err != nil {
return err
}
}
return nil
}
func stringValue(s string, fieldVal interface{}) error {
var err error
switch v := fieldVal.(type) {
case *bool:
*v, err = strconv.ParseBool(s)
case *string:
*v = s
case *[]byte:
*v = []byte(s)
case *int:
*v, err = strconv.Atoi(s)
case *int64:
*v, err = strconv.ParseInt(s, 10, 64)
case *uint:
var a uint64
a, err = strconv.ParseUint(s, 10, 32)
*v = uint(a)
case *uint64:
*v, err = strconv.ParseUint(s, 10, 64)
case *float64:
*v, err = strconv.ParseFloat(s, 64)
case *time.Duration:
*v, err = time.ParseDuration(s)
default:
return fmt.Errorf("unhandled parameter type %T", fieldVal)
}
return err
}