Skip to content

Commit

Permalink
Adjust Swagger and examples to the new files module (#2862)
Browse files Browse the repository at this point in the history
  • Loading branch information
kciesielski authored Apr 27, 2023
1 parent 7a9e887 commit 3569f3b
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 9 deletions.
3 changes: 2 additions & 1 deletion doc/server/logic.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(()))
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
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

object StaticContentFromFilesNettyServer extends App {
NettyFutureServer()
.port(8080)
.addEndpoints(filesServerEndpoints[Future](emptyInput)("/var/www"))
.addEndpoints(staticFilesServerEndpoints[Future](emptyInput)("/var/www"))
.start()
.flatMap(_ => Future.never)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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(())
Expand Down

0 comments on commit 3569f3b

Please sign in to comment.