Routing Engine - plecong/gimlet GitHub Wiki

The routing engine is modular in that the developer only defines the route pattern and the logic that should be executed if that pattern matches. Supporting multiple types of routing pattern styles (e.g. Sinatra, Flask, Gaelyk) is simply a matter of passing the pattern to the engine (optionally with the entire list of patterns and their precedence) and finding the match along with some parameter parsing from the URL. Parameters parsed from the URL will be put into a splat binding variable.

interface RoutingEngine {
    RouteInstance match(RouteDefinition routes, HttpServletRequest request)
}

class RouteInstance {
    RouteHandler handler
    Map<String,Object> splat
}

Routes

Regardless to how they are defined, a basic route definition will have 3 elements: a String pattern, a HTTP verb, and the logic that is to be executed. Since the logic can either be a Closure or a method, the actual call is wrapped into a RouteHandler:

class Route {
    String pattern
    Verb verb
    RouteHandler handler
}

The RouteDefinition class is a helper that maintains the order in which all of the routes were defined in order to define precedence.