From 8803d28649f25e9e3cbacd3a8c7c734ac4adb2df Mon Sep 17 00:00:00 2001 From: Janishar Ali Date: Mon, 1 Jul 2024 14:18:43 +0530 Subject: [PATCH] improve micro architecture --- arch/micro/interfaces.go | 2 +- arch/micro/nats.go | 17 +++++++++++++---- arch/micro/request.go | 12 ++++++------ arch/micro/router.go | 12 +++++++----- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/arch/micro/interfaces.go b/arch/micro/interfaces.go index 87310f1..86f04d8 100644 --- a/arch/micro/interfaces.go +++ b/arch/micro/interfaces.go @@ -30,7 +30,7 @@ type Controller interface { type Router interface { network.BaseRouter - NatsClient() *NatsClient + NatsClient() NatsClient LoadControllers(controllers []Controller) } diff --git a/arch/micro/nats.go b/arch/micro/nats.go index 09a6b25..d54ef55 100644 --- a/arch/micro/nats.go +++ b/arch/micro/nats.go @@ -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) @@ -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, diff --git a/arch/micro/request.go b/arch/micro/request.go index e24e63b..ec17d12 100644 --- a/arch/micro/request.go +++ b/arch/micro/request.go @@ -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 } @@ -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 } diff --git a/arch/micro/router.go b/arch/micro/router.go index 218f45e..6b25d4f 100644 --- a/arch/micro/router.go +++ b/arch/micro/router.go @@ -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, @@ -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 } @@ -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) } }