github.com/april1989/origin-go-tools@v0.0.32/godoc/golangorgenv/golangorgenv.go (about)

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package golangorgenv provides environment information for programs running at
     6  // golang.org and its subdomains.
     7  package golangorgenv
     8  
     9  import (
    10  	"log"
    11  	"os"
    12  	"strconv"
    13  )
    14  
    15  var (
    16  	checkCountry = boolEnv("GOLANGORG_CHECK_COUNTRY")
    17  	enforceHosts = boolEnv("GOLANGORG_ENFORCE_HOSTS")
    18  )
    19  
    20  // CheckCountry reports whether country restrictions should be enforced.
    21  func CheckCountry() bool {
    22  	return checkCountry
    23  }
    24  
    25  // EnforceHosts reports whether host filtering should be enforced.
    26  func EnforceHosts() bool {
    27  	return enforceHosts
    28  }
    29  
    30  func boolEnv(key string) bool {
    31  	v := os.Getenv(key)
    32  	if v == "" {
    33  		// TODO(dmitshur): In the future, consider detecting if running in App Engine,
    34  		// and if so, making the environment variables mandatory rather than optional.
    35  		return false
    36  	}
    37  	b, err := strconv.ParseBool(v)
    38  	if err != nil {
    39  		log.Fatalf("environment variable %s (%q) must be a boolean", key, v)
    40  	}
    41  	return b
    42  }