Skip to content

Commit

Permalink
simplify the definition
Browse files Browse the repository at this point in the history
  • Loading branch information
massimosiani committed Jul 20, 2022
1 parent c784b73 commit 8f29651
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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))
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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))
}
}
}

0 comments on commit 8f29651

Please sign in to comment.