From 8f296514f3d0424d3d2071bcde671247f925509d Mon Sep 17 00:00:00 2001 From: Massimo Siani Date: Wed, 20 Jul 2022 19:20:12 +0200 Subject: [PATCH] simplify the definition --- .../akka/http/examples/tapir/Main.scala | 16 ++++----- .../scala/natchez/akka/http/AkkaRoute.scala | 35 +++++++++++++++++++ 2 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 natchez-akka-http/shared/src/main/scala/natchez/akka/http/AkkaRoute.scala diff --git a/examples/tapir/shared/src/main/scala/natchez/akka/http/examples/tapir/Main.scala b/examples/tapir/shared/src/main/scala/natchez/akka/http/examples/tapir/Main.scala index 9241211..38cd930 100644 --- a/examples/tapir/shared/src/main/scala/natchez/akka/http/examples/tapir/Main.scala +++ b/examples/tapir/shared/src/main/scala/natchez/akka/http/examples/tapir/Main.scala @@ -24,10 +24,10 @@ import cats.effect.std.Dispatcher import cats.effect.unsafe.implicits.global import cats.effect.{IO, IOApp, Resource} import cats.~> -import natchez.akka.http.NatchezAkkaHttp +import natchez.akka.http.AkkaRoute import natchez.akka.http.entrypoint.toEntryPointOps import natchez.log.Log -import natchez.{EntryPoint, Span, Trace} +import natchez.{EntryPoint, Span} import org.typelevel.log4cats.Logger import org.typelevel.log4cats.slf4j.Slf4jLogger import sttp.tapir.integ.cats.syntax.* @@ -61,14 +61,10 @@ object Main extends IOApp.Simple { // lift the Route in a Kleisli to pass the span around implicitly // use the Trace constraint on the services - val liftedRoutes: Kleisli[IO, Span[IO], Route] = Kleisli { span => - Trace.ioTrace(span).flatMap { implicit t => - IO.executionContext.flatMap { implicit ec => - val helloService = new HelloService[IO] - val helloRoute: Route = - AkkaHttpServerInterpreter().toRoute(helloService.helloEndpoint.imapK(toFuture)(fromFuture)) - NatchezAkkaHttp.server(IO.delay(helloRoute)) - } + val liftedRoutes: Kleisli[IO, Span[IO], Route] = AkkaRoute.liftedRouteIO { implicit t => + IO.executionContext.map { implicit ec => + val helloService = new HelloService[IO] + AkkaHttpServerInterpreter().toRoute(helloService.helloEndpoint.imapK(toFuture)(fromFuture)) } } diff --git a/natchez-akka-http/shared/src/main/scala/natchez/akka/http/AkkaRoute.scala b/natchez-akka-http/shared/src/main/scala/natchez/akka/http/AkkaRoute.scala new file mode 100644 index 0000000..94bf279 --- /dev/null +++ b/natchez-akka-http/shared/src/main/scala/natchez/akka/http/AkkaRoute.scala @@ -0,0 +1,35 @@ +/* + * Copyright 2022 Massimo Siani + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package natchez.akka.http + +import akka.http.scaladsl.server.Route +import cats.data.Kleisli +import cats.effect.IO +import cats.effect.std.Dispatcher +import natchez.{Span, Trace} + +object AkkaRoute { + def liftedRoute(f: Trace[IO] => Route)(implicit D: Dispatcher[IO]): Kleisli[IO, Span[IO], Route] = + liftedRouteIO(t => IO.delay(f(t))) + + def liftedRouteIO(f: Trace[IO] => IO[Route])(implicit D: Dispatcher[IO]): Kleisli[IO, Span[IO], Route] = Kleisli { + span => + Trace.ioTrace(span).flatMap { implicit t => + NatchezAkkaHttp.server(f(t)) + } + } +}