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 f72c38c commit 3db3151
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 56 deletions.
25 changes: 0 additions & 25 deletions arch/micro/context.go

This file was deleted.

8 changes: 2 additions & 6 deletions arch/micro/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@ import (
)

type baseController struct {
MessageSender
network.BaseController
context *Context
}

func NewBaseController(basePath string, authProvider network.AuthenticationProvider, authorizeProvider network.AuthorizationProvider) BaseController {
return &baseController{
MessageSender: NewMessageSender(),
BaseController: network.NewBaseController(basePath, authProvider, authorizeProvider),
context: EmptyContext(),
}
}

func (c *baseController) Context() *Context {
return c.context
}
12 changes: 10 additions & 2 deletions arch/micro/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@ type NatsGroup = micro.Group
type NatsHandlerFunc = micro.HandlerFunc
type NatsRequest = micro.Request

type SendMessage interface {
Message(data any)
Error(err error)
}

type MessageSender interface {
SendNats(req NatsRequest) SendMessage
}

type BaseController interface {
MessageSender
network.BaseController
Context() *Context
}

type Controller interface {
Expand All @@ -22,7 +31,6 @@ type Controller interface {
type Router interface {
network.BaseRouter
NatsClient() *NatsClient
Disconnect()
LoadControllers(controllers []Controller)
}

Expand Down
19 changes: 17 additions & 2 deletions arch/micro/message.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
package micro

type Message[T any] struct {
Data *T `json:"data,omitempty"`
Data T `json:"data,omitempty"`
Error *string `json:"error,omitempty"`
}

func NewMessage[T any](data *T, err error) *Message[T] {
type AnyMessage = Message[any]

func NewAnyMessage(data any, err error) *AnyMessage {
var e *string
if err != nil {
er := err.Error()
e = &er
}

return &AnyMessage{
Data: data,
Error: e,
}
}

func NewMessage[T any](data T, err error) *Message[T] {
var e *string
if err != nil {
er := err.Error()
Expand Down
19 changes: 13 additions & 6 deletions arch/micro/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ type Config struct {
NatsUrl string
NatsServiceName string
NatsServiceVersion string
Timeout time.Duration
}

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

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

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

Expand All @@ -36,11 +43,11 @@ func NewNatsClient(config *Config) *NatsClient {
panic(err)
}

fmt.Println("connected to nats..")
fmt.Println("connected to nats")

return &NatsClient{
Conn: nc,
Service: srv,
Timeout: nats.DefaultTimeout,
Conn: nc,
Service: srv,
Timeout: config.Timeout,
}
}
53 changes: 47 additions & 6 deletions arch/micro/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,66 @@ package micro
import (
"encoding/json"
"errors"
"time"
)

func Respond[T any](req NatsRequest, data *T, err error) {
req.RespondJSON(NewMessage(data, err))
type RequestBuilder[T any] interface {
NatsClient() *NatsClient
Request(data any) Request[T]
}

func Request[T any, V any](ctx *Context, subject string, send *T, receive *V) (*V, error) {
sendMsg := NewMessage(send, nil)
type requestBuilder[T any] struct {
natsClient *NatsClient
subject string
timeout time.Duration
}

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

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


func (c *requestBuilder[T]) Request(data any) Request[T] {
return newRequest(c, data)
}

type Request[T any] interface {
Nats() (*T, error)
}

type request[T any] struct {
builder *requestBuilder[T]
data any
}

func newRequest[T any](builder *requestBuilder[T], data any) Request[T] {
return &request[T]{
builder: builder,
data: data,
}
}

func (r *request[T]) Nats() (*T, error) {
sendMsg := NewMessage(r.data, nil)
sendPayload, err := json.Marshal(sendMsg)
if err != nil {
return nil, err
}

msg, err := ctx.NatsClient.Conn.Request(subject, sendPayload, ctx.NatsClient.Timeout)
msg, err := r.builder.natsClient.Conn.Request(r.builder.subject, sendPayload, r.builder.timeout)
if err != nil {
return nil, err
}

var receiveMsg Message[V]
var receiveMsg Message[*T]
err = json.Unmarshal(msg.Data, &receiveMsg)
if err != nil {
return nil, err
Expand Down
10 changes: 1 addition & 9 deletions arch/micro/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ type router struct {
natsClient *NatsClient
}

func NewRouter(mode string, config *Config) Router {
natsClient := NewNatsClient(config)
func NewRouter(mode string, natsClient *NatsClient) Router {
return &router{
netRouter: network.NewRouter(mode),
natsClient: natsClient,
Expand All @@ -30,10 +29,6 @@ func (r *router) NatsClient() *NatsClient {
return r.natsClient
}

func (r *router) Disconnect() {
r.natsClient.Conn.Close()
}

func (r *router) LoadRootMiddlewares(middlewares []network.RootMiddleware) {
r.netRouter.LoadRootMiddlewares(middlewares)
}
Expand All @@ -48,9 +43,6 @@ func (r *router) LoadControllers(controllers []Controller) {
for _, c := range controllers {
baseSub := fmt.Sprintf(`%s.%s`, r.natsClient.Service.Info().Name, strings.ReplaceAll(c.Path(), "/", ""))

c.Context().NatsClient = r.natsClient
c.Context().NatsSubject = baseSub

ng := r.natsClient.Service.AddGroup(baseSub)
c.MountNats(ng)
}
Expand Down
25 changes: 25 additions & 0 deletions arch/micro/sender.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package micro

type sender struct{}

func NewMessageSender() MessageSender {
return &sender{}
}

func (m *sender) SendNats(req NatsRequest) SendMessage {
return &send{
natsRequest: req,
}
}

type send struct {
natsRequest NatsRequest
}

func (s *send) Message(data any) {
s.natsRequest.RespondJSON(NewAnyMessage(data, nil))
}

func (s *send) Error(err error) {
s.natsRequest.RespondJSON(NewAnyMessage(nil, err))
}

0 comments on commit 3db3151

Please sign in to comment.