Skip to content

Commit

Permalink
Closes #2050: short example of serving static content
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed May 4, 2022
1 parent b376b60 commit 5b56b80
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
20 changes: 19 additions & 1 deletion doc/endpoint/static.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The easiest way to expose static content from the local filesystem is to use the
is parametrised with the path, at which the content should be exposed, as well as the local system path, from which
to read the data.

Such an endpoint has to be interpreted using your server interpreter. For example, using the akka-http interpreter:
Such an endpoint has to be interpreted using your server interpreter. For example, using the [akka-http](../server/akkahttp.md) interpreter:

```scala mdoc:compile-only
import akka.http.scaladsl.server.Route
Expand All @@ -28,6 +28,24 @@ val filesRoute: Route = AkkaHttpServerInterpreter().toRoute(
Using the above endpoint, a request to `/site/static/css/styles.css` will try to read the
`/home/static/data/css/styles.css` file.

To expose files without a prefix, use `emptyInput`. For example, using the [netty](../server/netty.md) interpreter, the
below exposes the content of `/var/www` at `http://localhost:8080`:

```scala mdoc:compile-only
import sttp.tapir.server.netty.NettyFutureServer
import sttp.tapir.{emptyInput, filesServerEndpoints}

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

NettyFutureServer()
.port(8080)
.addEndpoints(filesServerEndpoints[Future](emptyInput)("/var/www"))
.start()
.flatMap(_ => Future.never)
```


A single file can be exposed using `fileGetServerEndpoint`.

The file server endpoints can be secured using `ServerLogic.prependSecurity`, see [server logic](../server/logic.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package sttp.tapir.examples

import sttp.tapir.server.netty.NettyFutureServer
import sttp.tapir.{emptyInput, filesServerEndpoints}

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

object StaticContentFromFilesNettyServer extends App {
NettyFutureServer()
.port(8080)
.addEndpoints(filesServerEndpoints[Future](emptyInput)("/var/www"))
.start()
.flatMap(_ => Future.never)
}

0 comments on commit 5b56b80

Please sign in to comment.