In the Circumflex framework, you can map an URL to a block like this:
get("/foo") = {
"hello, world!"
}
which, when browsing to /foo, will show the given string as expected. Now, to write a complete web application, you almost always need some form of authentication and authorisation. I’m trying to write some kind of wrapper for the above construct, so I can write this:
get("/foo") = requireLogin {
"hello, world!"
}
The requireLogin method would then check if the user is logged in, and if yes, execute the given block. If not, however, it should do a redirect to the login page.
Now I somehow can’t get the syntax right (i’m still a Scala newbie). How would you do this in a generic fashion?
Try something like this:
This executes the passed code with probability 0.5, returning
Some(<result delivered by work>), or returnsNoneis the other cases. You can call it either like this:or with block notation:
The trick is to use a by-name parameter, signalled by the
=>symbol. Read more e.g. here: http://daily-scala.blogspot.com/2009/12/by-name-parameter-to-function.html