mux

package
v0.0.0-...-648f786 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

The registered topic, against which the router matches incoming requests, can contain two types of parameters:

Syntax    Type
:name     named parameter
*name     catch-all parameter

Named parameters are dynamic topic segments. They match anything until the next '/' or the topic end:

topic: /blog/:category/:post

Requests:
 /blog/go/request-routers            match: category="go", post="request-routers"
 /blog/go/request-routers/           no match, but the router would redirect
 /blog/go/                           no match
 /blog/go/request-routers/comments   no match

The value of parameters is saved as a slice of the Param struct, consisting each of a key and a value. The slice is passed to the Handle func as a third parameter. There are two ways to retrieve the value of a parameter:

// by the name of the parameter
user := ps.ByName("user") // defined by :user or *user

// by the index of the parameter. This way you can also get the name (key)
thirdKey   := ps[2].Key   // the name of the 3rd parameter
thirdValue := ps[2].Value // the value of the 3rd parameter

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HandlerFunc

type HandlerFunc func(c mqtt.Client, msg mqtt.Message, ps *Params)

Handle is a function that can be registered to a route to handle MQTT requests. Like MQTT.HandlerFunc, but has a third parameter for the values of wildcards (topic variables).

type Param

type Param struct {
	Key   string
	Value string
}

Param is a single URL 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 URL 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.

type Router

type Router struct {
	NotFoundHandle HandlerFunc
	// contains filtered or unexported fields
}

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

func NewRouter

func NewRouter() *Router

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

func (*Router) Handle

func (r *Router) Handle(topic string, handle HandlerFunc)

Handle registers the handler for the given pattern.

func (*Router) ServeMQTT

func (r *Router) ServeMQTT(c mqtt.Client, msg mqtt.Message)

ServeMQTT makes the router implement the mqtt.MessageHandle interface.

Jump to

Keyboard shortcuts

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