mux

package
v2.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2022 License: Apache-2.0 Imports: 12 Imported by: 34

Documentation

Overview

Example (AuthenticationMiddleware)
package main

import (
	"log"

	"github.com/plgd-dev/go-coap/v2/mux"
)

// Middleware function, which will be called for each request
func loggingMiddleware(next mux.Handler) mux.Handler {
	return mux.HandlerFunc(func(w mux.ResponseWriter, r *mux.Message) {
		log.Printf("ClientAddress %v, %v\n", w.Client().RemoteAddr(), r.String())
		next.ServeCOAP(w, r)
	})
}

func main() {
	r := mux.NewRouter()
	r.HandleFunc("/", func(w mux.ResponseWriter, r *mux.Message) {
		// Do something here
	})
	r.Use(loggingMiddleware)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	Ping(ctx context.Context) error
	Get(ctx context.Context, path string, opts ...message.Option) (*message.Message, error)
	Delete(ctx context.Context, path string, opts ...message.Option) (*message.Message, error)
	Post(ctx context.Context, path string, contentFormat message.MediaType, payload io.ReadSeeker, opts ...message.Option) (*message.Message, error)
	Put(ctx context.Context, path string, contentFormat message.MediaType, payload io.ReadSeeker, opts ...message.Option) (*message.Message, error)
	Observe(ctx context.Context, path string, observeFunc func(notification *message.Message), opts ...message.Option) (Observation, error)
	ClientConn() interface{}

	RemoteAddr() net.Addr
	Context() context.Context
	SetContextValue(key interface{}, val interface{})
	WriteMessage(req *message.Message) error
	Do(req *message.Message) (*message.Message, error)
	Close() error
	Sequence() uint64
	// Done signalizes that connection is not more processed.
	Done() <-chan struct{}
}

type ErrorFunc added in v2.5.0

type ErrorFunc = func(error)

type Handler

type Handler interface {
	ServeCOAP(w ResponseWriter, r *Message)
}

type HandlerFunc

type HandlerFunc func(w ResponseWriter, r *Message)

The HandlerFunc type is an adapter to allow the use of ordinary functions as COAP handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler object that calls f.

func (HandlerFunc) ServeCOAP

func (f HandlerFunc) ServeCOAP(w ResponseWriter, r *Message)

ServeCOAP calls f(w, r).

type Message

type Message struct {
	*message.Message
	// SequenceNumber identifies the order of the message from a TCP connection. For UDP it is just for debugging.
	SequenceNumber uint64
	// IsConfirmable indicates that a UDP message is confirmable. For TCP the value has no semantic.
	// When a handler blocks a confirmable message, the client might decide to issue a re-transmission.
	// Long running handlers can be handled in a go routine and send the response via w.Client().
	// The ACK is sent as soon as the handler returns.
	IsConfirmable bool
	RouteParams   *RouteParams
}

Message contains message with sequence number.

type MiddlewareFunc

type MiddlewareFunc func(Handler) Handler

MiddlewareFunc is a function which receives an Handler and returns another Handler. Typically, the returned handler is a closure which does something with the ResponseWriter and Message passed to it, and then calls the handler passed as parameter to the MiddlewareFunc.

func (MiddlewareFunc) Middleware

func (mw MiddlewareFunc) Middleware(handler Handler) Handler

Middleware allows MiddlewareFunc to implement the middleware interface.

type Observation

type Observation = interface {
	Cancel(ctx context.Context) error
	Canceled() bool
}

type ResponseWriter

type ResponseWriter = interface {
	SetResponse(code codes.Code, contentFormat message.MediaType, d io.ReadSeeker, opts ...message.Option) error
	Client() Client
}

type Route added in v2.5.0

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

func (*Route) GetRouteRegexp added in v2.5.0

func (route *Route) GetRouteRegexp() (string, error)

type RouteParams added in v2.5.0

type RouteParams struct {
	Path         string
	Vars         map[string]string
	PathTemplate string
}

RouteParams contains all the information related to a route

type Router

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

Router is an COAP request multiplexer. It matches the path name of each incoming request against a list of registered patterns add calls the handler for the pattern with same name. Router is also safe for concurrent access from multiple goroutines.

func NewRouter

func NewRouter() *Router

NewRouter allocates and returns a new Router.

func (*Router) DefaultHandle

func (r *Router) DefaultHandle(handler Handler)

DefaultHandle set default handler to the Router

func (*Router) DefaultHandleFunc

func (r *Router) DefaultHandleFunc(handler func(w ResponseWriter, r *Message))

DefaultHandleFunc set a default handler function to the Router.

func (*Router) GetRoute added in v2.5.0

func (r *Router) GetRoute(pattern string) *Route

GetRoute obtains route from the pattern it has been assigned

func (*Router) GetRoutes added in v2.5.0

func (r *Router) GetRoutes() map[string]Route

func (*Router) Handle

func (r *Router) Handle(pattern string, handler Handler) error

Handle adds a handler to the Router for pattern.

func (*Router) HandleFunc

func (r *Router) HandleFunc(pattern string, handler func(w ResponseWriter, r *Message))

HandleFunc adds a handler function to the Router for pattern.

func (*Router) HandleRemove

func (r *Router) HandleRemove(pattern string) error

HandleRemove deregistrars the handler specific for pattern from the Router.

func (*Router) Match added in v2.5.0

func (r *Router) Match(path string, routeParams *RouteParams) (matchedRoute *Route, matchedPattern string)

Find a handler on a handler map given a path string Most-specific (longest) pattern wins

func (*Router) ServeCOAP

func (r *Router) ServeCOAP(w ResponseWriter, req *Message)

ServeCOAP dispatches the request to the handler whose pattern most closely matches the request message. If DefaultServeMux is used the correct thing for DS queries is done: a possible parent is sought. If no handler is found a standard NotFound message is returned

func (*Router) Use

func (r *Router) Use(mwf ...MiddlewareFunc)

Use appends a MiddlewareFunc to the chain. Middleware can be used to intercept or otherwise modify requests and/or responses, and are executed in the order that they are applied to the Router.

Jump to

Keyboard shortcuts

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