Skip to content

Commit

Permalink
feat: register routes for modules
Browse files Browse the repository at this point in the history
  • Loading branch information
hokamsingh committed Aug 19, 2024
1 parent fc61fa2 commit ca63a11
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 43 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ go 1.22.5
require (
github.com/gorilla/mux v1.8.1
github.com/lpernett/godotenv v0.0.0-20230527005122-0de1d4c5ef5e
go.uber.org/dig v1.18.0
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/lpernett/godotenv v0.0.0-20230527005122-0de1d4c5ef5e h1:6b4YTtccT1y/3eSsDCVhB6boPPCh5bQwP1Pa863yH28=
github.com/lpernett/godotenv v0.0.0-20230527005122-0de1d4c5ef5e/go.mod h1:K+inF/XYdmRn4sSP3IU4EM3KcOdGVJUJqZPmrQSxjGo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
go.uber.org/dig v1.18.0 h1:imUL1UiY0Mg4bqbFfsRQO5G4CGRBec/ZujWTvSVp3pw=
go.uber.org/dig v1.18.0/go.mod h1:Us0rSJiThwCv2GteUN0Q7OKvU7n5J4dxZ9JKUXozFdE=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
42 changes: 10 additions & 32 deletions internal/core/di/di.go
Original file line number Diff line number Diff line change
@@ -1,50 +1,28 @@
package di

import (
"errors"
"reflect"
"sync"
"go.uber.org/dig"
)

type Container struct {
services map[string]interface{}
mu sync.RWMutex
container *dig.Container
}

func NewContainer() *Container {
return &Container{
services: make(map[string]interface{}),
container: dig.New(),
}
}

func (c *Container) Register(name string, service interface{}) {
c.mu.Lock()
defer c.mu.Unlock()
c.services[name] = service
func (c *Container) Register(constructor interface{}) error {
return c.container.Provide(constructor)
}

func (c *Container) Get(name string) (interface{}, error) {
c.mu.RLock()
defer c.mu.RUnlock()
service, exists := c.services[name]
if !exists {
return nil, errors.New("service not found")
}
return service, nil
// Provide is an alias for Register. It registers a constructor or provider in the container
func (c *Container) Provide(constructor interface{}) error {
return c.Register(constructor)
}

func (c *Container) Inject(target interface{}) error {
val := reflect.ValueOf(target).Elem()
for i := 0; i < val.NumField(); i++ {
field := val.Field(i)
if field.CanSet() && field.Kind() == reflect.Interface {
serviceName := field.Type().String()
service, err := c.Get(serviceName)
if err != nil {
return err
}
field.Set(reflect.ValueOf(service))
}
}
return nil
func (c *Container) Invoke(function interface{}) error {
return c.container.Invoke(function)
}
17 changes: 8 additions & 9 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ func GetFolderPath(folderName string) (string, error) {
return folderPath, nil
}

func RegisterModuleControllers(r *router.Router, container *di.Container, module module.Module) error {
for _, ctrl := range module.Controllers {
if c, ok := ctrl.(controller.Controller); ok {
// Inject dependencies
container.Inject(c)
// Register routes
c.RegisterRoutes(r.Mux)
// registerModuleRoutes is a helper function to register routes for a module.
func RegisterModuleRoutes(container *di.Container, r *router.Router, _ interface{}) error {
return container.Invoke(func(module *module.Module) {
for _, ctrl := range module.Controllers {
if c, ok := ctrl.(controller.Controller); ok {
c.RegisterRoutes(r.Mux)
}
}
}
return nil
})
}
4 changes: 2 additions & 2 deletions pkg/lessgo/less.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ func GetFolderPath(folderName string) (string, error) {
return utils.GetFolderPath(folderName)
}

func RegisterModuleControllers(r *router.Router, container *di.Container, module module.Module) error {
return utils.RegisterModuleControllers(r, container, module)
func RegisterModuleRoutes(r *router.Router, container *di.Container, module module.Module) error {
return utils.RegisterModuleRoutes(container, r, module)
}

0 comments on commit ca63a11

Please sign in to comment.