All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sbuslab.http.directives.SbusDirectives.scala Maven / Gradle / Ivy

There is a newer version: 1.2.26
Show newest version
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
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy