...

Source file src/github.com/palantir/go-baseapp/baseapp/config.go

Documentation: github.com/palantir/go-baseapp/baseapp

     1  // Copyright 2018 Palantir Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package baseapp
    16  
    17  import (
    18  	"os"
    19  	"strconv"
    20  	"time"
    21  )
    22  
    23  type TLSConfig struct {
    24  	CertFile string `yaml:"cert_file" json:"certFile"`
    25  	KeyFile  string `yaml:"key_file" json:"keyFile"`
    26  }
    27  
    28  // HTTPConfig contains options for HTTP servers. It is usually embedded in a
    29  // larger configuration struct.
    30  type HTTPConfig struct {
    31  	Address   string     `yaml:"address" json:"address"`
    32  	Port      int        `yaml:"port" json:"port"`
    33  	PublicURL string     `yaml:"public_url" json:"publicUrl"`
    34  	TLSConfig *TLSConfig `yaml:"tls_config" json:"tlsConfig"`
    35  
    36  	ShutdownWaitTime *time.Duration `yaml:"shutdown_wait_time" json:"shutdownWaitTime"`
    37  }
    38  
    39  // SetValuesFromEnv sets values in the configuration from corresponding
    40  // environment variables, if they exist. The optional prefix is added to the
    41  // start of the environment variable names.
    42  func (c *HTTPConfig) SetValuesFromEnv(prefix string) {
    43  	setStringFromEnv("ADDRESS", prefix, &c.Address)
    44  	setIntFromEnv("PORT", prefix, &c.Port)
    45  	setStringFromEnv("PUBLIC_URL", prefix, &c.PublicURL)
    46  
    47  	var d time.Duration
    48  	if setDurationFromEnv("SHUTDOWN_WAIT_TIME", prefix, &d) {
    49  		c.ShutdownWaitTime = &d
    50  	}
    51  
    52  	var tls TLSConfig
    53  	if c.TLSConfig != nil {
    54  		tls = *c.TLSConfig
    55  	}
    56  	setStringFromEnv("TLS_CERT_FILE", prefix, &tls.CertFile)
    57  	setStringFromEnv("TLS_KEY_FILE", prefix, &tls.KeyFile)
    58  	if tls.CertFile != "" || tls.KeyFile != "" {
    59  		c.TLSConfig = &tls
    60  	}
    61  }
    62  
    63  // LoggingConfig contains options for logging, such as log level and textual representation.
    64  // It is usually embedded in a larger configuration struct.
    65  type LoggingConfig struct {
    66  	Level string `yaml:"level" json:"level"`
    67  
    68  	// Pretty will make the output human-readable
    69  	Pretty bool `yaml:"pretty" json:"pretty"`
    70  }
    71  
    72  // SetValuesFromEnv sets values in the configuration from corresponding
    73  // environment variables, if they exist. The optional prefix is added to the
    74  // start of the environment variable names.
    75  func (c *LoggingConfig) SetValuesFromEnv(prefix string) {
    76  	setStringFromEnv("LOG_LEVEL", prefix, &c.Level)
    77  	setBoolFromEnv("LOG_PRETTY", prefix, &c.Pretty)
    78  }
    79  
    80  func setStringFromEnv(key, prefix string, value *string) bool {
    81  	if v, ok := os.LookupEnv(prefix + key); ok {
    82  		*value = v
    83  		return true
    84  	}
    85  	return false
    86  }
    87  
    88  func setDurationFromEnv(key, prefix string, value *time.Duration) bool {
    89  	if v, ok := os.LookupEnv(prefix + key); ok {
    90  		if d, err := time.ParseDuration(v); err == nil {
    91  			*value = d
    92  			return true
    93  		}
    94  	}
    95  	return false
    96  }
    97  
    98  func setIntFromEnv(key, prefix string, value *int) bool {
    99  	if v, ok := os.LookupEnv(prefix + key); ok {
   100  		if i, err := strconv.Atoi(v); err == nil {
   101  			*value = i
   102  			return true
   103  		}
   104  	}
   105  	return false
   106  }
   107  
   108  func setBoolFromEnv(key, prefix string, value *bool) bool {
   109  	if v, ok := os.LookupEnv(prefix + key); ok {
   110  		if b, err := strconv.ParseBool(v); err == nil {
   111  			*value = b
   112  			return true
   113  		}
   114  	}
   115  	return false
   116  }
   117  

View as plain text