github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/baseapp/router.go (about)

     1  package baseapp
     2  
     3  import (
     4  	"fmt"
     5  
     6  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     7  )
     8  
     9  type Router struct {
    10  	routes map[string]sdk.Handler
    11  }
    12  
    13  var _ sdk.Router = NewRouter()
    14  
    15  // NewRouter returns a reference to a new router.
    16  func NewRouter() *Router {
    17  	return &Router{
    18  		routes: make(map[string]sdk.Handler),
    19  	}
    20  }
    21  
    22  // AddRoute adds a route path to the router with a given handler. The route must
    23  // be alphanumeric.
    24  func (rtr *Router) AddRoute(path string, h sdk.Handler) sdk.Router {
    25  	if !isAlphaNumeric(path) {
    26  		panic("route expressions can only contain alphanumeric characters")
    27  	}
    28  	if rtr.routes[path] != nil {
    29  		panic(fmt.Sprintf("route %s has already been initialized", path))
    30  	}
    31  
    32  	rtr.routes[path] = h
    33  	return rtr
    34  }
    35  
    36  // Route returns a handler for a given route path.
    37  //
    38  // TODO: Handle expressive matches.
    39  func (rtr *Router) Route(_ sdk.Context, path string) sdk.Handler {
    40  	return rtr.routes[path]
    41  }