Skip to content

Commit

Permalink
improve context for micro controller
Browse files Browse the repository at this point in the history
  • Loading branch information
janishar committed Jun 30, 2024
1 parent 273624d commit f72c38c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
20 changes: 13 additions & 7 deletions arch/micro/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@ import (
"context"
)

type NatsContext struct {
type Context struct {
context.Context
Client *NatsClient
Subject string
NatsClient *NatsClient
NatsSubject string
}

func NewNatsContext(client *NatsClient, baseSub string) *NatsContext {
return &NatsContext{
func EmptyContext() *Context {
return &Context{
Context: context.Background(),
Client: client,
Subject: baseSub,
}
}

func NewContext(natsClient *NatsClient, baseSub string) *Context {
return &Context{
Context: context.Background(),
NatsClient: natsClient,
NatsSubject: baseSub,
}
}
11 changes: 4 additions & 7 deletions arch/micro/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@ import (

type baseController struct {
network.BaseController
natsCtx *NatsContext
context *Context
}

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

func (c *baseController) SetNatsContext(ctx *NatsContext) {
c.natsCtx = ctx
}

func (c *baseController) NatsContext() *NatsContext {
return c.natsCtx
func (c *baseController) Context() *Context {
return c.context
}
3 changes: 1 addition & 2 deletions arch/micro/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ type NatsRequest = micro.Request

type BaseController interface {
network.BaseController
SetNatsContext(ctx *NatsContext)
NatsContext() *NatsContext
Context() *Context
}

type Controller interface {
Expand Down
6 changes: 2 additions & 4 deletions arch/micro/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@ package micro
import (
"encoding/json"
"errors"
"fmt"
)

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

func Request[T any, V any](ctx *NatsContext, sub string, send *T, receive *V) (*V, error) {
func Request[T any, V any](ctx *Context, subject string, send *T, receive *V) (*V, error) {
sendMsg := NewMessage(send, nil)
sendPayload, err := json.Marshal(sendMsg)
if err != nil {
return nil, err
}

subject := fmt.Sprintf(`%s.%s`, ctx.Subject, sub)
msg, err := ctx.Client.Conn.Request(subject, sendPayload, ctx.Client.Timeout)
msg, err := ctx.NatsClient.Conn.Request(subject, sendPayload, ctx.NatsClient.Timeout)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions arch/micro/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func (r *router) LoadControllers(controllers []Controller) {
for _, c := range controllers {
baseSub := fmt.Sprintf(`%s.%s`, r.natsClient.Service.Info().Name, strings.ReplaceAll(c.Path(), "/", ""))

ctx := NewNatsContext(r.natsClient, baseSub)
c.SetNatsContext(ctx)
c.Context().NatsClient = r.natsClient
c.Context().NatsSubject = baseSub

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

0 comments on commit f72c38c

Please sign in to comment.