Path parameters can be extracted from the Path(segments: Seq[String])
argument.
Create a file path_params.sc
and paste this code into it:
//> using scala "3.4.2"
//> using dep ba.sake::sharaf:0.8.0
import io.undertow.Undertow
import ba.sake.sharaf.*, routing.*
val routes = Routes:
case GET() -> Path("string", x) =>
Response.withBody(s"string = ${x}")
case GET() -> Path("int", param[Int](x)) =>
Response.withBody(s"int = ${x}")
Undertow.builder
.addHttpListener(8181, "localhost")
.setHandler(SharafHandler(routes))
.build
.start()
println(s"Server started at http://localhost:8181")
Then run it like this:
scala-cli path_params.sc
Now go to http://localhost:8181/string/abc
and you will get the param returned: string = abc
.
When you go to http://localhost:8181/int/123,
Sharaf will try to extract an Int
from the path parameter.
If it doesn't match, it will fall through, try the next route.