Skip to content

Commit

Permalink
improve micro architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
janishar committed Jul 1, 2024
1 parent 3db3151 commit 8803d28
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
2 changes: 1 addition & 1 deletion arch/micro/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Controller interface {

type Router interface {
network.BaseRouter
NatsClient() *NatsClient
NatsClient() NatsClient
LoadControllers(controllers []Controller)
}

Expand Down
17 changes: 13 additions & 4 deletions arch/micro/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,28 @@ type Config struct {
Timeout time.Duration
}

type NatsClient struct {
type NatsClient interface {
GetInstance() *natsClient
Disconnect()
}

type natsClient struct {
Conn *nats.Conn
Service micro.Service
Timeout time.Duration
}

func (n *NatsClient) Disconnect() {
func (n *natsClient) GetInstance() *natsClient {
return n
}

func (n *natsClient) Disconnect() {
fmt.Println("disconnecting nats..")
n.Conn.Close()
fmt.Println("disconnected nats")
}

func NewNatsClient(config *Config) *NatsClient {
func NewNatsClient(config *Config) NatsClient {
fmt.Println("connecting to nats..")

nc, err := nats.Connect(config.NatsUrl)
Expand All @@ -45,7 +54,7 @@ func NewNatsClient(config *Config) *NatsClient {

fmt.Println("connected to nats")

return &NatsClient{
return &natsClient{
Conn: nc,
Service: srv,
Timeout: config.Timeout,
Expand Down
12 changes: 6 additions & 6 deletions arch/micro/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ import (
)

type RequestBuilder[T any] interface {
NatsClient() *NatsClient
NatsClient() NatsClient
Request(data any) Request[T]
}

type requestBuilder[T any] struct {
natsClient *NatsClient
natsClient NatsClient
subject string
timeout time.Duration
}

func NewRequestBuilder[T any](natsClient *NatsClient, subject string) RequestBuilder[T] {
func NewRequestBuilder[T any](natsClient NatsClient, subject string) RequestBuilder[T] {
return &requestBuilder[T]{
natsClient: natsClient,
subject: subject,
timeout: natsClient.Timeout,
timeout: natsClient.GetInstance().Timeout,
}
}

func (c *requestBuilder[T]) NatsClient() *NatsClient {
func (c *requestBuilder[T]) NatsClient() NatsClient {
return c.natsClient
}

Expand Down Expand Up @@ -57,7 +57,7 @@ func (r *request[T]) Nats() (*T, error) {
return nil, err
}

msg, err := r.builder.natsClient.Conn.Request(r.builder.subject, sendPayload, r.builder.timeout)
msg, err := r.builder.natsClient.GetInstance().Conn.Request(r.builder.subject, sendPayload, r.builder.timeout)
if err != nil {
return nil, err
}
Expand Down
12 changes: 7 additions & 5 deletions arch/micro/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (

type router struct {
netRouter network.Router
natsClient *NatsClient
natsClient NatsClient
}

func NewRouter(mode string, natsClient *NatsClient) Router {
func NewRouter(mode string, natsClient NatsClient) Router {
return &router{
netRouter: network.NewRouter(mode),
natsClient: natsClient,
Expand All @@ -25,7 +25,7 @@ func (r *router) GetEngine() *gin.Engine {
return r.netRouter.GetEngine()
}

func (r *router) NatsClient() *NatsClient {
func (r *router) NatsClient() NatsClient {
return r.natsClient
}

Expand All @@ -40,10 +40,12 @@ func (r *router) LoadControllers(controllers []Controller) {
}
r.netRouter.LoadControllers(nc)

natsClient := r.natsClient.GetInstance()

for _, c := range controllers {
baseSub := fmt.Sprintf(`%s.%s`, r.natsClient.Service.Info().Name, strings.ReplaceAll(c.Path(), "/", ""))
baseSub := fmt.Sprintf(`%s.%s`, natsClient.Service.Info().Name, strings.ReplaceAll(c.Path(), "/", ""))

ng := r.natsClient.Service.AddGroup(baseSub)
ng := natsClient.Service.AddGroup(baseSub)
c.MountNats(ng)
}
}
Expand Down

0 comments on commit 8803d28

Please sign in to comment.