github.com/decred/politeia@v1.4.0/politeiawww/plugin/v1/register.go (about) 1 // Copyright (c) 2022 The Decred developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package v1 6 7 import "github.com/pkg/errors" 8 9 var ( 10 // The following maps store the various initialization functions that have 11 // been registered with this package. 12 pluginInitFns = make(map[string]func(InitArgs) (Plugin, error)) 13 userManagerInitFns = make(map[string]func(InitArgs) (UserManager, error)) 14 authManagerInitFns = make(map[string]func(InitArgs) (AuthManager, error)) 15 ) 16 17 // InitArgs contains the arguments used to initialize the plugin interface 18 // types. 19 type InitArgs struct { 20 Settings []Setting 21 } 22 23 // Setting represents a configurable plugin setting. 24 // 25 // The value can either contain a single value or multiple values. Multiple 26 // values will be formatted as a JSON encoded []string. 27 type Setting struct { 28 Key string // Name of setting 29 Value string // Value of setting 30 } 31 32 // RegisterPluginInitFn registers a Plugin initialization function with this 33 // package. This should be done by the plugin implementation as part of its 34 // package level init() function. The registered function is called at runtime 35 // to initialize the Plugin. 36 func RegisterPluginInitFn(pluginID string, fn func(InitArgs) (Plugin, error)) { 37 pluginInitFns[pluginID] = fn 38 } 39 40 // RegisterUserManagerInitFn registers a UserManager initialization function 41 // with this package. This should be done by the plugin implementation as part 42 // of its package level init() function. The registered function is called at 43 // runtime to initialize the UserManager . 44 func RegisterUserManagerInitFn(pluginID string, fn func(InitArgs) (UserManager, error)) { 45 userManagerInitFns[pluginID] = fn 46 } 47 48 // RegisterAuthManagerInitFn registers an AuthManager initialization function 49 // with this package. This should be done by the plugin implementation as part 50 // of its package level init() function. The registered function is called at 51 // runtime to initialize the AuthManager. 52 func RegisterAuthManagerInitFn(pluginID string, fn func(InitArgs) (AuthManager, error)) { 53 authManagerInitFns[pluginID] = fn 54 } 55 56 // NewPlugin uses the registered plugin initialization function to initialize 57 // and return a Plugin. 58 func NewPlugin(pluginID string, args InitArgs) (Plugin, error) { 59 fn, ok := pluginInitFns[pluginID] 60 if !ok { 61 return nil, errors.Errorf("plugin '%v' did not register "+ 62 "a plugin initialization function", pluginID) 63 } 64 return fn(args) 65 } 66 67 // NewUserManager uses the registered user manager initialization function to 68 // initialize and return a UserManager. 69 func NewUserManager(pluginID string, args InitArgs) (UserManager, error) { 70 fn, ok := userManagerInitFns[pluginID] 71 if !ok { 72 return nil, errors.Errorf("plugin '%v' did not register "+ 73 "a user manager initialization function", pluginID) 74 } 75 return fn(args) 76 } 77 78 // NewAuthManager uses the registered authManager initialization function to 79 // initialize and return an AuthManager. 80 func NewAuthManager(pluginID string, args InitArgs) (AuthManager, error) { 81 fn, ok := authManagerInitFns[pluginID] 82 if !ok { 83 return nil, errors.Errorf("plugin '%v' did not register "+ 84 "an auth manager initialization function", pluginID) 85 } 86 return fn(args) 87 }