Skip to content

Commit

Permalink
Wrapping midleware instead of tap action.
Browse files Browse the repository at this point in the history
  • Loading branch information
Caparow committed Dec 15, 2023
1 parent ec71103 commit b674b2a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ class TestServices[F[+_, +_]: IO2](
rejectedNames: Set[String]
): IRTServerMiddleware[F, C] = new IRTServerMiddleware[F, C] {
override def priority: Int = 0
override def prepare(methodId: IRTMethodId)(context: C, parsedBody: Json): F[Throwable, Unit] = {
F.when(rejectedNames.contains(context.user)) {
F.fail(new IRTUnathorizedRequestContextException(s"Rejected for users: $rejectedNames."))
}
override def apply(methodId: IRTMethodId)(context: C, parsedBody: Json)(next: => F[Throwable, Json]): F[Throwable, Json] = {
F.ifThenElse(rejectedNames.contains(context.user))(
F.fail(new IRTUnathorizedRequestContextException(s"Rejected for users: $rejectedNames.")),
next,
)
}
}
final val wsStorage: WsSessionsStorage[F, AuthContext] = new WsSessionsStorageImpl[F, AuthContext](logger)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ trait IRTServerMethod[F[+_, +_], C] {
def methodId: IRTMethodId
def invoke(context: C, parsedBody: Json): F[Throwable, Json]

/** Contramap eval on context C2 -> C. If context is missing IRTUnathorizedRequestContextException will raise. */
final def contramap[C2](updateContext: (C2, Json) => F[Throwable, Option[C]])(implicit E: Error2[F]): IRTServerMethod[F, C2] = new IRTServerMethod[F, C2] {
override def methodId: IRTMethodId = self.methodId
override def invoke(context: C2, parsedBody: Json): F[Throwable, Json] = {
Expand All @@ -16,6 +17,14 @@ trait IRTServerMethod[F[+_, +_], C] {
.flatMap(self.invoke(_, parsedBody))
}
}

/** Wrap invocation with function '(Context, Body)(Method.Invoke) => Result' . */
final def wrap(middleware: (C, Json) => F[Throwable, Json] => F[Throwable, Json]): IRTServerMethod[F, C] = new IRTServerMethod[F, C] {
override def methodId: IRTMethodId = self.methodId
override def invoke(context: C, parsedBody: Json): F[Throwable, Json] = {
middleware(context, parsedBody)(self.invoke(context, parsedBody))
}
}
}

object IRTServerMethod {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@ import io.circe.Json

trait IRTServerMiddleware[F[_, _], C] {
def priority: Int
def prepare(methodId: IRTMethodId)(context: C, parsedBody: Json): F[Throwable, Unit]
def apply(
methodId: IRTMethodId
)(context: C,
parsedBody: Json,
)(next: => F[Throwable, Json]
): F[Throwable, Json]
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ trait IRTServerMultiplexor[F[+_, +_], C] {
.flatMap(_.invoke(context, parsedBody))
}

/** Contramap eval on context C2 -> C. If context is missing IRTUnathorizedRequestContextException will raise. */
final def contramap[C2](
updateContext: (C2, Json) => F[Throwable, Option[C]]
)(implicit io2: IO2[F]
): IRTServerMultiplexor[F, C2] = {
val mappedMethods = self.methods.map { case (k, v) => k -> v.contramap(updateContext) }
new IRTServerMultiplexor.FromMethods(mappedMethods)
}

final def wrap(middleware: IRTServerMiddleware[F, C])(implicit io2: IO2[F]): IRTServerMultiplexor[F, C] = {
/** Wrap invocation with function '(Context, Body)(Method.Invoke) => Result' . */
final def wrap(middleware: IRTServerMiddleware[F, C]): IRTServerMultiplexor[F, C] = {
val wrappedMethods = self.methods.map {
case (methodId, method) =>
val wrappedMethod: IRTServerMethod[F, C] = method.contramap[C] {
val wrappedMethod: IRTServerMethod[F, C] = method.wrap {
case (ctx, body) =>
middleware.prepare(methodId)(ctx, body).as(Some(ctx))
next => middleware(method.methodId)(ctx, body)(next)
}
methodId -> wrappedMethod
}
Expand Down

0 comments on commit b674b2a

Please sign in to comment.