From d21380f57d68b00d8a70d7a2a429069f6f771563 Mon Sep 17 00:00:00 2001 From: kciesielski Date: Thu, 27 Apr 2023 13:46:59 +0200 Subject: [PATCH 1/2] Adjust examples to the new files module --- .../static_content/StaticContentFromFilesAkkaServer.scala | 3 ++- .../static_content/StaticContentFromFilesNettyServer.scala | 5 +++-- .../StaticContentFromResourcesAkkaServer.scala | 6 +++--- .../static_content/StaticContentSecureAkkaServer.scala | 3 ++- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/examples/src/main/scala/sttp/tapir/examples/static_content/StaticContentFromFilesAkkaServer.scala b/examples/src/main/scala/sttp/tapir/examples/static_content/StaticContentFromFilesAkkaServer.scala index e9c888e241..3be642ac2c 100644 --- a/examples/src/main/scala/sttp/tapir/examples/static_content/StaticContentFromFilesAkkaServer.scala +++ b/examples/src/main/scala/sttp/tapir/examples/static_content/StaticContentFromFilesAkkaServer.scala @@ -6,6 +6,7 @@ import akka.http.scaladsl.server.Route import sttp.client3._ import sttp.model.{ContentRangeUnits, Header, HeaderNames, StatusCode} import sttp.tapir._ +import sttp.tapir.files._ import sttp.tapir.server.akkahttp.AkkaHttpServerInterpreter import java.nio.file.{Files, Path, StandardOpenOption} @@ -20,7 +21,7 @@ object StaticContentFromFilesAkkaServer extends App { val exampleDirectory: Path = Files.createTempDirectory("akka-static-example") Files.write(exampleDirectory.resolve("f1"), content.getBytes, StandardOpenOption.CREATE_NEW) - val fileEndpoints = filesServerEndpoints[Future]("range-example")(exampleDirectory.toFile.getAbsolutePath) + val fileEndpoints = staticFilesServerEndpoints[Future]("range-example")(exampleDirectory.toFile.getAbsolutePath) val route: Route = AkkaHttpServerInterpreter().toRoute(fileEndpoints) // starting the server diff --git a/examples/src/main/scala/sttp/tapir/examples/static_content/StaticContentFromFilesNettyServer.scala b/examples/src/main/scala/sttp/tapir/examples/static_content/StaticContentFromFilesNettyServer.scala index e58f9b1943..9d31b266a6 100644 --- a/examples/src/main/scala/sttp/tapir/examples/static_content/StaticContentFromFilesNettyServer.scala +++ b/examples/src/main/scala/sttp/tapir/examples/static_content/StaticContentFromFilesNettyServer.scala @@ -1,7 +1,8 @@ package sttp.tapir.examples.static_content import sttp.tapir.server.netty.NettyFutureServer -import sttp.tapir.{emptyInput, filesServerEndpoints} +import sttp.tapir.emptyInput +import sttp.tapir.files._ import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future @@ -9,7 +10,7 @@ import scala.concurrent.Future object StaticContentFromFilesNettyServer extends App { NettyFutureServer() .port(8080) - .addEndpoints(filesServerEndpoints[Future](emptyInput)("/var/www")) + .addEndpoints(staticFilesServerEndpoints[Future](emptyInput)("/var/www")) .start() .flatMap(_ => Future.never) } diff --git a/examples/src/main/scala/sttp/tapir/examples/static_content/StaticContentFromResourcesAkkaServer.scala b/examples/src/main/scala/sttp/tapir/examples/static_content/StaticContentFromResourcesAkkaServer.scala index 83beadc796..9ab0ce693b 100644 --- a/examples/src/main/scala/sttp/tapir/examples/static_content/StaticContentFromResourcesAkkaServer.scala +++ b/examples/src/main/scala/sttp/tapir/examples/static_content/StaticContentFromResourcesAkkaServer.scala @@ -4,8 +4,8 @@ import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.server.Route import sttp.tapir._ +import sttp.tapir.files._ import sttp.tapir.server.akkahttp.AkkaHttpServerInterpreter -import sttp.tapir.static.ResourcesOptions import scala.concurrent.duration.DurationInt import scala.concurrent.{Await, Future} @@ -16,10 +16,10 @@ object StaticContentFromResourcesAkkaServer extends App { import actorSystem.dispatcher // we're pretending to be a SPA application, that is we serve index.html if the requested resource cannot be found - val resourceEndpoints = resourcesGetServerEndpoint[Future](emptyInput)( + val resourceEndpoints = staticResourcesGetServerEndpoint[Future](emptyInput)( StaticContentFromResourcesAkkaServer.getClass.getClassLoader, "webapp", - ResourcesOptions.default.defaultResource(List("index.html")) + FilesOptions.default.defaultFile(List("index.html")) ) val route: Route = AkkaHttpServerInterpreter().toRoute(resourceEndpoints) diff --git a/examples/src/main/scala/sttp/tapir/examples/static_content/StaticContentSecureAkkaServer.scala b/examples/src/main/scala/sttp/tapir/examples/static_content/StaticContentSecureAkkaServer.scala index 0f442fb7f4..002608d557 100644 --- a/examples/src/main/scala/sttp/tapir/examples/static_content/StaticContentSecureAkkaServer.scala +++ b/examples/src/main/scala/sttp/tapir/examples/static_content/StaticContentSecureAkkaServer.scala @@ -6,6 +6,7 @@ import akka.http.scaladsl.server.Route import sttp.client3._ import sttp.model.StatusCode import sttp.tapir._ +import sttp.tapir.files._ import sttp.tapir.server.akkahttp.AkkaHttpServerInterpreter import java.nio.file.{Files, Path, StandardOpenOption} @@ -21,7 +22,7 @@ object StaticContentSecureAkkaServer extends App { import actorSystem.dispatcher // defining the endpoints - val secureFileEndpoints = filesServerEndpoints[Future]("secure")(exampleDirectory.toFile.getAbsolutePath) + val secureFileEndpoints = staticFilesServerEndpoints[Future]("secure")(exampleDirectory.toFile.getAbsolutePath) .map(_.prependSecurityPure(auth.bearer[String](), statusCode(StatusCode.Forbidden)) { token => // Right means success, Left - an error, here mapped to a constant status code if (token.startsWith("secret")) Right(()) else Left(()) From ac77c7db086149a5a6773ad5f8257a0824da0a6b Mon Sep 17 00:00:00 2001 From: kciesielski Date: Thu, 27 Apr 2023 14:20:31 +0200 Subject: [PATCH 2/2] Additional adjustments to the new files API --- doc/server/logic.md | 3 ++- .../src/main/scala/sttp/tapir/swagger/SwaggerUI.scala | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/doc/server/logic.md b/doc/server/logic.md index dd8c53617a..5665bf7b6d 100644 --- a/doc/server/logic.md +++ b/doc/server/logic.md @@ -202,10 +202,11 @@ before the security logic defined in the endpoint so far (if any). For example: ```scala mdoc:compile-only import sttp.tapir._ +import sttp.tapir.files._ import scala.concurrent.Future import sttp.model.StatusCode -val secureFileEndpoints = filesServerEndpoints[Future]("secure")("/home/data") +val secureFileEndpoints = staticFilesServerEndpoints[Future]("secure")("/home/data") .map(_.prependSecurity(auth.bearer[String](), statusCode(StatusCode.Forbidden)) { token => Future.successful(if (token.startsWith("secret")) Right(()) else Left(())) }) diff --git a/docs/swagger-ui/src/main/scala/sttp/tapir/swagger/SwaggerUI.scala b/docs/swagger-ui/src/main/scala/sttp/tapir/swagger/SwaggerUI.scala index afe325a1a2..1d24434033 100644 --- a/docs/swagger-ui/src/main/scala/sttp/tapir/swagger/SwaggerUI.scala +++ b/docs/swagger-ui/src/main/scala/sttp/tapir/swagger/SwaggerUI.scala @@ -2,6 +2,7 @@ package sttp.tapir.swagger import sttp.model.{HeaderNames, MediaType, StatusCode} import sttp.tapir._ +import sttp.tapir.files._ import sttp.tapir.server.ServerEndpoint import java.util.Properties @@ -56,7 +57,7 @@ object SwaggerUI { val swaggerInitializerJsEndpoint = baseEndpoint.in("swagger-initializer.js").out(textJavascriptUtf8).serverLogicPure[F](_ => Right(swaggerInitializerJsWithReplacedUrl)) - val resourcesEndpoint = resourcesGetServerEndpoint[F](prefixInput)( + val resourcesEndpoint = staticResourcesGetServerEndpoint[F](prefixInput)( SwaggerUI.getClass.getClassLoader, s"META-INF/resources/webjars/swagger-ui/$swaggerVersion/" )