github.com/Finschia/finschia-sdk@v0.48.1/baseapp/router.go (about)

     1  package baseapp
     2  
     3  import (
     4  	"fmt"
     5  
     6  	sdk "github.com/Finschia/finschia-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(route sdk.Route) sdk.Router {
    25  	if !sdk.IsAlphaNumeric(route.Path()) {
    26  		panic("route expressions can only contain alphanumeric characters")
    27  	}
    28  	if rtr.routes[route.Path()] != nil {
    29  		panic(fmt.Sprintf("route %s has already been initialized", route.Path()))
    30  	}
    31  
    32  	rtr.routes[route.Path()] = route.Handler()
    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  }