Running as a Netty-based server
Using Future
To expose an endpoint using a Netty-based server, first add the following dependency:
"com.softwaremill.sttp.tapir" %% "tapir-netty-server" % "1.0.2"
Then, use:
NettyFutureServer().addEndpoints
to exposeFuture
-based server endpoints.NettyCatsServer().addEndpoints
to exposeF
-based server endpoints, whereF
is any cats-effect supported effect.
These methods require a single, or a list of ServerEndpoint
s, which can be created by adding server logic
to an endpoint.
For example:
import sttp.tapir._
import sttp.tapir.server.netty.NettyServerType.TCP
import sttp.tapir.server.netty.{NettyFutureServer, NettyFutureServerBinding}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import java.net.InetSocketAddress
val helloWorld = endpoint
.get
.in("hello").in(query[String]("name"))
.out(stringBody)
.serverLogic(name => Future.successful[Either[Unit, String]](Right(s"Hello, $name!")))
val binding: Future[NettyFutureServerBinding[TCP]] =
NettyFutureServer().addEndpoint(helloWorld).start()
Domain socket support
There is possibility to use Domain socket instead of TCP for handling traffic.
import sttp.tapir.server.netty.NettyServerType.DomainSocket
import sttp.tapir.server.netty.{NettyFutureServer, NettyFutureServerBinding}
import sttp.tapir.{endpoint, query, stringBody}
import java.nio.file.Paths
import java.util.UUID
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future}
val serverBinding: Future[NettyFutureServerBinding[DomainSocket]] =
NettyFutureServer.domainSocket
.path(Paths.get(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString))
.addEndpoint(
endpoint.get.in("hello").in(query[String]("name")).out(stringBody).serverLogic(name =>
Future.successful[Either[Unit, String]](Right(s"Hello, $name!")))
)
.start()
Configuration
The interpreter can be configured by providing an NettyFutureServerOptions
value, see server options for
details.
Some options can be configured directly using a NettyFutureServer
instance, such as the host and port. Others
can be passed using the NettyFutureServer(options)
methods. Options may also be overridden when adding endpoints.
Using cats-effect
Add the following dependency:
"com.softwaremill.sttp.tapir" %% "tapir-netty-server-cats" % "1.0.2"