mux

package
v2.2.0-getDeviceByIP Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2021 License: Apache-2.0 Imports: 7 Imported by: 33

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
}

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
}

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
}

type ResponseWriter

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

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) 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) 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