Programatic creation of PathMatcher

scala
akka
akka-http
(Brice Figureau) #1

Hi,

I have a custom management REST API built with akka-http that is built by actors in the system registering a path. When the admin uses this route, it sends a message to the actor that registered it. It allows us to manager some part our application (for instance listing or modifying a given actor internal state, etc).

val extractedPath: String = ... // route given by the registering actor
path(separateOnSlashes(extractedPath)) {
{
  get {
     complete {
        // processing of request
     }
}

But I also need sometimes to pass a parameter. The registering actor gives a route path akin to /users/:userid, where I remove the parameter from the string and inject the prefix part into something like that:

val extractedPath: String = ... // route given by the registering actor
path(separateOnSlashes(extractedPath) / Segment) { value =>
{
  get {
     complete {
        // processing of request
     }
}

Now the problem is that I’d like to first support the parameter to be in an arbitrary position (ie /users/:userid/friends/).
I have code that is able to return me a Seq[PathMatcher[_]], which I try to reduce like this: path(route.specs.reduce(_ / _)), but this gives this compilation error:

Error:(490, 57) could not find implicit value for parameter join: akka.http.scaladsl.server.util.TupleOps.Join[_$1,_$1]
      path(route.specs.reduce(_ / _)) { value =>

I don’t really see what implicit I should import.
How can I solve this compilation error ?

Once that is solved, I’d like to support multiple parameters (for instance /users/:userid/friends/:friendid). I fear it won’t be possible because the underlying directive function need to have a dynamic arity.

Thanks!
Brice

(Johan Andrén) #2

I think you will have to create your own directive for dynamic path + sometimes extracting parts of it then try to get path to do it.

Note that you can actually completely change the routing tree dynamically, requires a bit of care with regards to thread safety. Here’s one example, may or may not be useful: https://gist.github.com/johanandren/abb594cafd10a266138730180ddf93c9