com.sbuslab.http.directives.SbusDirectives.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of akka-http-tools Show documentation
Show all versions of akka-http-tools Show documentation
Tools for akka-http (marshaling, error handling, websockets, sbus support)
package com.sbuslab.http.directives
import scala.concurrent.{ExecutionContext, Future}
import akka.http.scaladsl.server.{Directive1, Directives}
import com.sbuslab.http.Headers
import com.sbuslab.sbus.Context
trait SbusDirectives extends Directives {
def sbusContext: Directive1[Context] = {
(extractClientIP & optionalHeaderValueByName("User-Agent")).tflatMap { case (ip, userAgent) ⇒
val sbusCtx = Context(Map(
"ip" → ip.value,
"userAgent" → userAgent.orNull
))
optionalHeaderValueByName(Headers.CorrelationId).flatMap {
case Some(corrId) ⇒ provide(sbusCtx.withCorrelationId(corrId))
case _ ⇒ provide(sbusCtx)
}
}
}
@scala.annotation.tailrec
final def wrap(field: String*)(f: Future[_])(implicit ec: ExecutionContext): Future[_] =
if (field.nonEmpty) wrap(field.init: _*)(f.map(res ⇒ Map(field.last → res)))
else f
}