Skip to content

Commit

Permalink
Play: Optimize usages of route (#3842)
Browse files Browse the repository at this point in the history
  • Loading branch information
kciesielski authored Jun 13, 2024
1 parent c5c59e1 commit a8efc67
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 21 deletions.
20 changes: 10 additions & 10 deletions perf-tests/src/main/scala/sttp/tapir/perf/play/Play.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,19 @@ object Play {

def runServer(routes: ActorSystem => Routes): IO[ServerRunner.KillSwitch] = {
implicit lazy val perfActorSystem: ActorSystem = ActorSystem(s"tapir-play")
val playRouter =
Router.from(
List(routes(perfActorSystem)).reduce((a: Routes, b: Routes) => {
val handler: PartialFunction[RequestHeader, Handler] = { case request =>
a.applyOrElse(request, b)
}
handler
})
)
val components = new DefaultPekkoHttpServerComponents {
override lazy val serverConfig: ServerConfig = ServerConfig(port = Some(Port), address = "127.0.0.1", mode = Mode.Test)
override lazy val actorSystem: ActorSystem = perfActorSystem
override def router: Router =
Router.from(
List(routes(actorSystem)).reduce((a: Routes, b: Routes) => {
val handler: PartialFunction[RequestHeader, Handler] = { case request =>
a.applyOrElse(request, b)
}

handler
})
)
override def router: Router = playRouter
}
IO(components.server).map(server => IO(server.stop()))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PlayServerWithContextTest(backend: SttpBackend[IO, Fs2Streams[IO] with Web
val components = new DefaultPekkoHttpServerComponents {
override lazy val serverConfig: ServerConfig = ServerConfig(port = Some(0), address = "127.0.0.1", mode = Mode.Test)
override lazy val actorSystem: ActorSystem = ActorSystem("tapir", defaultExecutionContext = Some(_actorSystem.dispatcher))
override def router: Router = Router.from(PlayServerInterpreter().toRoutes(e)).withPrefix("/test")
override lazy val router: Router = Router.from(PlayServerInterpreter().toRoutes(e)).withPrefix("/test")
}
val s = components.server
val r = Future.successful(()).flatMap { _ =>
Expand All @@ -49,7 +49,7 @@ class PlayServerWithContextTest(backend: SttpBackend[IO, Fs2Streams[IO] with Web
val components = new DefaultPekkoHttpServerComponents {
override lazy val serverConfig: ServerConfig = ServerConfig(port = Some(0), address = "127.0.0.1", mode = Mode.Test)
override lazy val actorSystem: ActorSystem = ActorSystem("tapir", defaultExecutionContext = Some(_actorSystem.dispatcher))
override def router: Router = Router.from(PlayServerInterpreter().toRoutes(e)).withPrefix("/test")
override lazy val router: Router = Router.from(PlayServerInterpreter().toRoutes(e)).withPrefix("/test")
}
val s = components.server
val r = basicRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class PlayTestServerInterpreter(implicit actorSystem: ActorSystem)
initialServerConfig.copy(configuration = customConf.withFallback(initialServerConfig.configuration))
override lazy val actorSystem: ActorSystem =
ActorSystem("tapir", defaultExecutionContext = Some(PlayTestServerInterpreter.this.actorSystem.dispatcher))
override def router: Router =
override lazy val router: Router =
Router.from(
routes.reduce((a: Routes, b: Routes) => {
val handler: PartialFunction[RequestHeader, Handler] = { case request =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package sttp.tapir.server.play

import akka.actor.ActorSystem
import enumeratum._
import akka.stream.scaladsl.{Flow, Sink, Source}
import cats.data.NonEmptyList
import cats.effect.{IO, Resource}
Expand All @@ -17,8 +16,6 @@ import sttp.tapir.server.tests._
import sttp.tapir.tests.{Test, TestSuite}

import scala.concurrent.Future
import sttp.tapir.codec.enumeratum.TapirCodecEnumeratum
import sttp.tapir.server.interceptor.decodefailure.DefaultDecodeFailureHandler

class PlayServerTest extends TestSuite {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PlayServerWithContextTest(backend: SttpBackend[IO, Fs2Streams[IO] with Web
val components = new DefaultAkkaHttpServerComponents {
override lazy val serverConfig: ServerConfig = ServerConfig(port = Some(0), address = "127.0.0.1", mode = Mode.Test)
override lazy val actorSystem: ActorSystem = ActorSystem("tapir", defaultExecutionContext = Some(_actorSystem.dispatcher))
override def router: Router = Router.from(PlayServerInterpreter().toRoutes(e)).withPrefix("/test")
override lazy val router: Router = Router.from(PlayServerInterpreter().toRoutes(e)).withPrefix("/test")
}
val s = components.server
val r = Future.successful(()).flatMap { _ =>
Expand All @@ -49,7 +49,7 @@ class PlayServerWithContextTest(backend: SttpBackend[IO, Fs2Streams[IO] with Web
val components = new DefaultAkkaHttpServerComponents {
override lazy val serverConfig: ServerConfig = ServerConfig(port = Some(0), address = "127.0.0.1", mode = Mode.Test)
override lazy val actorSystem: ActorSystem = ActorSystem("tapir", defaultExecutionContext = Some(_actorSystem.dispatcher))
override def router: Router = Router.from(PlayServerInterpreter().toRoutes(e)).withPrefix("/test")
override lazy val router: Router = Router.from(PlayServerInterpreter().toRoutes(e)).withPrefix("/test")
}
val s = components.server
val r = basicRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ class PlayTestServerInterpreter(implicit actorSystem: ActorSystem)
PlayServerInterpreter(serverOptions).toRoutes(es)
}

import play.core.server.AkkaHttpServer

override def serverWithStop(
routes: NonEmptyList[Routes],
gracefulShutdownTimeout: Option[FiniteDuration]
Expand All @@ -48,7 +46,7 @@ class PlayTestServerInterpreter(implicit actorSystem: ActorSystem)
initialServerConfig.copy(configuration = customConf.withFallback(initialServerConfig.configuration))
override lazy val actorSystem: ActorSystem =
ActorSystem("tapir", defaultExecutionContext = Some(PlayTestServerInterpreter.this.actorSystem.dispatcher))
override def router: Router =
override lazy val router: Router =
Router.from(
routes.reduce((a: Routes, b: Routes) => {
val handler: PartialFunction[RequestHeader, Handler] = { case request =>
Expand Down

0 comments on commit a8efc67

Please sign in to comment.