github.com/true-sqn/fabric@v2.1.1+incompatible/core/handlers/library/config.go (about)

     1  /*
     2   Copyright IBM Corp All Rights Reserved.
     3  
     4   SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package library
     8  
     9  import (
    10  	"github.com/mitchellh/mapstructure"
    11  	"github.com/spf13/viper"
    12  )
    13  
    14  // Config configures the factory methods
    15  // and plugins for the registry
    16  type Config struct {
    17  	AuthFilters []*HandlerConfig `mapstructure:"authFilters" yaml:"authFilters"`
    18  	Decorators  []*HandlerConfig `mapstructure:"decorators" yaml:"decorators"`
    19  	Endorsers   PluginMapping    `mapstructure:"endorsers" yaml:"endorsers"`
    20  	Validators  PluginMapping    `mapstructure:"validators" yaml:"validators"`
    21  }
    22  
    23  // PluginMapping stores a map between chaincode id to plugin config
    24  type PluginMapping map[string]*HandlerConfig
    25  
    26  // HandlerConfig defines configuration for a plugin or compiled handler
    27  type HandlerConfig struct {
    28  	Name    string `mapstructure:"name" yaml:"name"`
    29  	Library string `mapstructure:"library" yaml:"library"`
    30  }
    31  
    32  func LoadConfig() (Config, error) {
    33  	var authFilters, decorators []*HandlerConfig
    34  	if err := mapstructure.Decode(viper.Get("peer.handlers.authFilters"), &authFilters); err != nil {
    35  		return Config{}, err
    36  	}
    37  
    38  	if err := mapstructure.Decode(viper.Get("peer.handlers.decorators"), &decorators); err != nil {
    39  		return Config{}, err
    40  	}
    41  
    42  	endorsers, validators := make(PluginMapping), make(PluginMapping)
    43  	e := viper.GetStringMap("peer.handlers.endorsers")
    44  	for k := range e {
    45  		name := viper.GetString("peer.handlers.endorsers." + k + ".name")
    46  		library := viper.GetString("peer.handlers.endorsers." + k + ".library")
    47  		endorsers[k] = &HandlerConfig{Name: name, Library: library}
    48  	}
    49  
    50  	v := viper.GetStringMap("peer.handlers.validators")
    51  	for k := range v {
    52  		name := viper.GetString("peer.handlers.validators." + k + ".name")
    53  		library := viper.GetString("peer.handlers.validators." + k + ".library")
    54  		validators[k] = &HandlerConfig{Name: name, Library: library}
    55  	}
    56  
    57  	return Config{
    58  		AuthFilters: authFilters,
    59  		Decorators:  decorators,
    60  		Endorsers:   endorsers,
    61  		Validators:  validators,
    62  	}, nil
    63  }