CoreInterface remodel, PluginRegistry added

This commit is contained in:
Darko Luketic 2019-01-16 05:48:01 +01:00
parent c9f3abd8b9
commit b722cb8a62
4 changed files with 265 additions and 33 deletions

153
core/base/base.go Normal file
View File

@ -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
}

View File

@ -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
}

16
main.go
View File

@ -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())
}
}

47
registry.go Normal file
View File

@ -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)
}