expose plugin service.

This commit is contained in:
Aleksandar Sukovic 2022-04-19 10:14:59 +00:00
parent 35892f4ed1
commit fdd0639036
3 changed files with 66 additions and 17 deletions

View File

@ -59,7 +59,7 @@ type serve struct {
member *member.MemberManagerCtx member *member.MemberManagerCtx
session *session.SessionManagerCtx session *session.SessionManagerCtx
webSocket *websocket.WebSocketManagerCtx webSocket *websocket.WebSocketManagerCtx
plugins *plugins.PluginsManagerCtx plugins *plugins.ManagerCtx
api *api.ApiManagerCtx api *api.ApiManagerCtx
http *http.HttpManagerCtx http *http.HttpManagerCtx
} }

View File

@ -14,13 +14,13 @@ import (
"gitlab.com/demodesk/neko/server/pkg/types" "gitlab.com/demodesk/neko/server/pkg/types"
) )
type PluginsManagerCtx struct { type ManagerCtx struct {
logger zerolog.Logger logger zerolog.Logger
plugins map[string]types.Plugin plugins map[string]types.Plugin
} }
func New(config *config.Plugins) *PluginsManagerCtx { func New(config *config.Plugins) *ManagerCtx {
manager := &PluginsManagerCtx{ manager := &ManagerCtx{
logger: log.With().Str("module", "plugins").Logger(), logger: log.With().Str("module", "plugins").Logger(),
plugins: map[string]types.Plugin{}, plugins: map[string]types.Plugin{},
} }
@ -33,7 +33,7 @@ func New(config *config.Plugins) *PluginsManagerCtx {
return manager return manager
} }
func (manager *PluginsManagerCtx) loadDir(dir string) error { func (manager *ManagerCtx) loadDir(dir string) error {
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
@ -50,7 +50,7 @@ func (manager *PluginsManagerCtx) loadDir(dir string) error {
}) })
} }
func (manager *PluginsManagerCtx) load(path string) error { func (manager *ManagerCtx) load(path string) error {
pl, err := plugin.Open(path) pl, err := plugin.Open(path)
if err != nil { if err != nil {
return err return err
@ -66,11 +66,16 @@ func (manager *PluginsManagerCtx) load(path string) error {
return fmt.Errorf("not a valid plugin") return fmt.Errorf("not a valid plugin")
} }
manager.plugins[path] = p _, ok = manager.plugins[p.Name()]
if ok {
return fmt.Errorf("plugin '%s' already exists", p.Name())
}
manager.plugins[p.Name()] = p
return nil return nil
} }
func (manager *PluginsManagerCtx) InitConfigs(cmd *cobra.Command) { func (manager *ManagerCtx) InitConfigs(cmd *cobra.Command) {
for path, plug := range manager.plugins { for path, plug := range manager.plugins {
if err := plug.Config().Init(cmd); err != nil { if err := plug.Config().Init(cmd); err != nil {
log.Err(err).Str("plugin", path).Msg("unable to initialize configuration") log.Err(err).Str("plugin", path).Msg("unable to initialize configuration")
@ -78,13 +83,13 @@ func (manager *PluginsManagerCtx) InitConfigs(cmd *cobra.Command) {
} }
} }
func (manager *PluginsManagerCtx) SetConfigs() { func (manager *ManagerCtx) SetConfigs() {
for _, plug := range manager.plugins { for _, plug := range manager.plugins {
plug.Config().Set() plug.Config().Set()
} }
} }
func (manager *PluginsManagerCtx) Start( func (manager *ManagerCtx) Start(
sessionManager types.SessionManager, sessionManager types.SessionManager,
webSocketManager types.WebSocketManager, webSocketManager types.WebSocketManager,
apiManager types.ApiManager, apiManager types.ApiManager,
@ -94,11 +99,12 @@ func (manager *PluginsManagerCtx) Start(
SessionManager: sessionManager, SessionManager: sessionManager,
WebSocketManager: webSocketManager, WebSocketManager: webSocketManager,
ApiManager: apiManager, ApiManager: apiManager,
LoadServiceFromPlugin: manager.LookupService,
}) })
} }
} }
func (manager *PluginsManagerCtx) Shutdown() error { func (manager *ManagerCtx) Shutdown() error {
for path, plug := range manager.plugins { for path, plug := range manager.plugins {
err := plug.Shutdown() err := plug.Shutdown()
manager.logger.Err(err).Str("plugin", path).Msg("plugin shutdown") manager.logger.Err(err).Str("plugin", path).Msg("plugin shutdown")
@ -106,3 +112,17 @@ func (manager *PluginsManagerCtx) Shutdown() error {
return nil return nil
} }
func (manager *ManagerCtx) LookupService(pluginName string) (any, error) {
plug, ok := manager.plugins[pluginName]
if !ok {
return nil, fmt.Errorf("plugin '%s' not found", pluginName)
}
expPlug, ok := plug.(types.ExposablePlugin)
if !ok {
return nil, fmt.Errorf("plugin '%s' is not exposable", pluginName)
}
return expPlug.ExposeService(), nil
}

View File

@ -1,15 +1,23 @@
package types package types
import ( import (
"errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
type Plugin interface { type Plugin interface {
Name() string
Config() PluginConfig Config() PluginConfig
Start(PluginManagers) Start(PluginManagers)
Shutdown() error Shutdown() error
} }
type ExposablePlugin interface {
Plugin
ExposeService() any
}
type PluginConfig interface { type PluginConfig interface {
Init(cmd *cobra.Command) error Init(cmd *cobra.Command) error
Set() Set()
@ -19,4 +27,25 @@ type PluginManagers struct {
SessionManager SessionManager SessionManager SessionManager
WebSocketManager WebSocketManager WebSocketManager WebSocketManager
ApiManager ApiManager ApiManager ApiManager
LoadServiceFromPlugin func(string) (any, error)
}
func (p *PluginManagers) Validate() error {
if p.SessionManager == nil {
return errors.New("SessionManager is nil")
}
if p.WebSocketManager == nil {
return errors.New("WebSocketManager is nil")
}
if p.ApiManager == nil {
return errors.New("ApiManager is nil")
}
if p.LoadServiceFromPlugin == nil {
return errors.New("LoadServiceFromPlugin is nil")
}
return nil
} }