Opa Compliance Pattern OR

I am writing my first Opa application. I am trying to set up a match for URL handling. The code is as follows:

function start(url) 
{
    match (url)
    {
        case {path:[] ...}: Resource.page("Home", home())
        case {...}: Resource.page("nofindy", nofindy())
    }
}
Server.start(Server.http, { dispatch: start })

It works like a charm, but now I want to be able to do synonymous comparisons. For example, I would like to be able to not only have / go to the home page, but also / home.

Is there a short way to do this, perhaps with the OR operator, or do I need to configure individual cases that run the same resource?

tl; dr: there is a better way to write the following snippet:

case {path:[] ...}: Resource.page("Home", home())
case {path:["home"] ...}: Resource.page("Home", home())
+3
source share
1 answer

You should use custominstead dispatch:

function start(v){
   Resource.page(v, <h1>{v}</h1>)
} 

urls = parser {
   case ("/" | "/home") : start("home")
   case .* : start("other")
}

Server.start(Server.http, {custom: urls})

: http://doc.opalang.org/manual/The-core-language/Parser

+3

All Articles