3

I am writing my first Opa application. I am trying to set up a match for url handling. 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 mappings. For example, I would like to be able to not only have / go to the home page, but also /home.

Is there a concise way to do this, perhaps with an OR operator, or do I need to set up separate cases that both trigger the same resource?

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

case {path:[] ...}: Resource.page("Home", home())
case {path:["home"] ...}: Resource.page("Home", home())
arcologies
  • 742
  • 1
  • 6
  • 22

1 Answers1

3

You should use a custom instead of a 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})

Learn more about the parser here: http://doc.opalang.org/manual/The-core-language/Parser

Cédrics
  • 1,974
  • 11
  • 13