Because the uuid does not depend on the request in any way, you can map a directive called 'pass' (pass is a a sort of identity directive that does nothing). e.g.
"directive" should "generate a uuid" in { import akka.http.scaladsl.server.Directives.pass val uuid = UUID.randomUUID().toString val withRequestId: Directive1[String] = pass.tmap(_ => uuid) val route = withRequestId { (requestId: String) => complete(requestId) } Get("http://example.com") ~> route ~> check { responseAs[String] shouldBe uuid } } On Thursday, 24 August 2017 00:55:56 UTC+1, kraythe wrote: > > Greetings, > > I am currently using the low level API but lookign again at the DSL. I > have been trying to figure out if there is a way to create a directive that > creates a unique request UUID and allows other directives to access that > UUID in every sub directive. Basically I want something like > > withRequestId { > requestId = UUID.randomUUID() > pathSingleSlash { > comlete(requestId.toString) > } > } > > I want this to be universal so the outer directive defines the id and the > rest of the routes dont have to bother. I dont like copy past code. Any > idea if and how this could be done? > -- >>>>>>>>>> Read the docs: http://akka.io/docs/ >>>>>>>>>> Check the FAQ: >>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user --- You received this message because you are subscribed to the Google Groups "Akka User List" group. To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+unsubscr...@googlegroups.com. To post to this group, send email to akka-user@googlegroups.com. Visit this group at https://groups.google.com/group/akka-user. For more options, visit https://groups.google.com/d/optout.