withSettings

withSettings

§Signature

§Description

Allows running an inner route using an alternative RoutingSettings in place of the default one.

The execution context can be extracted in an inner route using extractSettings directly, or used by directives which internally extract the materializer without sufracing this fact in the API.

§Example

  1. val special = RoutingSettings(system).withFileIODispatcher("special-io-dispatcher")
  2.  
  3. def sample() =
  4. path("sample") {
  5. complete {
  6. // internally uses the configured fileIODispatcher:
  7. val source = FileIO.fromPath(Paths.get("example.json"))
  8. HttpResponse(entity = HttpEntity(ContentTypes.`application/json`, source))
  9. }
  10. }
  11.  
  12. val route =
  13. get {
  14. pathPrefix("special") {
  15. withSettings(special) {
  16. sample() // `special` file-io-dispatcher will be used to read the file
  17. }
  18. } ~ sample() // default file-io-dispatcher will be used to read the file
  19. }
  20.  
  21. // tests:
  22. Post("/special/sample") ~> route ~> check {
  23. responseAs[String] shouldEqual s"{}"
  24. }
  25. Get("/sample") ~> route ~> check {
  26. responseAs[String] shouldEqual "{}"
  27. }