添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
鼻子大的苹果  ·  UnicodeDecodeError: ...·  1 月前    · 
气势凌人的牛腩  ·  Rclone task scheduler ...·  4 月前    · 
任性的鸡蛋  ·  WPS协同办公联盟启动 ...·  4 月前    · 
老实的草稿本  ·  傅里叶变换 | brezezee·  5 月前    · 
  • Running as an akka-http server
  • Running as an http4s server
  • Running as an http4s server using ZIO
  • Running as a Netty-based server
  • Running as a Helidon Níma server
  • Running as a Finatra server
  • Running as a pekko-http server
  • Running as a Play server
  • Running as a Vert.X server
    • Scala’s standard Future
    • Configuration
    • Defining an endpoint together with the server logic
    • Cats Effect typeclasses
    • Running as a zio-http server
    • Running as an Armeria server
    • Running as a JDK http server
    • Running using the AWS serverless stack
    • Server options
    • Path matching
    • Interceptors
    • Server logic
    • Observability
    • Error handling
    • Logging & debugging
    • Client interpreters

    • Using as an sttp client
    • Using as a Play client
    • Using as an http4s client
    • Documentation interpreters

    • Generating OpenAPI documentation
    • Generating AsyncAPI documentation
    • Generating JSON Schema
    • Testing

    • Testing
    • Generators

    • Generate endpoint definitions from an OpenAPI YAML
    • Other subjects

    • Other interpreters
    • Creating your own tapir
    • Troubleshooting
    • Migrating
    • Goals of the project
    • Contributing
    • Running as a Vert.X server

      Endpoints can be mounted as Vert.x Route s on top of a Vert.x Router .

      Vert.x interpreter can be used with different effect systems (cats-effect, ZIO) as well as Scala’s standard Future .

      Scala’s standard Future

      Add the following dependency

      "com.softwaremill.sttp.tapir" %% "tapir-vertx-server" % "1.10.7"
      

      to use this interpreter with Future.

      Then import the object:

      import sttp.tapir.server.vertx.VertxFutureServerInterpreter._
      

      This object contains the following methods:

    • route(e: ServerEndpoint[Any, Future]): returns a function Router => Route that will create a route with a handler attached, matching the endpoint definition. Errors will be recovered automatically (but generically)

    • blockingRoute(e: ServerEndpoint[Any, Future]): returns a function Router => Route that will create a route with a blocking handler attached, matching the endpoint definition. Errors will be recovered automatically (but generically)

    • In practice, routes will be mounted on a router, this router can then be used as a request handler for your http server. An HTTP server can then be started as in the following example:

      import sttp.tapir._
      import sttp.tapir.server.vertx.VertxFutureServerInterpreter
      import sttp.tapir.server.vertx.VertxFutureServerInterpreter._
      import io.vertx.core.Vertx
      import io.vertx.ext.web._
      import scala.concurrent.{Await, Future}
      import scala.concurrent.duration._
      object Main {
        // JVM entry point that starts the HTTP server
        def main(args: Array[String]): Unit = {
          val vertx = Vertx.vertx()
          val server = vertx.createHttpServer()
          val router = Router.router(vertx)
          val anEndpoint: PublicEndpoint[(String, Int), Unit, String, Any] = ??? // your definition here
          def logic(s: String, i: Int): Future[Either[Unit, String]] = ??? // your logic here
          val attach = VertxFutureServerInterpreter().route(anEndpoint.serverLogic((logic _).tupled))
          attach(router) // your endpoint is now attached to the router, and the route has been created
          Await.result(server.requestHandler(router).listen(9000).asScala, Duration.Inf)
      

      Configuration

      Every endpoint can be configured by providing an instance of VertxFutureEndpointOptions, see server options for details. You can also provide your own ExecutionContext to execute the logic.

      Defining an endpoint together with the server logic

      It’s also possible to define an endpoint together with the server logic in a single, more concise step. See server logic for details.

      Cats Effect typeclasses

      Add the following dependency

      "com.softwaremill.sttp.tapir" %% "tapir-vertx-server-cats" % "1.10.7"
      

      to use this interpreter with Cats Effect typeclasses.

      Then import the object:

      import sttp.tapir.server.vertx.cats.VertxCatsServerInterpreter._
      

      This object contains the route[F[_]](e: ServerEndpoint[Fs2Streams[F], F]) method, which returns a function Router => Route that will create a route, with a handler attached, matching the endpoint definition. Errors will be recovered automatically.

      Here is simple example which starts HTTP server with one route:

      import cats.effect._
      import cats.effect.std.
      
      
      
      
          
      Dispatcher
      import io.vertx.core.Vertx
      import io.vertx.ext.web.Router
      import sttp.tapir._
      import sttp.tapir.server.vertx.cats.VertxCatsServerInterpreter
      import sttp.tapir.server.vertx.cats.VertxCatsServerInterpreter._
      object App extends IOApp {
        val responseEndpoint: PublicEndpoint[String, Unit, String, Any] =
          endpoint
            .in("response")
            .in(query[String]("key"))
            .out(plainBody[String])
        def handler(req: String): IO[Either[Unit, String]] =
          IO.pure(Right(req))
        override def run(args: List[String]): IO[ExitCode] = {
          Dispatcher[IO]
            .flatMap { dispatcher =>
              Resource
                .make(
                  IO.delay {
                    val vertx = Vertx.vertx()
                    val server = vertx.createHttpServer()
                    val router = Router.router(vertx)
                    val attach = VertxCatsServerInterpreter[IO](dispatcher).route(responseEndpoint.serverLogic(handler))
                    attach(router)
                    server.requestHandler(router).listen(8080)
                  }.flatMap(_.asF[IO])
                )({ server =>
                  IO.delay(server.close).flatMap(_.asF[IO].void)
            .use(_ => IO.never)
      

      This interpreter also supports streaming using FS2 streams:

      import cats.effect._
      import cats.effect.std.Dispatcher
      import fs2._
      import sttp.capabilities.fs2.Fs2Streams
      import sttp.tapir._
      import sttp.tapir.server.vertx.cats.VertxCatsServerInterpreter
      val streamedResponse =
        endpoint
          .in("stream")
          .in(query[Int]("key"))
          .out(streamTextBody(Fs2Streams[IO])(CodecFormat.TextPlain()))
      def dispatcher: Dispatcher[IO] = ???
      val attach = VertxCatsServerInterpreter(dispatcher).route(streamedResponse.serverLogicSuccess[IO] { key =>
        IO.pure(Stream.chunk(Chunk.array("Hello world!".getBytes)).repeatN(key))
      

      ZIO

      Add the following dependency

      "com.softwaremill.sttp.tapir" %% "tapir-vertx-server-zio" % "1.10.7"
      

      to use this interpreter with ZIO.

      Then import the object:

      import sttp.tapir.server.vertx.zio.VertxZioServerInterpreter._
      

      This object contains method def route(e: ServerEndpoint[ZioStreams, RIO[R, *]]) which returns a function Router => Route that will create a route matching the endpoint definition, and with the logic attached as a handler.

      Here is simple example which starts HTTP server with one route:

      import io.vertx.core.Vertx
      import io.vertx.ext.web.Router
      import sttp.tapir.{plainBody, query}
      import sttp.tapir.ztapir._
      import sttp.tapir.server.vertx.zio.VertxZioServerInterpreter
      import sttp.tapir.server.vertx.zio.VertxZioServerInterpreter._
      import zio._
      object Short extends ZIOAppDefault {
        override implicit val runtime = zio.Runtime.default
        val responseEndpoint =
          endpoint
            .in("response")
            .in(query[String]("key"))
            .out(plainBody[String])
        val attach = VertxZioServerInterpreter().route(responseEndpoint.zServerLogic { key => ZIO.succeed(key) })
        override def run = {
          ZIO.scoped(
              .acquireRelease(
                  .attempt {
                    val vertx = Vertx.vertx()
                    val server = vertx.createHttpServer()
                    val router = Router.router(vertx)
                    attach(router)
                    server.requestHandler(router).listen(8080)
                  .flatMap(_.asRIO)
              ) { server =>
                ZIO.attempt(server.close()).flatMap(_.asRIO).orDie
              } *> ZIO.never
      

      This interpreter supports streaming using ZStreams.

  •