-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
63 lines (52 loc) · 1.95 KB
/
config.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
/*
Copyright IBM Corp All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package library
import (
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
)
// Config configures the factory methods
// and plugins for the registry
type Config struct {
AuthFilters []*HandlerConfig `mapstructure:"authFilters" yaml:"authFilters"`
Decorators []*HandlerConfig `mapstructure:"decorators" yaml:"decorators"`
Endorsers PluginMapping `mapstructure:"endorsers" yaml:"endorsers"`
Validators PluginMapping `mapstructure:"validators" yaml:"validators"`
}
// PluginMapping stores a map between chaincode id to plugin config
type PluginMapping map[string]*HandlerConfig
// HandlerConfig defines configuration for a plugin or compiled handler
type HandlerConfig struct {
Name string `mapstructure:"name" yaml:"name"`
Library string `mapstructure:"library" yaml:"library"`
}
func LoadConfig() (Config, error) {
var authFilters, decorators []*HandlerConfig
if err := mapstructure.Decode(viper.Get("peer.handlers.authFilters"), &authFilters); err != nil {
return Config{}, err
}
if err := mapstructure.Decode(viper.Get("peer.handlers.decorators"), &decorators); err != nil {
return Config{}, err
}
endorsers, validators := make(PluginMapping), make(PluginMapping)
e := viper.GetStringMap("peer.handlers.endorsers")
for k := range e {
name := viper.GetString("peer.handlers.endorsers." + k + ".name")
library := viper.GetString("peer.handlers.endorsers." + k + ".library")
endorsers[k] = &HandlerConfig{Name: name, Library: library}
}
v := viper.GetStringMap("peer.handlers.validators")
for k := range v {
name := viper.GetString("peer.handlers.validators." + k + ".name")
library := viper.GetString("peer.handlers.validators." + k + ".library")
validators[k] = &HandlerConfig{Name: name, Library: library}
}
return Config{
AuthFilters: authFilters,
Decorators: decorators,
Endorsers: endorsers,
Validators: validators,
}, nil
}