github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/core/comm/config.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package comm
    18  
    19  import (
    20  	"github.com/spf13/viper"
    21  )
    22  
    23  // Is the configuration cached?
    24  var configurationCached = false
    25  
    26  // Cached values of commonly used configuration constants.
    27  var tlsEnabled bool
    28  
    29  // CacheConfiguration computes and caches commonly-used constants and
    30  // computed constants as package variables. Routines which were previously
    31  func CacheConfiguration() (err error) {
    32  
    33  	tlsEnabled = viper.GetBool("peer.tls.enabled")
    34  
    35  	configurationCached = true
    36  
    37  	return
    38  }
    39  
    40  // cacheConfiguration logs an error if error checks have failed.
    41  func cacheConfiguration() {
    42  	if err := CacheConfiguration(); err != nil {
    43  		commLogger.Errorf("Execution continues after CacheConfiguration() failure : %s", err)
    44  	}
    45  }
    46  
    47  // TLSEnabled return cached value for "peer.tls.enabled" configuration value
    48  func TLSEnabled() bool {
    49  	if !configurationCached {
    50  		cacheConfiguration()
    51  	}
    52  	return tlsEnabled
    53  }