listDirectoryContents

Signature

def listDirectoryContents(directories: String*)(implicit renderer: DirectoryRenderer): Route

Description

Completes GET requests with a unified listing of the contents of all given directories. The actual rendering of the directory contents is performed by the in-scope Marshaller<DirectoryListing>Marshaller[DirectoryListing].

To just serve files use getFromDirectory.

To serve files and provide a browseable directory listing use getFromBrowseableDirectories instead.

The rendering can be overridden by providing a custom Marshaller<DirectoryListing>Marshaller[DirectoryListing]DirectoryRenderer implementation, you can read more about it in getFromDirectory ’s documentation.

Note that it’s not required to wrap this directive with get as this directive will only respond to GET requests.

Example

Scala
sourceval route =
  concat(
    path("tmp") {
      listDirectoryContents("/tmp")
    },
    path("custom") {
      // implement your custom renderer here
      val renderer = new DirectoryRenderer {
        override def marshaller(renderVanityFooter: Boolean): ToEntityMarshaller[DirectoryListing] = ???
      }
      listDirectoryContents("/tmp")(renderer)
    }
  )

// tests:
Get("/logs/example") ~> route ~> check {
  responseAs[String] shouldEqual "example file contents"
}
Java
sourceimport akka.http.javadsl.server.Directives;

import static akka.http.javadsl.server.Directives.listDirectoryContents;
import static akka.http.javadsl.server.Directives.path;

final Route route = Directives.concat(
  path("tmp", () -> listDirectoryContents("/tmp")),
  path("custom", () -> {
    // implement your custom renderer here
    final DirectoryRenderer renderer = renderVanityFooter -> {
      throw new NotImplementedError();
    };
    return listDirectoryContents(renderer, "/tmp");
  })
);

// tests:
testRoute(route).run(HttpRequest.GET("/logs/example"))
  .assertEntity("example file contents");
Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.