Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renamed NettyId to NettySync #3689

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions doc/server/netty.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ To expose an endpoint using a [Netty](https://netty.io)-based server, first add
Then, use:

- `NettyFutureServer().addEndpoints` to expose `Future`-based server endpoints.
- `NettyIdServer().addEndpoints` to expose `Loom`-based server endpoints.
- `NettySyncServer().addEndpoints` to expose `Loom`-based server endpoints.
- `NettyCatsServer().addEndpoints` to expose `F`-based server endpoints, where `F` is any cats-effect supported effect. [Streaming](../endpoint/streaming.md) request and response bodies is supported with fs2.
- `NettyZioServer().addEndpoints` to expose `ZIO`-based server endpoints, where `R` represents ZIO requirements supported effect. Streaming is supported with ZIO Streams.

Expand Down Expand Up @@ -48,16 +48,16 @@ The `tapir-netty-server-loom` server uses `Id[T]` as its wrapper effect for comp

```scala
import sttp.tapir._
import sttp.tapir.server.netty.loom.{Id, NettyIdServer, NettyIdServerBinding}
import sttp.tapir.server.netty.loom.{Id, NettySyncServer, NettySyncServerBinding}

val helloWorld = endpoint
.get
.in("hello").in(query[String]("name"))
.out(stringBody)
.serverLogicSuccess[Id](name => s"Hello, $name!")

val binding: NettyIdServerBinding =
NettyIdServer().addEndpoint(helloWorld).start()
val binding: NettySyncServerBinding =
NettySyncServer().addEndpoint(helloWorld).start()
```

## Configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import sttp.tapir.server.ServerEndpoint

object Tapir extends Endpoints

object NettyId {
object NettySync {

def runServer(endpoints: List[ServerEndpoint[Any, Id]], withServerLog: Boolean = false): IO[ServerRunner.KillSwitch] = {
val declaredPort = Port
val declaredHost = "0.0.0.0"
val serverOptions = buildOptions(NettyIdServerOptions.customiseInterceptors, withServerLog)
val serverOptions = buildOptions(NettySyncServerOptions.customiseInterceptors, withServerLog)
// Starting netty server
val serverBinding: NettyIdServerBinding =
NettyIdServer(serverOptions)
val serverBinding: NettySyncServerBinding =
NettySyncServer(serverOptions)
.port(declaredPort)
.host(declaredHost)
.addEndpoints(endpoints)
Expand All @@ -25,8 +25,8 @@ object NettyId {
}
}

object TapirServer extends ServerRunner { override def start = NettyId.runServer(Tapir.genEndpointsId(1)) }
object TapirMultiServer extends ServerRunner { override def start = NettyId.runServer(Tapir.genEndpointsId(128)) }
object TapirServer extends ServerRunner { override def start = NettySync.runServer(Tapir.genEndpointsId(1)) }
object TapirMultiServer extends ServerRunner { override def start = NettySync.runServer(Tapir.genEndpointsId(128)) }
object TapirInterceptorMultiServer extends ServerRunner {
override def start = NettyId.runServer(Tapir.genEndpointsId(128), withServerLog = true)
override def start = NettySync.runServer(Tapir.genEndpointsId(128), withServerLog = true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import sttp.tapir.model.ServerRequest
import sttp.tapir.server.netty.internal.NettyRequestBody
import sttp.tapir.server.netty.internal.reactivestreams.{FileWriterSubscriber, SimpleSubscriber}

private[netty] class NettyIdRequestBody(val createFile: ServerRequest => TapirFile) extends NettyRequestBody[Id, NoStreams] {
private[netty] class NettySyncRequestBody(val createFile: ServerRequest => TapirFile) extends NettyRequestBody[Id, NoStreams] {

override implicit val monad: MonadError[Id] = idMonad
override val streams: capabilities.Streams[NoStreams] = NoStreams
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,40 @@ import scala.concurrent.Promise
import scala.concurrent.duration.FiniteDuration
import scala.util.control.NonFatal

case class NettyIdServer(routes: Vector[IdRoute], options: NettyIdServerOptions, config: NettyConfig) {
case class NettySyncServer(routes: Vector[IdRoute], options: NettySyncServerOptions, config: NettyConfig) {
private val executor = Executors.newVirtualThreadPerTaskExecutor()

def addEndpoint(se: ServerEndpoint[Any, Id]): NettyIdServer = addEndpoints(List(se))
def addEndpoint(se: ServerEndpoint[Any, Id], overrideOptions: NettyIdServerOptions): NettyIdServer =
def addEndpoint(se: ServerEndpoint[Any, Id]): NettySyncServer = addEndpoints(List(se))
def addEndpoint(se: ServerEndpoint[Any, Id], overrideOptions: NettySyncServerOptions): NettySyncServer =
addEndpoints(List(se), overrideOptions)
def addEndpoints(ses: List[ServerEndpoint[Any, Id]]): NettyIdServer = addRoute(NettyIdServerInterpreter(options).toRoute(ses))
def addEndpoints(ses: List[ServerEndpoint[Any, Id]], overrideOptions: NettyIdServerOptions): NettyIdServer =
addRoute(NettyIdServerInterpreter(overrideOptions).toRoute(ses))
def addEndpoints(ses: List[ServerEndpoint[Any, Id]]): NettySyncServer = addRoute(NettySyncServerInterpreter(options).toRoute(ses))
def addEndpoints(ses: List[ServerEndpoint[Any, Id]], overrideOptions: NettySyncServerOptions): NettySyncServer =
addRoute(NettySyncServerInterpreter(overrideOptions).toRoute(ses))

def addRoute(r: IdRoute): NettyIdServer = copy(routes = routes :+ r)
def addRoutes(r: Iterable[IdRoute]): NettyIdServer = copy(routes = routes ++ r)
def addRoute(r: IdRoute): NettySyncServer = copy(routes = routes :+ r)
def addRoutes(r: Iterable[IdRoute]): NettySyncServer = copy(routes = routes ++ r)

def options(o: NettyIdServerOptions): NettyIdServer = copy(options = o)
def config(c: NettyConfig): NettyIdServer = copy(config = c)
def modifyConfig(f: NettyConfig => NettyConfig): NettyIdServer = config(f(config))
def options(o: NettySyncServerOptions): NettySyncServer = copy(options = o)
def config(c: NettyConfig): NettySyncServer = copy(config = c)
def modifyConfig(f: NettyConfig => NettyConfig): NettySyncServer = config(f(config))

def host(hostname: String): NettyIdServer = modifyConfig(_.host(hostname))
def host(hostname: String): NettySyncServer = modifyConfig(_.host(hostname))

def port(p: Int): NettyIdServer = modifyConfig(_.port(p))
def port(p: Int): NettySyncServer = modifyConfig(_.port(p))

def start(): NettyIdServerBinding =
def start(): NettySyncServerBinding =
startUsingSocketOverride[InetSocketAddress](None) match {
case (socket, stop) =>
NettyIdServerBinding(socket, stop)
NettySyncServerBinding(socket, stop)
}

def startUsingDomainSocket(path: Option[Path] = None): NettyIdDomainSocketBinding =
def startUsingDomainSocket(path: Option[Path] = None): NettySyncDomainSocketBinding =
startUsingDomainSocket(path.getOrElse(Paths.get(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString)))

def startUsingDomainSocket(path: Path): NettyIdDomainSocketBinding =
def startUsingDomainSocket(path: Path): NettySyncDomainSocketBinding =
startUsingSocketOverride(Some(new DomainSocketAddress(path.toFile))) match {
case (socket, stop) =>
NettyIdDomainSocketBinding(socket, stop)
NettySyncDomainSocketBinding(socket, stop)
}

private def startUsingSocketOverride[SA <: SocketAddress](socketOverride: Option[SA]): (SA, () => Unit) = {
Expand Down Expand Up @@ -174,22 +174,22 @@ case class NettyIdServer(routes: Vector[IdRoute], options: NettyIdServerOptions,
}
}

object NettyIdServer {
def apply(): NettyIdServer = NettyIdServer(Vector.empty, NettyIdServerOptions.default, NettyConfig.default)
object NettySyncServer {
def apply(): NettySyncServer = NettySyncServer(Vector.empty, NettySyncServerOptions.default, NettyConfig.default)

def apply(serverOptions: NettyIdServerOptions): NettyIdServer =
NettyIdServer(Vector.empty, serverOptions, NettyConfig.default)
def apply(serverOptions: NettySyncServerOptions): NettySyncServer =
NettySyncServer(Vector.empty, serverOptions, NettyConfig.default)

def apply(config: NettyConfig): NettyIdServer =
NettyIdServer(Vector.empty, NettyIdServerOptions.default, config)
def apply(config: NettyConfig): NettySyncServer =
NettySyncServer(Vector.empty, NettySyncServerOptions.default, config)

def apply(serverOptions: NettyIdServerOptions, config: NettyConfig): NettyIdServer =
NettyIdServer(Vector.empty, serverOptions, config)
def apply(serverOptions: NettySyncServerOptions, config: NettyConfig): NettySyncServer =
NettySyncServer(Vector.empty, serverOptions, config)
}
case class NettyIdServerBinding(localSocket: InetSocketAddress, stop: () => Unit) {
case class NettySyncServerBinding(localSocket: InetSocketAddress, stop: () => Unit) {
def hostName: String = localSocket.getHostName
def port: Int = localSocket.getPort
}
case class NettyIdDomainSocketBinding(localSocket: DomainSocketAddress, stop: () => Unit) {
case class NettySyncDomainSocketBinding(localSocket: DomainSocketAddress, stop: () => Unit) {
def path: String = localSocket.path()
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ package sttp.tapir.server.netty.loom
import sttp.tapir.server.ServerEndpoint
import sttp.tapir.server.netty.internal.{NettyToResponseBody, NettyServerInterpreter, RunAsync}

trait NettyIdServerInterpreter {
def nettyServerOptions: NettyIdServerOptions
trait NettySyncServerInterpreter {
def nettyServerOptions: NettySyncServerOptions

def toRoute(
ses: List[ServerEndpoint[Any, Id]]
): IdRoute = {
NettyServerInterpreter.toRoute[Id](
ses,
nettyServerOptions.interceptors,
new NettyIdRequestBody(nettyServerOptions.createFile),
new NettySyncRequestBody(nettyServerOptions.createFile),
new NettyToResponseBody[Id](RunAsync.Id),
nettyServerOptions.deleteFile,
RunAsync.Id
)
}
}

object NettyIdServerInterpreter {
def apply(serverOptions: NettyIdServerOptions = NettyIdServerOptions.default): NettyIdServerInterpreter = {
new NettyIdServerInterpreter {
override def nettyServerOptions: NettyIdServerOptions = serverOptions
object NettySyncServerInterpreter {
def apply(serverOptions: NettySyncServerOptions = NettySyncServerOptions.default): NettySyncServerInterpreter = {
new NettySyncServerInterpreter {
override def nettyServerOptions: NettySyncServerOptions = serverOptions
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,36 @@ import sttp.tapir.server.netty.internal.NettyDefaults
import sttp.tapir.server.interceptor.{CustomiseInterceptors, Interceptor}
import sttp.tapir.{Defaults, TapirFile}

case class NettyIdServerOptions(
case class NettySyncServerOptions(
interceptors: List[Interceptor[Id]],
createFile: ServerRequest => TapirFile,
deleteFile: TapirFile => Unit
) {
def prependInterceptor(i: Interceptor[Id]): NettyIdServerOptions = copy(interceptors = i :: interceptors)
def appendInterceptor(i: Interceptor[Id]): NettyIdServerOptions = copy(interceptors = interceptors :+ i)
def prependInterceptor(i: Interceptor[Id]): NettySyncServerOptions = copy(interceptors = i :: interceptors)
def appendInterceptor(i: Interceptor[Id]): NettySyncServerOptions = copy(interceptors = interceptors :+ i)
}

object NettyIdServerOptions {
object NettySyncServerOptions {

/** Default options, using TCP sockets (the most common case). This can be later customised using [[NettyIdServerOptions#nettyOptions()]].
/** Default options, using TCP sockets (the most common case). This can be later customised using [[NettySyncServerOptions#nettyOptions()]].
*/
def default: NettyIdServerOptions = customiseInterceptors.options
def default: NettySyncServerOptions = customiseInterceptors.options

private def default(
interceptors: List[Interceptor[Id]]
): NettyIdServerOptions =
NettyIdServerOptions(
): NettySyncServerOptions =
NettySyncServerOptions(
interceptors,
_ => Defaults.createTempFile(),
Defaults.deleteFile()
)

/** Customise the interceptors that are being used when exposing endpoints as a server. By default uses TCP sockets (the most common
* case), but this can be later customised using [[NettyIdServerOptions#nettyOptions()]].
* case), but this can be later customised using [[NettySyncServerOptions#nettyOptions()]].
*/
def customiseInterceptors: CustomiseInterceptors[Id, NettyIdServerOptions] = {
def customiseInterceptors: CustomiseInterceptors[Id, NettySyncServerOptions] = {
CustomiseInterceptors(
createOptions = (ci: CustomiseInterceptors[Id, NettyIdServerOptions]) => default(ci.interceptors)
createOptions = (ci: CustomiseInterceptors[Id, NettySyncServerOptions]) => default(ci.interceptors)
).serverLog(defaultServerLog)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import sttp.tapir.tests.{Test, TestSuite}
import scala.concurrent.Future
import scala.concurrent.duration.FiniteDuration

class NettyIdServerTest extends TestSuite with EitherValues {
class NettySyncServerTest extends TestSuite with EitherValues {
override def tests: Resource[IO, List[Test]] =
backendResource.flatMap { backend =>
Resource
.make(IO.delay {
val eventLoopGroup = new NioEventLoopGroup()

val interpreter = new NettyIdTestServerInterpreter(eventLoopGroup)
val interpreter = new NettySyncTestServerInterpreter(eventLoopGroup)
val createServerTest = new DefaultCreateServerTest(backend, interpreter)
val sleeper: Sleeper[Id] = (duration: FiniteDuration) => Thread.sleep(duration.toMillis)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import sttp.tapir.tests.Port

import scala.concurrent.duration.FiniteDuration

class NettyIdTestServerInterpreter(eventLoopGroup: NioEventLoopGroup)
extends TestServerInterpreter[Id, Any, NettyIdServerOptions, IdRoute] {
class NettySyncTestServerInterpreter(eventLoopGroup: NioEventLoopGroup)
extends TestServerInterpreter[Id, Any, NettySyncServerOptions, IdRoute] {
override def route(es: List[ServerEndpoint[Any, Id]], interceptors: Interceptors): IdRoute = {
val serverOptions: NettyIdServerOptions = interceptors(NettyIdServerOptions.customiseInterceptors).options
NettyIdServerInterpreter(serverOptions).toRoute(es)
val serverOptions: NettySyncServerOptions = interceptors(NettySyncServerOptions.customiseInterceptors).options
NettySyncServerInterpreter(serverOptions).toRoute(es)
}

override def serverWithStop(
Expand All @@ -24,8 +24,8 @@ class NettyIdTestServerInterpreter(eventLoopGroup: NioEventLoopGroup)
val config =
NettyConfig.default.eventLoopGroup(eventLoopGroup).randomPort.withDontShutdownEventLoopGroupOnClose.noGracefulShutdown
val customizedConfig = gracefulShutdownTimeout.map(config.withGracefulShutdownTimeout).getOrElse(config)
val options = NettyIdServerOptions.default
val bind = IO.blocking(NettyIdServer(options, customizedConfig).addRoutes(routes.toList).start())
val options = NettySyncServerOptions.default
val bind = IO.blocking(NettySyncServer(options, customizedConfig).addRoutes(routes.toList).start())

Resource
.make(bind.map(b => (b.port, IO.blocking(b.stop())))) { case (_, stop) => stop }
Expand Down
Loading