Skip to content

Commit

Permalink
improve di management
Browse files Browse the repository at this point in the history
  • Loading branch information
ad3n committed Feb 10, 2021
1 parent 149a074 commit 8aab486
Show file tree
Hide file tree
Showing 17 changed files with 124 additions and 149 deletions.
24 changes: 22 additions & 2 deletions cmds/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion cmds/module/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
38 changes: 33 additions & 5 deletions configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -29,6 +59,4 @@ func (c *Config) Parse() []string {
if err != nil {
panic(err)
}

return c.Modules
}
13 changes: 13 additions & 0 deletions configs/log_extension.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 0 additions & 6 deletions configs/struct.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package configs

import "github.com/sirupsen/logrus"

type (
User struct {
Id string
Expand Down Expand Up @@ -51,10 +49,6 @@ type (
Columns []*FieldTemplate
}

LoggerExtension struct {
Extensions []logrus.Hook
}

ModuleTemplate struct {
Name string
Fields []*FieldTemplate
Expand Down
35 changes: 35 additions & 0 deletions dics/core.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Don't change anything in this file, this file used by Skeleton Module Manager
package dics

import (
Expand All @@ -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"
Expand Down Expand Up @@ -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(
Expand Down
37 changes: 0 additions & 37 deletions dics/dispatcher.go

This file was deleted.

31 changes: 0 additions & 31 deletions dics/interface.go

This file was deleted.

25 changes: 0 additions & 25 deletions dics/logger.go

This file was deleted.

25 changes: 0 additions & 25 deletions dics/middleware.go

This file was deleted.

16 changes: 0 additions & 16 deletions dics/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions events/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand Down
2 changes: 1 addition & 1 deletion generators/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
7 changes: 7 additions & 0 deletions handlers/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions listeners.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions loggers.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
loggers:
- core:logger:extension:mongodb
2 changes: 2 additions & 0 deletions middlewares.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
middlewares:
- core:middleware:auth

0 comments on commit 8aab486

Please sign in to comment.