pekko/akka-docs/rst/scala/http/routing-dsl/directives/file-and-resource-directives/getFromDirectory.rst

38 lines
1.5 KiB
ReStructuredText
Raw Normal View History

.. _-getFromDirectory-:
getFromDirectory
================
Signature
---------
.. includecode2:: /../../akka-http/src/main/scala/akka/http/scaladsl/server/directives/FileAndResourceDirectives.scala
:snippet: getFromDirectory
Description
-----------
2015-10-08 16:26:29 +02:00
Allows exposing a directory's files for GET requests for its contents.
2015-10-08 16:26:29 +02:00
The ``unmatchedPath`` (see :ref:`-extractUnmatchedPath-`) of the ``RequestContext`` is first transformed by
the given ``pathRewriter`` function, before being appended to the given directory name to build the final file name.
2015-10-08 16:26:29 +02:00
To serve a single file use :ref:`-getFromFile-`.
To serve browsable directory listings use :ref:`-getFromBrowseableDirectories-`.
To serve files from a classpath directory use :ref:`-getFromResourceDirectory-` instead.
Note that it's not required to wrap this directive with ``get`` as this directive will only respond to ``GET`` requests.
2015-10-08 16:26:29 +02:00
.. note::
The file's contents will be read using an Akka Streams `Source` which *automatically uses
2015-10-08 16:26:29 +02:00
a pre-configured dedicated blocking io dispatcher*, which separates the blocking file operations from the rest of the stream.
Note also that thanks to using Akka Streams internally, the file will be served at the highest speed reachable by
2015-10-08 16:26:29 +02:00
the client, and not faster i.e. the file will *not* end up being loaded in full into memory before writing it to
the client.
Example
-------
2015-10-08 16:26:29 +02:00
.. includecode2:: ../../../../code/docs/http/scaladsl/server/directives/FileAndResourceDirectivesExamplesSpec.scala
:snippet: getFromDirectory-examples