Created
February 4, 2017 12:11
-
-
Save dakatsuka/5cb0561c9be01b7d4a9be660ec95d99d to your computer and use it in GitHub Desktop.
Akka HTTPを便利に使うtrait
This file contains 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 akka.http.scaladsl.server.{ Directive, Directive0, Directive1, Route } | |
import akka.http.scaladsl.server.Directives._ | |
import akka.http.scaladsl.server.directives.OnSuccessMagnet | |
import akka.http.scaladsl.server.util.Tupler | |
import akka.http.scaladsl.util.FastFuture._ | |
import de.heikoseeberger.akkahttpcirce.CirceSupport | |
import io.circe.export.Exported | |
import io.circe.generic.decoding.DerivedDecoder | |
import io.circe.generic.encoding.DerivedObjectEncoder | |
import io.circe.{ Decoder, ObjectEncoder } | |
import io.circe.generic.util.macros.ExportMacros | |
import scala.concurrent.Future | |
import scala.language.experimental.macros | |
trait Handler extends CirceSupport { | |
implicit def exportDecoder[A]: Exported[Decoder[A]] = macro ExportMacros.exportDecoder[DerivedDecoder, A] | |
implicit def exportEncoder[A]: Exported[ObjectEncoder[A]] = macro ExportMacros.exportEncoder[DerivedObjectEncoder, A] | |
implicit class Directive0ForComprehensionSupport(directive0: Directive0) { | |
def map[T](f: Unit => T): Directive1[T] = directive0.tmap(f) | |
def flatMap[T](f: Unit => Directive1[T]): Directive1[T] = directive0.tflatMap(f) | |
} | |
implicit class Directive1Operators[T](directive1: Directive1[T]) { | |
def <<&(f: T => Directive0): Directive1[T] = directive1.flatMap(t => f(t).tmap(_ => t)) | |
} | |
implicit def directive0ToDirective1(directive0: Directive0): Directive1[Unit] = | |
directive0.tflatMap(provide) | |
implicit def directiveIncludingFuture[T](futureDirective: Directive[Tuple1[Future[T]]])(implicit tupler: Tupler[T]): OnSuccessMagnet { type Out = tupler.Out } = { | |
new OnSuccessMagnet { | |
type Out = tupler.Out | |
val directive: Directive[Out] = futureDirective.flatMap { future => | |
Directive[tupler.Out] { inner => ctx => | |
import ctx.executionContext | |
future.fast.flatMap(t => inner(tupler(t))(ctx)) | |
}(tupler.OutIsTuple) | |
}(tupler.OutIsTuple) | |
} | |
} | |
def routes: Route | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment