From b722cb8a62758480f68c77302216aad861c626a4 Mon Sep 17 00:00:00 2001 From: Darko Luketic <2694548+dalu@users.noreply.github.com> Date: Wed, 16 Jan 2019 05:48:01 +0100 Subject: [PATCH] CoreInterface remodel, PluginRegistry added --- core/base/base.go | 153 ++++++++++++++++++++++++++++++++++++++++++++++ interfaces.go | 82 ++++++++++++++++--------- main.go | 16 ++++- registry.go | 47 ++++++++++++++ 4 files changed, 265 insertions(+), 33 deletions(-) create mode 100644 core/base/base.go create mode 100644 registry.go diff --git a/core/base/base.go b/core/base/base.go new file mode 100644 index 0000000..748777f --- /dev/null +++ b/core/base/base.go @@ -0,0 +1,153 @@ +package base + +import ( + "fmt" + + "github.com/globalsign/mgo" + "github.com/jinzhu/gorm" +) + +type Base struct{} + +func (b *Base) Register() (string, error) { + if e := b.BeforeLoadPlugins(); e != nil { + return "", e + } + if e := b.LoadPlugins(); e != nil { + return "", e + } + if e := b.AfterLoadPlugins(); e != nil { + return "", e + } + if e := b.BeforeLoadConfiguration(); e != nil { + return "", e + } + if e := b.LoadConfiguration(); e != nil { + return "", e + } + if e := b.AfterLoadConfiguration(); e != nil { + return "", e + } + if e := b.BeforeConnectToDatabases(); e != nil { + return "", e + } + if e := b.ConnectToDatabases(); e != nil { + return "", e + } + if e := b.AfterConnectToDatabases(); e != nil { + return "", e + } + if e := b.BeforeSetupLogger(); e != nil { + return "", e + } + if e := b.SetupLogger(); e != nil { + return "", e + } + if e := b.AfterSetupLogger(); e != nil { + return "", e + } + if e := b.BeforeSetupRoutes(); e != nil { + return "", e + } + if e := b.SetupRoutes(); e != nil { + return "", e + } + if e := b.AfterSetupRoutes(); e != nil { + return "", e + } + if e := b.BeforeRun(); e != nil { + return "", e + } + if e := b.Run(); e != nil { + return "", e + } + if e := b.AfterRun(); e != nil { + return "", e + } + return "base", nil +} + +func (b *Base) UnRegister() error { + return nil +} + +func (b *Base) BeforeLoadPlugins() error { + fmt.Println("Hello World. BeforeLoadPlugins") + return nil +} + +func (b *Base) LoadPlugins() error { + return nil +} + +func (b *Base) AfterLoadPlugins() error { + return nil +} + +func (b *Base) BeforeLoadConfiguration() error { + return nil +} + +func (b *Base) LoadConfiguration() error { + return nil +} + +func (b *Base) AfterLoadConfiguration() error { + return nil +} + +func (b *Base) ConnectMongoDB(string) (*mgo.Session, error) { + return nil, nil +} + +func (b *Base) ConnectGORM(string) (*gorm.DB, error) { + return nil, nil +} + +func (b *Base) BeforeConnectToDatabases() error { + return nil +} + +func (b *Base) ConnectToDatabases() error { + return nil +} + +func (b *Base) AfterConnectToDatabases() error { + return nil +} + +func (b *Base) BeforeSetupLogger() error { + return nil +} + +func (b *Base) SetupLogger() error { + return nil +} + +func (b *Base) AfterSetupLogger() error { + return nil +} + +func (b *Base) BeforeSetupRoutes() error { + return nil +} + +func (b *Base) SetupRoutes() error { + return nil +} + +func (b *Base) AfterSetupRoutes() error { + return nil +} + +func (b *Base) BeforeRun() error { + return nil +} + +func (b *Base) Run() error { + return nil +} + +func (b *Base) AfterRun() error { + return nil +} diff --git a/interfaces.go b/interfaces.go index f155f6b..8b5d7ea 100644 --- a/interfaces.go +++ b/interfaces.go @@ -1,45 +1,67 @@ package main +import ( + "github.com/globalsign/mgo" + "github.com/jinzhu/gorm" +) + type CoreInterface interface { - BeforeLoadPlugins() - LoadPlugins() - AfterLoadPlugins() + Register() (string, error) + UnRegister() error - BeforeLoadConfiguration() - LoadConfiguration() - AfterLoadConfiguration() + BeforeLoadPlugins() error + LoadPlugins() error + AfterLoadPlugins() error - BeforeConnectToDatabases() - ConnectToDatabases() - AfterConnectToDatabases() + BeforeLoadConfiguration() error + LoadConfiguration() error + AfterLoadConfiguration() error - BeforeSetupLogger() - SetupLogger() - AfterSetupLogger() + // connection string parameter e.g. "localhost" + ConnectMongoDB(string) (*mgo.Session, error) - BeforeSetupRoutes() - SetupRoutes() - AfterSetupRoutes() + // connection string parameter + ConnectGORM(string) (*gorm.DB, error) - BeforeRun() - Run() - AfterRun() + // ConnectNeo4j + // ConnectArangoDB + // ConnectCassandra + // ConnectDGraph + + BeforeConnectToDatabases() error + ConnectToDatabases() error + AfterConnectToDatabases() error + + BeforeSetupLogger() error + SetupLogger() error + AfterSetupLogger() error + + BeforeSetupRoutes() error + SetupRoutes() error + AfterSetupRoutes() error + + BeforeRun() error + Run() error + AfterRun() error } type HandlerInterface interface { - BeforeProcessRequest() - ProcessRequest() - AfterProcessRequest() + Register() (string, error) + UnRegister() error - BeforeLoadDataFromDatabase() - LoadDataFromDatabase() - AfterLoadDataFromDatabase() + BeforeProcessRequest() error + ProcessRequest() error + AfterProcessRequest() error - BeforeSaveDataToDatabase() - SaveDataToDatabase() - AfterSaveDataToDatabase() + BeforeLoadDataFromDatabase() error + LoadDataFromDatabase() error + AfterLoadDataFromDatabase() error - BeforeRenderOutput() - RenderOutput() - AfterRenderOutput() + BeforeSaveDataToDatabase() error + SaveDataToDatabase() error + AfterSaveDataToDatabase() error + + BeforeRenderOutput() error + RenderOutput() error + AfterRenderOutput() error } diff --git a/main.go b/main.go index e721569..bc7e716 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,17 @@ package main +import ( + "log" + + "git.icod.de/dalu/contentplatform/core/base" +) + +var registry = NewPluginRegistry() + func main() { - /* - Some way to load the core plugins and the handler plugins - */ + + if e := registry.RegisterCorePlugin(new(base.Base)); e != nil { + log.Fatal(e.Error()) + } + } diff --git a/registry.go b/registry.go new file mode 100644 index 0000000..d58946a --- /dev/null +++ b/registry.go @@ -0,0 +1,47 @@ +package main + +type PluginRegistry struct { + CorePlugin map[string]CoreInterface + HandlerPlugin map[string]HandlerInterface +} + +func NewPluginRegistry() *PluginRegistry { + r := new(PluginRegistry) + r.CorePlugin = make(map[string]CoreInterface) + r.HandlerPlugin = make(map[string]HandlerInterface) + return r +} + +func (r *PluginRegistry) RegisterCorePlugin(plugin CoreInterface) error { + name, e := plugin.Register() + if e != nil { + return e + } + r.CorePlugin[name] = plugin + return nil +} + +func (r *PluginRegistry) RegisterHandlerPlugin(plugin HandlerInterface) error { + name, e := plugin.Register() + if e != nil { + return e + } + r.HandlerPlugin[name] = plugin + return nil +} + +func (r *PluginRegistry) GetCorePlugin(name string) CoreInterface { + return r.CorePlugin[name] +} + +func (r *PluginRegistry) GetHandlerPlugin(name string) HandlerInterface { + return r.HandlerPlugin[name] +} + +func (r *PluginRegistry) RemoveCorePlugin(name string) { + delete(r.CorePlugin, name) +} + +func (r *PluginRegistry) RemoveHandlerPlugin(name string) { + delete(r.HandlerPlugin, name) +}