transport

package
v1.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MapperHandler

func MapperHandler[R, V any](dbFn func(*R) (V, error), emptyRequest bool) func(*nats.Msg) (any, int, error)

MapperHandler creates a function that serves as a bridge between your database and a message broker (nats.Msg). It takes a function as a parameter that retrieves a value of type V from a pointer to a value of type R, and maps this to a nats.Msg.

The returned function takes a pointer to nats.Msg as its argument. It attempts to unmarshal the data from the message into a value of type R. If the data is not nil and unmarshalling is successful, the function performs validation on the struct. If validation fails or if an error occurs during unmarshalling, it returns the error with a 400 status code.

After successful validation, the dbFn function is called with a pointer to the value of type R, and if an error occurs during this database operation, it returns the error with a 500 status code.

If all operations are successful, it returns the value retrieved from the database (of type V), a 200 status code, and nil for error.

dbFn: function that takes a pointer to a value of type R and returns a value of type V and an error. It is used to perform the database operation.

Returns: A function that takes a pointer to nats.Msg and returns a value of type any, a status code of type int, and an error. This returned function serves as the handler for processing the message broker data and mapping it to the database model.

Types

type NATSError

type NATSError struct {
	Status int    `json:"status,omitempty"`
	Error  string `json:"error,omitempty"`
}

type NATSOk

type NATSOk struct {
	Status int `json:"status,omitempty"`
	Body   any `json:"body,omitempty"`
}

type Transport

type Transport struct {
	// contains filtered or unexported fields
}

func New

func New(natsUrl, nkeyFile, name string) (*Transport, error)

New initializes a new Transport instance. It connects to a NATS server using provided URL and nkey file, and creates error and OK response pools for efficient handling of responses. natsUrl: NATS server URL nkeyFile: path to the nkey file for authentication name: name of the NATS client Returns a pointer to a Transport instance or an error.

func (*Transport) Handle

func (t *Transport) Handle(e string, fn func(*nats.Msg) (any, int, error))

Handle subscribes to a topic in the message broker, executes a function 'fn' for each received message, and sends a response back. It logs the processing time for each message and handles any errors that occur, using pools for error and OK responses to optimize resource usage. e: topic to subscribe to fn: function to process each message received

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL