This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def addBookLogic(b: Book, authToken: String): Future[Either[Unit, Boolean]] = ... | |
val addBook: Endpoint[(Book, String), Unit, Boolean, Nothing] = ... | |
import tapir.server.akkahttp._ | |
import akka.http.scaladsl.server.Route | |
val addBookRoute: Route = addBook.toRoute(addBookLogic _) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val findBook: Endpoint[String, Unit, Option[Book], Nothing] = | |
endpoint | |
.get | |
.in("book" / "find" / path[String]) | |
.out(jsonBody[Option[Book]]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val addBook: Endpoint[(Book, String), Unit, Boolean, Nothing] = | |
endpoint | |
.post | |
.in("book" / "add") | |
.in(jsonBody[Book]) | |
.in(header[String]("X-Auth-Token")) | |
.out(plainBody[Boolean]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tapir._ | |
val addBook: Endpoint[(Book, String), Unit, Boolean, Nothing] = | |
endpoint | |
.in(jsonBody[Book]) | |
.in(header[String]("X-Auth-Token")) | |
.out(plainBody[Boolean]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | |
<encoder> | |
<pattern>%d{HH:mm:ss.SSS} %highlight(%replace([%X{cid}] ){'\[\] ', ''})[%thread] %-5level %logger{5} - %msg%n</pattern> | |
</encoder> | |
<filter class="com.softwaremill.monix.SetCorrelationIdInMDCFilter" /> | |
</appender> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SetCorrelationIdInMDCFilter extends Filter[ILoggingEvent] { | |
override def decide(event: ILoggingEvent): FilterReply = { | |
CorrelationId().foreach(MDC.put("cid", _)) | |
FilterReply.NEUTRAL | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SetCorrelationIdBackend(delegate: SttpBackend[Task, Nothing]) | |
extends SttpBackend[Task, Nothing] { | |
override def send[T](request: sttp.Request[T, Nothing]): Task[Response[T]] = { | |
// suspending the calculation of the correlation id until the request | |
// send is evaluated | |
Task { | |
CorrelationId() match { | |
case Some(cid) => request.header(CorrelationIdHeader, cid) | |
case None => request |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Server1 extends App with StrictLogging { | |
val dsl = Http4sDsl[Task] | |
import dsl._ | |
val service = HttpRoutes.of[Task] { | |
case GET -> Root / "test1" => Ok() | |
} | |
BlazeServerBuilder[Task] | |
.bindHttp(8081) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object CorrelationId { | |
val CorrelationIdHeader = "X-Correlation-ID" | |
def setCorrelationIdMiddleware(service: HttpRoutes[Task]): HttpRoutes[Task] = | |
Kleisli { req: Request[Task] => | |
val cid = req.headers.get(CaseInsensitiveString(CorrelationIdHeader)) match { | |
case None => newCorrelationId() | |
case Some(cidHeader) => cidHeader.value | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object CorrelationId extends StrictLogging { | |
private val localCid = new Local[Option[String]](() => { | |
None | |
}) | |
def apply(): Option[String] = localCid() | |
} |