Skip to content

Commit

Permalink
fix: Annotate Headers & Update zio-http to version 3.0.0-RC2 (#106)
Browse files Browse the repository at this point in the history
* fix: Annotate Headers & Update zio-http to version 3.0.0-RC2
* fix: cleanup code - remove websocket logic
* fix: get Header.ContentType not working
  • Loading branch information
FabioPinheiro authored and mineme0110 committed Apr 30, 2024
1 parent 8f80a3d commit 441a7f0
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 303 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ lazy val V = new {
val zio = "2.0.15"
val zioJson = "0.4.2"
// val zioMunitTest = "0.1.1"
val zioHttp = "0.0.5"
val zioHttp = "3.0.0-RC2"
val zioConfig = "4.0.0-RC16"
val zioLogging = "2.1.14"
val zioSl4j = "2.1.14"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import fmgp.did.comm.*
import io.iohk.atala.mediator.comm.*
import io.iohk.atala.mediator.utils.MyHeaders
import zio.*
import zio.http.*
import zio.http.model.*
import zio.http.{MediaType => ZMediaType, *}
import zio.json.*

import scala.util.chaining._

object MessageDispatcherJVM {
val layer: ZLayer[Client, Throwable, MessageDispatcher] =
ZLayer.fromZIO(
Expand All @@ -26,14 +27,19 @@ class MessageDispatcherJVM(client: Client) extends MessageDispatcher {
destination: String,
xForwardedHost: Option[String],
): ZIO[Any, DispatcherError, String] = {
val contentTypeHeader = Headers.contentType(msg.`protected`.obj.typ.getOrElse(MediaTypes.ENCRYPTED).typ)
val xForwardedHostHeader = Headers(xForwardedHost.map(x => Header(MyHeaders.xForwardedHost, x)))
val contentTypeHeader = msg.`protected`.obj.typ
.getOrElse(MediaTypes.ENCRYPTED)
// .pipe(e => Header.ContentType(ZMediaType(e.mainType, e.subType))) FIXME
.pipe(e => Header.ContentType(ZMediaType.application.any.copy(subType = "didcomm-encrypted+json")))
val xForwardedHostHeader = xForwardedHost.map(x => Header.Custom(customName = MyHeaders.xForwardedHost, x))

// xForwardedHost.map(x => Header.(MyHeaders.xForwardedHost, x))
for {
res <- Client
.request(
url = destination,
method = Method.POST,
headers = contentTypeHeader ++ xForwardedHostHeader,
headers = Headers(Seq(Some(contentTypeHeader), xForwardedHostHeader).flatten),
content = Body.fromCharSequence(msg.toJson),
)
.tapError(ex => ZIO.logWarning(s"Fail when calling '$destination': ${ex.toString}"))
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,51 @@ package io.iohk.atala.mediator.utils

import zio.*
import zio.http.*
import java.util.concurrent.TimeUnit

extension [R, Err](app: HttpApp[R, Err])
def annotateLogs: HttpApp[R, Err] =
app @@ new HttpAppMiddleware.Simple[Any, Nothing] { self1 =>
override def apply[R, Err](
http: Http[R, Err, Request, Response],
)(implicit trace: Trace): Http[R, Err, Request, Response] =
http.mapZIO { request =>
val headers = request.headers.toSeq.map(e => (e.key.toString.toLowerCase, e.value)).toMap
inline def composeAnnotate(
inline headerName: String,
inline logName: String,
inline run: ZIO[R, Err, Response]
) = headers.get(headerName) match
case Some(value) => ZIO.logAnnotate(logName, value.toString) { run }
case None => run

composeAnnotate(
"fly-client-ip",
"client-ip",
composeAnnotate(
"X-Request-ID",
"fly-request-id",
composeAnnotate(
"user-agent",
"user-agent",
composeAnnotate(
"host",
"host",
ZIO.succeed(request)
)
)
)
object MiddlewareUtils {

final def serverTime: RequestHandlerMiddleware[Nothing, Any, Nothing, Any] = HttpAppMiddleware.patchZIO(_ =>
for {
currentMilliseconds <- Clock.currentTime(TimeUnit.MILLISECONDS)
withHeader = Response.Patch.addHeader("X-Time", currentMilliseconds.toString)
} yield withHeader,
)

final def annotateHeaders: RequestHandlerMiddleware[Nothing, Any, Nothing, Any] =
new RequestHandlerMiddleware.Simple[Any, Nothing] {
override def apply[R1 <: Any, Err1 >: Nothing](
handler: Handler[R1, Err1, Request, Response],
)(implicit trace: Trace): Handler[R1, Err1, Request, Response] =
Handler.fromFunctionZIO { (request: Request) =>

def annotations = request.headers.toSet.flatMap(h =>
h.headerName.toLowerCase() match {
case "fly-client-ip" => Some(LogAnnotation("client-ip", h.renderedValue))
case "fly-request-id" => Some(LogAnnotation("fly-request-id", h.renderedValue))
case "x-request-id" => Some(LogAnnotation("x-request-id", h.renderedValue))
case "user-agent" => Some(LogAnnotation("user-agent", h.renderedValue))
case "host" => Some(LogAnnotation("host", h.renderedValue))
case _ => None
}
)

val requestHandler = handler
.runZIO(request)
.sandbox
.exit
.timed
.tap {
case (duration, Exit.Success(response)) =>
ZIO.log(s"${response.status.code} ${request.method} ${request.url.encode} ${duration.toMillis}ms")
case (duration, Exit.Failure(cause)) =>
ZIO.log(s"Failed ${request.method} ${request.url.encode} ${duration.toMillis}ms: " + cause.prettyPrint)
}
.flatMap(_._2)
.unsandbox

ZIO.logAnnotate(annotations)(requestHandler)
}
}

}

This file was deleted.

0 comments on commit 441a7f0

Please sign in to comment.