Skip to content

Commit 8f29651

Browse files
committed
simplify the definition
1 parent c784b73 commit 8f29651

File tree

2 files changed

+41
-10
lines changed
  • examples/tapir/shared/src/main/scala/natchez/akka/http/examples/tapir
  • natchez-akka-http/shared/src/main/scala/natchez/akka/http

2 files changed

+41
-10
lines changed

examples/tapir/shared/src/main/scala/natchez/akka/http/examples/tapir/Main.scala

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import cats.effect.std.Dispatcher
2424
import cats.effect.unsafe.implicits.global
2525
import cats.effect.{IO, IOApp, Resource}
2626
import cats.~>
27-
import natchez.akka.http.NatchezAkkaHttp
27+
import natchez.akka.http.AkkaRoute
2828
import natchez.akka.http.entrypoint.toEntryPointOps
2929
import natchez.log.Log
30-
import natchez.{EntryPoint, Span, Trace}
30+
import natchez.{EntryPoint, Span}
3131
import org.typelevel.log4cats.Logger
3232
import org.typelevel.log4cats.slf4j.Slf4jLogger
3333
import sttp.tapir.integ.cats.syntax.*
@@ -61,14 +61,10 @@ object Main extends IOApp.Simple {
6161

6262
// lift the Route in a Kleisli to pass the span around implicitly
6363
// use the Trace constraint on the services
64-
val liftedRoutes: Kleisli[IO, Span[IO], Route] = Kleisli { span =>
65-
Trace.ioTrace(span).flatMap { implicit t =>
66-
IO.executionContext.flatMap { implicit ec =>
67-
val helloService = new HelloService[IO]
68-
val helloRoute: Route =
69-
AkkaHttpServerInterpreter().toRoute(helloService.helloEndpoint.imapK(toFuture)(fromFuture))
70-
NatchezAkkaHttp.server(IO.delay(helloRoute))
71-
}
64+
val liftedRoutes: Kleisli[IO, Span[IO], Route] = AkkaRoute.liftedRouteIO { implicit t =>
65+
IO.executionContext.map { implicit ec =>
66+
val helloService = new HelloService[IO]
67+
AkkaHttpServerInterpreter().toRoute(helloService.helloEndpoint.imapK(toFuture)(fromFuture))
7268
}
7369
}
7470

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2022 Massimo Siani
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package natchez.akka.http
18+
19+
import akka.http.scaladsl.server.Route
20+
import cats.data.Kleisli
21+
import cats.effect.IO
22+
import cats.effect.std.Dispatcher
23+
import natchez.{Span, Trace}
24+
25+
object AkkaRoute {
26+
def liftedRoute(f: Trace[IO] => Route)(implicit D: Dispatcher[IO]): Kleisli[IO, Span[IO], Route] =
27+
liftedRouteIO(t => IO.delay(f(t)))
28+
29+
def liftedRouteIO(f: Trace[IO] => IO[Route])(implicit D: Dispatcher[IO]): Kleisli[IO, Span[IO], Route] = Kleisli {
30+
span =>
31+
Trace.ioTrace(span).flatMap { implicit t =>
32+
NatchezAkkaHttp.server(f(t))
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)