From 8aab486f878c58b0645a14966dcfc6107c0593fa Mon Sep 17 00:00:00 2001 From: Muhamad Surya Iksanudin Date: Wed, 10 Feb 2021 11:52:30 +0700 Subject: [PATCH] improve di management --- cmds/app/main.go | 24 ++++++++++++++++++++++-- cmds/module/main.go | 2 +- configs/config.go | 38 +++++++++++++++++++++++++++++++++----- configs/log_extension.go | 13 +++++++++++++ configs/struct.go | 6 ------ dics/core.go | 35 +++++++++++++++++++++++++++++++++++ dics/dispatcher.go | 37 ------------------------------------- dics/interface.go | 31 ------------------------------- dics/logger.go | 25 ------------------------- dics/middleware.go | 25 ------------------------- dics/provider.go | 16 ---------------- events/dispatcher.go | 1 + generators/module.go | 2 +- handlers/middleware.go | 7 +++++++ listeners.yaml | 7 +++++++ loggers.yaml | 2 ++ middlewares.yaml | 2 ++ 17 files changed, 124 insertions(+), 149 deletions(-) create mode 100644 configs/log_extension.go delete mode 100644 dics/dispatcher.go delete mode 100644 dics/interface.go delete mode 100644 dics/logger.go delete mode 100644 dics/middleware.go create mode 100644 listeners.yaml create mode 100644 loggers.yaml create mode 100644 middlewares.yaml diff --git a/cmds/app/main.go b/cmds/app/main.go index 3e81d11..77bd2bc 100644 --- a/cmds/app/main.go +++ b/cmds/app/main.go @@ -5,16 +5,36 @@ import ( "github.com/crowdeco/skeleton/configs" "github.com/crowdeco/skeleton/generated/dic" + "github.com/sirupsen/logrus" ) func main() { container, _ := dic.NewContainer() + parser := container.GetCoreConfigParser() var servers []configs.Server - for _, m := range container.GetCoreConfigParser().Parse() { - servers = append(servers, container.Get(fmt.Sprintf("%s:server", m)).(configs.Server)) + for _, c := range parser.ParseModules() { + servers = append(servers, container.Get(fmt.Sprintf("%s:server", c)).(configs.Server)) } + var listeners []configs.Listener + for _, c := range parser.ParseListeners() { + listeners = append(listeners, container.Get(c).(configs.Listener)) + } + + var middlewares []configs.Middleware + for _, c := range parser.ParseMiddlewares() { + middlewares = append(middlewares, container.Get(c).(configs.Middleware)) + } + + var extensions []logrus.Hook + for _, c := range parser.ParseLoggers() { + extensions = append(extensions, container.Get(c).(logrus.Hook)) + } + + container.GetCoreLoggerExtension().Register(extensions) + container.GetCoreHandlerMiddleware().Register(middlewares) + container.GetCoreEventDispatcher().Register(listeners) container.GetCoreRouterGateway().Register(servers) container.GetCoreApplication().Run(servers) } diff --git a/cmds/module/main.go b/cmds/module/main.go index 6e505d6..bceda33 100644 --- a/cmds/module/main.go +++ b/cmds/module/main.go @@ -79,7 +79,7 @@ func unregister(container *dic.Container, util *color.Color, module string) { pluralizer := container.GetCoreUtilPluralizer() moduleName := word.Camelcase(pluralizer.Singular(module)) modulePlural := word.Underscore(pluralizer.Plural(moduleName)) - list := config.Parse() + list := config.ParseModules() exist := false for _, v := range list { diff --git a/configs/config.go b/configs/config.go index b364207..c75730e 100644 --- a/configs/config.go +++ b/configs/config.go @@ -9,18 +9,48 @@ import ( ) const MODULES_FILE = "modules.yaml" +const LISTENERS_FILE = "listeners.yaml" +const LOGGERS_FILE = "loggers.yaml" +const MIDDLEWARES_FILE = "middlewares.yaml" type Config struct { - Modules []string `yaml:"modules"` + Modules []string `yaml:"modules"` + Listeners []string `yaml:"listeners"` + Loggers []string `yaml:"loggers"` + Middlewares []string `yaml:"middlewares"` } -func (c *Config) Parse() []string { +func (c *Config) ParseModules() []string { + c.parse(MODULES_FILE) + + return c.Modules +} + +func (c *Config) ParseListeners() []string { + c.parse(LISTENERS_FILE) + + return c.Listeners +} + +func (c *Config) ParseLoggers() []string { + c.parse(LOGGERS_FILE) + + return c.Loggers +} + +func (c *Config) ParseMiddlewares() []string { + c.parse(MIDDLEWARES_FILE) + + return c.Middlewares +} + +func (c *Config) parse(file string) { workDir, err := os.Getwd() if err != nil { panic(err) } - config, err := ioutil.ReadFile(fmt.Sprintf("%s/%s", workDir, MODULES_FILE)) + config, err := ioutil.ReadFile(fmt.Sprintf("%s/%s", workDir, file)) if err != nil { panic(err) } @@ -29,6 +59,4 @@ func (c *Config) Parse() []string { if err != nil { panic(err) } - - return c.Modules } diff --git a/configs/log_extension.go b/configs/log_extension.go new file mode 100644 index 0000000..0ba8eee --- /dev/null +++ b/configs/log_extension.go @@ -0,0 +1,13 @@ +package configs + +import ( + "github.com/sirupsen/logrus" +) + +type LoggerExtension struct { + Extensions []logrus.Hook +} + +func (l *LoggerExtension) Register(extensions []logrus.Hook) { + l.Extensions = extensions +} diff --git a/configs/struct.go b/configs/struct.go index 8190db7..d9f514d 100644 --- a/configs/struct.go +++ b/configs/struct.go @@ -1,7 +1,5 @@ package configs -import "github.com/sirupsen/logrus" - type ( User struct { Id string @@ -51,10 +49,6 @@ type ( Columns []*FieldTemplate } - LoggerExtension struct { - Extensions []logrus.Hook - } - ModuleTemplate struct { Name string Fields []*FieldTemplate diff --git a/dics/core.go b/dics/core.go index 1908c14..22302cf 100644 --- a/dics/core.go +++ b/dics/core.go @@ -1,3 +1,4 @@ +// Don't change anything in this file, this file used by Skeleton Module Manager package dics import ( @@ -12,6 +13,7 @@ import ( amqp "github.com/ThreeDotsLabs/watermill-amqp/pkg/amqp" configs "github.com/crowdeco/skeleton/configs" drivers "github.com/crowdeco/skeleton/configs/drivers" + events "github.com/crowdeco/skeleton/events" generators "github.com/crowdeco/skeleton/generators" handlers "github.com/crowdeco/skeleton/handlers" interfaces "github.com/crowdeco/skeleton/interfaces" @@ -208,6 +210,39 @@ var Core = []dingo.Def{ Name: "core:database:driver:postgresql", Build: (*drivers.PostgreSql)(nil), }, + { + Name: "core:application", + Build: func( + database configs.Application, + elasticsearch configs.Application, + grpc configs.Application, + queue configs.Application, + rest configs.Application, + ) (*interfaces.Application, error) { + return &interfaces.Application{ + Applications: []configs.Application{database, elasticsearch, grpc, queue, rest}, + }, nil + }, + Params: dingo.Params{ + "0": dingo.Service("core:interface:database"), + "1": dingo.Service("core:interface:elasticsearch"), + "2": dingo.Service("core:interface:grpc"), + "3": dingo.Service("core:interface:queue"), + "4": dingo.Service("core:interface:rest"), + }, + }, + { + Name: "core:event:dispatcher", + Build: (*events.Dispatcher)(nil), + }, + { + Name: "core:handler:middleware", + Build: (*handlers.Middleware)(nil), + }, + { + Name: "core:logger:extension", + Build: (*configs.LoggerExtension)(nil), + }, { Name: "core:connection:database", Build: func( diff --git a/dics/dispatcher.go b/dics/dispatcher.go deleted file mode 100644 index b124064..0000000 --- a/dics/dispatcher.go +++ /dev/null @@ -1,37 +0,0 @@ -package dics - -import ( - configs "github.com/crowdeco/skeleton/configs" - events "github.com/crowdeco/skeleton/events" - "github.com/sarulabs/dingo/v4" -) - -var Dispatcher = []dingo.Def{ - { - Name: "core:event:dispatcher", - Build: func( - a configs.Listener, - b configs.Listener, - c configs.Listener, - d configs.Listener, - e configs.Listener, - f configs.Listener, - ) (*events.Dispatcher, error) { - dispatcher := events.Dispatcher{ - Events: make(map[string][]configs.Listener), - } - - dispatcher.Register([]configs.Listener{a, b, c, d, e, f}) - - return &dispatcher, nil - }, - Params: dingo.Params{ - "0": dingo.Service("core:listener:create:elasticsearch"), - "1": dingo.Service("core:listener:update:elasticsearch"), - "2": dingo.Service("core:listener:delete:elasticsearch"), - "3": dingo.Service("core:listener:create:created_by"), - "4": dingo.Service("core:listener:update:updated_by"), - "5": dingo.Service("core:listener:delete:deleted_by"), - }, - }, -} diff --git a/dics/interface.go b/dics/interface.go deleted file mode 100644 index e3e8a76..0000000 --- a/dics/interface.go +++ /dev/null @@ -1,31 +0,0 @@ -package dics - -import ( - configs "github.com/crowdeco/skeleton/configs" - interfaces "github.com/crowdeco/skeleton/interfaces" - "github.com/sarulabs/dingo/v4" -) - -var Interface = []dingo.Def{ - { - Name: "core:application", - Build: func( - database configs.Application, - elasticsearch configs.Application, - grpc configs.Application, - queue configs.Application, - rest configs.Application, - ) (*interfaces.Application, error) { - return &interfaces.Application{ - Applications: []configs.Application{database, elasticsearch, grpc, queue, rest}, - }, nil - }, - Params: dingo.Params{ - "0": dingo.Service("core:interface:database"), - "1": dingo.Service("core:interface:elasticsearch"), - "2": dingo.Service("core:interface:grpc"), - "3": dingo.Service("core:interface:queue"), - "4": dingo.Service("core:interface:rest"), - }, - }, -} diff --git a/dics/logger.go b/dics/logger.go deleted file mode 100644 index 9e2eda4..0000000 --- a/dics/logger.go +++ /dev/null @@ -1,25 +0,0 @@ -package dics - -import ( - configs "github.com/crowdeco/skeleton/configs" - "github.com/sarulabs/dingo/v4" - "github.com/sirupsen/logrus" -) - -var Logger = []dingo.Def{ - { - Name: "core:logger:extension", - Build: func( - mongodb logrus.Hook, - ) (*configs.LoggerExtension, error) { - return &configs.LoggerExtension{ - Extensions: []logrus.Hook{ - mongodb, - }, - }, nil - }, - Params: dingo.Params{ - "0": dingo.Service("core:logger:extension:mongodb"), - }, - }, -} diff --git a/dics/middleware.go b/dics/middleware.go deleted file mode 100644 index fcee99d..0000000 --- a/dics/middleware.go +++ /dev/null @@ -1,25 +0,0 @@ -package dics - -import ( - configs "github.com/crowdeco/skeleton/configs" - handlers "github.com/crowdeco/skeleton/handlers" - "github.com/sarulabs/dingo/v4" -) - -var Middleware = []dingo.Def{ - { - Name: "core:handler:middleware", - Build: func( - auth configs.Middleware, - ) (*handlers.Middleware, error) { - return &handlers.Middleware{ - Middlewares: []configs.Middleware{ - auth, - }, - }, nil - }, - Params: dingo.Params{ - "0": dingo.Service("core:middleware:auth"), - }, - }, -} diff --git a/dics/provider.go b/dics/provider.go index e10440b..f689266 100644 --- a/dics/provider.go +++ b/dics/provider.go @@ -14,22 +14,6 @@ func (p *Provider) Load() error { if err := p.AddDefSlice(Core); err != nil { return err } - - if err := p.AddDefSlice(Dispatcher); err != nil { - return err - } - - if err := p.AddDefSlice(Interface); err != nil { - return err - } - - if err := p.AddDefSlice(Logger); err != nil { - return err - } - - if err := p.AddDefSlice(Middleware); err != nil { - return err - } //@modules:register return nil diff --git a/events/dispatcher.go b/events/dispatcher.go index cc2f2b9..fc24d83 100644 --- a/events/dispatcher.go +++ b/events/dispatcher.go @@ -12,6 +12,7 @@ type Dispatcher struct { } func (d *Dispatcher) Register(listeners []configs.Listener) { + d.Events = make(map[string][]configs.Listener) sort.Slice(listeners, func(i, j int) bool { return listeners[i].Priority() > listeners[j].Priority() }) diff --git a/generators/module.go b/generators/module.go index ed29ddd..3dcd44a 100644 --- a/generators/module.go +++ b/generators/module.go @@ -21,7 +21,7 @@ func (g *Module) Generate(template *configs.Template, modulePath string, workDir panic(err) } - g.Config.Parse() + g.Config.ParseModules() g.Config.Modules = append(g.Config.Modules, fmt.Sprintf("module:%s", template.ModuleLowercase)) g.Config.Modules = g.makeUnique(g.Config.Modules) diff --git a/handlers/middleware.go b/handlers/middleware.go index 9c77f59..61627c9 100644 --- a/handlers/middleware.go +++ b/handlers/middleware.go @@ -11,6 +11,13 @@ type Middleware struct { Middlewares []configs.Middleware } +func (m *Middleware) Register(middlewares []configs.Middleware) { + sort.Slice(middlewares, func(i, j int) bool { + return middlewares[i].Priority() > middlewares[j].Priority() + }) + m.Middlewares = middlewares +} + func (m *Middleware) Attach(handler http.Handler) http.Handler { sort.Slice(m.Middlewares, func(i, j int) bool { return m.Middlewares[i].Priority() > m.Middlewares[j].Priority() diff --git a/listeners.yaml b/listeners.yaml new file mode 100644 index 0000000..06e900a --- /dev/null +++ b/listeners.yaml @@ -0,0 +1,7 @@ +listeners: + - core:listener:create:elasticsearch + - core:listener:update:elasticsearch + - core:listener:delete:elasticsearch + - core:listener:create:created_by + - core:listener:update:updated_by + - core:listener:delete:deleted_by diff --git a/loggers.yaml b/loggers.yaml new file mode 100644 index 0000000..4cdcc80 --- /dev/null +++ b/loggers.yaml @@ -0,0 +1,2 @@ +loggers: + - core:logger:extension:mongodb diff --git a/middlewares.yaml b/middlewares.yaml new file mode 100644 index 0000000..5343d1f --- /dev/null +++ b/middlewares.yaml @@ -0,0 +1,2 @@ +middlewares: + - core:middleware:auth