Skip to content

Instantly share code, notes, and snippets.

@aesteve
Created May 5, 2017 11:50
Show Gist options
  • Save aesteve/cd6c7436f9fb54f75696da8b767434e5 to your computer and use it in GitHub Desktop.
Save aesteve/cd6c7436f9fb54f75696da8b767434e5 to your computer and use it in GitHub Desktop.
public interface RouteDSL {
RouteDSL param(String paramName, Handler<RoutingContext> checker)
default<T> RouteDSL param(String paramName, Function<String, T> checker) {
return param(paramName, ctx -> {
String p = ctx.request().getParam(name);
try {
ctx.put(paramName, checker.apply(p));
ctx.next();
} catch(Exception e) {
ctx.response().setStatusCode(400).end("Invalid parameter " + name);
}
};
}
RouteDSL param(String paramName, Handler<RoutingContext> checker)
default<T> RouteDSL asyncParam(String paramName, Function<String, Future<T>> checker) {
return param(paramName, ctx -> {
String p = ctx.request().getParam(name);
checker.apply(p).setHandler(res -> {
if (res.failed()) {
ctx.fail(res.cause());
} else {
ctx.put(paramName, res.result());
ctx.next();
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment