Class Router

java.lang.Object
dev.gmitch215.bytebox.http.Router

public final class Router extends Object
Routes a request to a handler by method and path.

Matching compares path segments rather than running a regular expression. For a path that is already a list of segments, comparing them is both the direct implementation and the faster one, and it needs no pattern engine at all.

 public class Api implements Worker {
 	private static final Router ROUTES = Router.create()
 		.get("/health", (request, path, env, ctx) -> Bytebox.response("ok"))
 		.get("/users/:id", (request, path, env, ctx) -> Bytebox.response(path.get("id")))
 		.post("/users", Api::create)
 		.notFound((request, path, env, ctx) -> Bytebox.response("no such route", 404));

 	@Override
 	public Response fetch(Request request, Env env, ExecutionCtx ctx) {
 		return ROUTES.route(request, env, ctx);
 	}
 }

A pattern is a path with two kinds of placeholder. :name matches one segment and captures it. A trailing * matches the rest of the path and captures it as *. Everything else is compared literally.

Routes are tried in the order they were added, so a literal declared before a parameter wins over it. That is the order the code reads in, which is the order a reader expects.

Since:
1.0.0
  • Method Details

    • create

      public static Router create()
      Returns a new router with no routes.
      Returns:
      a new router with no routes
    • on

      public Router on(String method, String pattern, Route route)
      Adds a route.
      Parameters:
      method - the HTTP method, uppercase, or * for any
      pattern - the path pattern
      route - what answers it
      Returns:
      this router
    • get

      public Router get(String pattern, Route route)
      Adds a GET route.
      Parameters:
      pattern - the path pattern
      route - what answers it
      Returns:
      this router
    • post

      public Router post(String pattern, Route route)
      Adds a POST route.
      Parameters:
      pattern - the path pattern
      route - what answers it
      Returns:
      this router
    • put

      public Router put(String pattern, Route route)
      Adds a PUT route.
      Parameters:
      pattern - the path pattern
      route - what answers it
      Returns:
      this router
    • patch

      public Router patch(String pattern, Route route)
      Adds a PATCH route.
      Parameters:
      pattern - the path pattern
      route - what answers it
      Returns:
      this router
    • delete

      public Router delete(String pattern, Route route)
      Adds a DELETE route.
      Parameters:
      pattern - the path pattern
      route - what answers it
      Returns:
      this router
    • any

      public Router any(String pattern, Route route)
      Adds a route for every method.
      Parameters:
      pattern - the path pattern
      route - what answers it
      Returns:
      this router
    • notFound

      public Router notFound(Route route)
      Sets what answers a request no route matched.
      Parameters:
      route - what answers it
      Returns:
      this router
    • use

      public Router use(Filter filter)
      Adds a filter, which sees every request before the route does.

      Run in the order added, outermost first. A filter that answers rather than continuing is what makes authentication and rate limiting work.

      Parameters:
      filter - the filter
      Returns:
      this router
    • route

      public Response route(Request request, Env env, ExecutionCtx ctx)
      Routes a request.
      Parameters:
      request - the request
      env - the bindings
      ctx - the invocation context
      Returns:
      whatever answered it