-
Notifications
You must be signed in to change notification settings - Fork 18
/
TapirToPekkoInterpreter.scala
51 lines (42 loc) · 1.89 KB
/
TapirToPekkoInterpreter.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Copyright © 2021 - 2024 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
* SPDX-License-Identifier: Apache-2.0
*/
package org.knora.webapi.slice.common.api
import org.apache.pekko.actor.ActorSystem
import org.apache.pekko.http.scaladsl.server.Route
import sttp.capabilities.WebSockets
import sttp.capabilities.pekko.PekkoStreams
import sttp.tapir.generic.auto.*
import sttp.tapir.json.zio.jsonBody
import sttp.tapir.server.ServerEndpoint
import sttp.tapir.server.metrics.zio.ZioMetrics
import sttp.tapir.server.model.ValuedEndpointOutput
import sttp.tapir.server.pekkohttp.PekkoHttpServerInterpreter
import sttp.tapir.server.pekkohttp.PekkoHttpServerOptions
import zio.ZLayer
import zio.json.DeriveJsonCodec
import zio.json.JsonCodec
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
final case class TapirToPekkoInterpreter()(system: ActorSystem) {
implicit val executionContext: ExecutionContext = system.dispatcher
private case class GenericErrorResponse(error: String)
private object GenericErrorResponse {
implicit val codec: JsonCodec[GenericErrorResponse] = DeriveJsonCodec.gen[GenericErrorResponse]
}
private def customizedErrorResponse(m: String): ValuedEndpointOutput[?] =
ValuedEndpointOutput(jsonBody[GenericErrorResponse], GenericErrorResponse(m))
private val serverOptions =
PekkoHttpServerOptions.customiseInterceptors
.defaultHandlers(customizedErrorResponse)
.metricsInterceptor(ZioMetrics.default[Future]().metricsInterceptor())
.notAcceptableInterceptor(None)
.options
private val interpreter: PekkoHttpServerInterpreter = PekkoHttpServerInterpreter(serverOptions)
def toRoute(endpoint: ServerEndpoint[PekkoStreams & WebSockets, Future]): Route =
interpreter.toRoute(endpoint)
}
object TapirToPekkoInterpreter {
val layer = ZLayer.derive[TapirToPekkoInterpreter]
}