Skip to content

Instantly share code, notes, and snippets.

@daviddenton
Last active August 28, 2017 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daviddenton/845c2490c9158c7b91c5f8f97ec7cf97 to your computer and use it in GitHub Desktop.
Save daviddenton/845c2490c9158c7b91c5f8f97ec7cf97 to your computer and use it in GitHub Desktop.
// add this method, and verb variants to ApiBuilder...
public static void get(String path, Function<Javalin, Handler> fn) {
staticInstance().get(prefixPath(path), fn.apply(staticInstance()));
}
// then you can remove the extra methods from ApiBuilder and Javalin which take the overloads
val myHandler = Handler { it.status(200) }
// this extension function will fix the readability problem IMHO, and add the security behaviour that you want.
fun Handler.securedBy(vararg roles: Role): (Javalin) -> Handler =
{ j -> Handler { ctx -> j.accessManager.manage(this, ctx, roles.toList()) } }
val whatever = path("users") {
get("/foo", myHandler.securedBy(ApiRole.ANYONE, ApiRole.USER_READ))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment