package mux
import "github.com/plgd-dev/go-coap/v3/mux"
Example (AuthenticationMiddleware)¶
Code:play
package main import ( "log" "github.com/plgd-dev/go-coap/v3/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.Conn().RemoteAddr(), r.String()) next.ServeCOAP(w, r) }) } func main() { r := mux.NewRouter() r.HandleFunc("/", func(mux.ResponseWriter, *mux.Message) { // Do something here }) r.Use(loggingMiddleware) }
Index ¶
- func FilterPath(unfiltered string) string
- func ToHandler[C Conn](m Handler) func(w *responsewriter.ResponseWriter[C], r *pool.Message)
- type Conn
- type ErrorFunc
- type Handler
- type HandlerFunc
- type Message
- type MiddlewareFunc
- type Observation
- type ResponseWriter
- type Route
- type RouteParams
- type Router
- func NewRouter() *Router
- func (r *Router) DefaultHandle(handler Handler)
- func (r *Router) DefaultHandleFunc(handler func(w ResponseWriter, r *Message))
- func (r *Router) GetRoute(pattern string) *Route
- func (r *Router) GetRoutes() map[string]Route
- func (r *Router) Handle(pattern string, handler Handler) error
- func (r *Router) HandleFunc(pattern string, handler func(w ResponseWriter, r *Message))
- func (r *Router) HandleRemove(pattern string) error
- func (r *Router) Match(path string, routeParams *RouteParams) (matchedRoute *Route, matchedPattern string)
- func (r *Router) ServeCOAP(w ResponseWriter, req *Message)
- func (r *Router) SetErrorHandler(h func(error))
- func (r *Router) Use(mwf ...MiddlewareFunc)
Examples ¶
Functions ¶
func FilterPath ¶
FilterPath checks the unfiltered input path or pattern against a blacklist and transforms them into valid paths of the same semantic meaning
func ToHandler ¶
func ToHandler[C Conn](m Handler) func(w *responsewriter.ResponseWriter[C], r *pool.Message)
ToHandler converts mux handler to udp/dtls/tcp handler.
Types ¶
type Conn ¶
type Conn interface { // create message from pool AcquireMessage(ctx context.Context) *pool.Message // return back the message to the pool for next use ReleaseMessage(m *pool.Message) Ping(ctx context.Context) error Get(ctx context.Context, path string, opts ...message.Option) (*pool.Message, error) Delete(ctx context.Context, path string, opts ...message.Option) (*pool.Message, error) Post(ctx context.Context, path string, contentFormat message.MediaType, payload io.ReadSeeker, opts ...message.Option) (*pool.Message, error) Put(ctx context.Context, path string, contentFormat message.MediaType, payload io.ReadSeeker, opts ...message.Option) (*pool.Message, error) Observe(ctx context.Context, path string, observeFunc func(notification *pool.Message), opts ...message.Option) (Observation, error) RemoteAddr() net.Addr // NetConn returns the underlying connection that is wrapped by client. The Conn returned is shared by all invocations of NetConn, so do not modify it. NetConn() net.Conn Context() context.Context SetContextValue(key interface{}, val interface{}) WriteMessage(req *pool.Message) error // used for GET,PUT,POST,DELETE Do(req *pool.Message) (*pool.Message, error) // used for observation (GET with observe 0) DoObserve(req *pool.Message, observeFunc func(req *pool.Message)) (Observation, error) Close() error Sequence() uint64 // Done signalizes that connection is not more processed. Done() <-chan struct{} AddOnClose(func()) NewGetRequest(ctx context.Context, path string, opts ...message.Option) (*pool.Message, error) NewObserveRequest(ctx context.Context, path string, opts ...message.Option) (*pool.Message, error) NewPutRequest(ctx context.Context, path string, contentFormat message.MediaType, payload io.ReadSeeker, opts ...message.Option) (*pool.Message, error) NewPostRequest(ctx context.Context, path string, contentFormat message.MediaType, payload io.ReadSeeker, opts ...message.Option) (*pool.Message, error) NewDeleteRequest(ctx context.Context, path string, opts ...message.Option) (*pool.Message, error) }
type ErrorFunc ¶
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 { *pool.Message RouteParams *RouteParams }
Message contains message with sequence number.
type MiddlewareFunc ¶
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, opts ...message.Option) error Canceled() bool }
type ResponseWriter ¶
type ResponseWriter = interface { SetResponse(code codes.Code, contentFormat message.MediaType, d io.ReadSeeker, opts ...message.Option) error Conn() Conn SetMessage(m *pool.Message) Message() *pool.Message }
type Route ¶
type Route struct {
// contains filtered or unexported fields
}
func (*Route) GetRouteRegexp ¶
type RouteParams ¶
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 ¶
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 ¶
GetRoute obtains route from the pattern it has been assigned
func (*Router) GetRoutes ¶
func (*Router) Handle ¶
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. This function will panic if the pattern parameter is invalid. If the APP provides 'user defined patterns' better use Handle(), which will return an error.
func (*Router) HandleRemove ¶
HandleRemove deregistrars the handler specific for pattern from the Router.
func (*Router) Match ¶
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) SetErrorHandler ¶
SetErrorHandler sets a custom error handler for the default mux handler set in the constructor.
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.
Source Files ¶
client.go message.go middleware.go muxResponseWriter.go regexp.go router.go
- Version
- v3.3.6 (latest)
- Published
- Sep 18, 2024
- Platform
- linux/amd64
- Imports
- 15 packages
- Last checked
- 1 week ago –
Tools for package owners.