natsrouter

package module
v2.0.9 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MatchedRoutePathParam = "$matchedRoutePath" //nolint

MatchedRoutePathParam is the Param name under which the path of the matched route is stored, if Router.SaveMatchedRoutePath is set.

Functions

This section is empty.

Types

type Handle

type Handle func(SubjectMsg, Params, interface{})

Handle is a function that can be registered to a route to handle NATS requests. It has a third parameter for the values of wildcards (path variables).

type Param

type Param struct {
	Key   string
	Value string
}

Param is a single parameter, consisting of a key and a value.

type Params

type Params []Param

Params is a Param-slice, as returned by the router. The slice is ordered, the first TOPIC parameter is also the first slice value. It is therefore safe to read values by the index.

func (Params) ByName

func (ps Params) ByName(name string) string

ByName returns the value of the first Param which key matches the given name. If no matching Param is found, an empty string is returned.

func (Params) MatchedRoutePath

func (ps Params) MatchedRoutePath() string

MatchedRoutePath retrieves the path of the matched route. Router.SaveMatchedRoutePath must have been enabled when the respective handler was added, otherwise this function always returns an empty string.

type Router

type Router struct {

	// If enabled, adds the matched route path onto the request context
	// before invoking the handler.
	// The matched route path is only added to handlers of routes that were
	// registered when this option was enabled.
	SaveMatchedRoutePath bool

	// Function to handle panics recovered from NATS handlers.
	// The handler can be used to keep your server from crashing because of
	// unrecovered panics.
	PanicHandler func(SubjectMsg, interface{})
	// contains filtered or unexported fields
}

Router is a handler which can be used to dispatch requests to different handler functions via configurable routes

func New

func New() *Router

New returns a new initialized Router. Path auto-correction, including trailing slashes, is enabled by default.

func (*Router) Handle

func (r *Router) Handle(path string, rank int, handle Handle)

Handle registers a new request handle with the given path.

func (*Router) Lookup

func (r *Router) Lookup(path string, rank int) (Handle, Params, bool)

Lookup allows the manual lookup of a rank + path combo. This is e.g. useful to build a framework around this router. If the path was found, it returns the handle function and the path parameter values.

func (*Router) ServeNATS

func (r *Router) ServeNATS(msg SubjectMsg) error

ServeNATS makes the router implement interface.

func (*Router) ServeNATSWithPayload

func (r *Router) ServeNATSWithPayload(msg SubjectMsg, payload interface{}) error

type SubjectMsg

type SubjectMsg interface {
	GetMsg() interface{}
	GetSubject() string
}

Jump to

Keyboard shortcuts

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