Routes

import krop.all.*

Paths

A Path represents a pattern to match against the path component of the request's URI. Paths are created starting with Path.root and then calling the / method to add segments to the pattern. For example

Path.root / "user" / "create"

matches a request with the path /user/create.

Capturing Path Segments

Use a Param to capture part of the path for later processing. For example

Path.root / "user" / Param.int / "view"

matches /user/<id>/view, where <id> is an Int, and makes the Int value available to the request handler.

Matching All Segments

A Path will fail to match if the URI's path has more segments than the Path matches. So Path.root / "user" / "create" will not match /user/create/1234. Use Segment.all to match and ignore all the segments to the end of the URI's path. For example

Path.root / "assets" / Segment.all

will match /assets/example.css and /assets/css/example.css.

To capture all segments to the end of the URI's path, use an instance of Param.All such as Param.vector. So

Path.root / "assets" / Param.vector

will capture the remainder of the URI's path as a Vector[String].

A Path that matches all segments is called a closed path. Attempting to add an element to a closed path will result in an exception.

Path.root / Segment.all / "crash"
// java.lang.IllegalStateException: Cannot add a segment or parameter to a closed path.
// 
//   A path is closed when it has a segment or parameter that matches all remaining elements.
//   A closed path cannot have additional segments of parameters added to it.
// 	at krop.route.Path.assertOpen(Path.scala:157)
// 	at krop.route.Path.$div(Path.scala:120)
// 	at repl.MdocSession$MdocApp.$init$$$anonfun$1(routes.md:41)