good morning!!!!

Skip to content
Snippets Groups Projects
router.go 2.37 KiB
Newer Older
  • Learn to ignore specific revisions
  • a's avatar
    a committed
    package jmux
    
    
    a's avatar
    a committed
    import (
    
    a's avatar
    a committed
    	"gfx.cafe/open/jrpc/pkg/jsonrpc"
    
    a's avatar
    a committed
    )
    
    a's avatar
    rpc
    a committed
    
    // NewRouter returns a new Mux object that implements the Router interface.
    func NewRouter() *Mux {
    	return NewMux()
    }
    
    
    type StructReflector interface {
    	// mimics the behavior of the handlers in the go-ethereum rpc package
    	// if you don't know how to use this, just use the chi-like interface instead.
    	RegisterStruct(pattern string, rcvr any) error
    
    a's avatar
    a committed
    	// mimics the behavior of the handlers in the go-ethereum rpc package
    	// if you don't know how to use this, just use the chi-like interface instead.
    	RegisterFunc(pattern string, rcvr any) error
    
    a's avatar
    rpc
    a committed
    // Router consisting of the core routing methods used by chi's Mux,
    
    // adapted to fit json-rpc.
    
    a's avatar
    rpc
    a committed
    type Router interface {
    
    a's avatar
    a committed
    	jsonrpc.Handler
    
    a's avatar
    rpc
    a committed
    	Routes
    
    a's avatar
    rpc
    a committed
    
    	// Use appends one or more middlewares onto the Router stack.
    
    a's avatar
    a committed
    	Use(middlewares ...func(jsonrpc.Handler) jsonrpc.Handler)
    
    a's avatar
    rpc
    a committed
    
    	// With adds inline middlewares for an endpoint handler.
    
    a's avatar
    a committed
    	With(middlewares ...func(jsonrpc.Handler) jsonrpc.Handler) Router
    
    a's avatar
    rpc
    a committed
    
    	// Group adds a new inline-Router along the current routing
    	// path, with a fresh middleware stack for the inline-Router.
    	Group(fn func(r Router)) Router
    
    	// Route mounts a sub-Router along a `pattern`` string.
    	Route(pattern string, fn func(r Router)) Router
    
    	// Mount attaches another Handler along ./pattern/*
    
    a's avatar
    a committed
    	Mount(pattern string, h jsonrpc.Handler)
    
    a's avatar
    rpc
    a committed
    
    	// Handle and HandleFunc adds routes for `pattern` that matches
    	// all HTTP methods.
    
    a's avatar
    a committed
    	Handle(pattern string, h jsonrpc.Handler)
    	HandleFunc(pattern string, h jsonrpc.HandlerFunc)
    
    a's avatar
    rpc
    a committed
    
    	// NotFound defines a handler to respond whenever a route could
    	// not be found.
    
    a's avatar
    a committed
    	NotFound(h jsonrpc.HandlerFunc)
    
    a's avatar
    rpc
    a committed
    }
    
    // Routes interface adds two methods for router traversal, which is also
    // used by the `docgen` subpackage to generation documentation for Routers.
    type Routes interface {
    	// Routes returns the routing tree in an easily traversable structure.
    	Routes() []Route
    
    	// Middlewares returns the list of middlewares in use by the router.
    	Middlewares() Middlewares
    
    	// Match searches the routing tree for a handler that matches
    	// the method/path - similar to routing a http request, but without
    	// executing the handler thereafter.
    	Match(rctx *Context, path string) bool
    }
    
    // Middlewares type is a slice of standard middleware handlers with methods
    // to compose middleware chains and Handler's.
    
    a's avatar
    a committed
    type Middlewares = jsonrpc.Middlewares