Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Mastering the Spray Routing DSL

Age Mooij
December 08, 2014

Mastering the Spray Routing DSL

Slides from a talk I did at the Scala Exchange 2014 conference. in London.

Earlier versions of the same talk were given at several meetups and at the Scala.IO 2014 conference in Paris.

Age Mooij

December 08, 2014
Tweet

More Decks by Age Mooij

Other Decks in Programming

Transcript

  1. import exchange.scala._ Preso( title = "Beyond URI Matching", sub =

    "Mastering the Spray Routing DSL", by = Dude("Age Mooij", "@agemooij"), at = ScalaExchange, in = London, on = December.8th.2014 )
  2. val theNext45Minutes = List( Part("Quick Spray Routing intro"), Part("Lots of

    DSL examples & tips") ) val code = Link( "https://github.com/agemooij/preso-scala.exchange-2014") val furtherReading = Link( "http://spray.io/documentation/1.2.2/spray-routing/")
  3. Example(Route) val thingsApiRoutes: Route = { pathPrefix("things") { pathEnd {

    get { complete(OK, List(Thing("Apple"), Thing("Orange"))) } ~ post { entity(as[Thing]) { thing 㱺 complete(Created) } } } ~ path(LongNumber) { id 㱺 get { complete(OK, Thing(s"Thing # $id")) } } } }
  4. type Route = RequestContext 㱺 Unit RealModel(Route) case class RequestContext(

    request: HttpRequest, responder: ActorRef, unmatchedPath: Uri.Path ) But("Use the mental model!")
  5. val route: Route = complete("W00t!") val route: Route = _.complete("W00t!")

    val route: Route = { ctx: RequestContext 㱺 ctx.complete("W00t!") } TheSimplest(Route)
  6. val routeOperations = List( RouteFiltering("Pass or reject"), RouteChaining("If rejected, try

    this one"), RouteTransformation( "Modify the request or the response") ) Composing(Routes)
  7. val chained: Route = get { complete("Get") } ~ delete

    { complete("Delete") } ~ (post | put) { complete("Post or Put") } FurtherReading( "http://spray.io/documentation/1.2.2/spray-routing/key-concepts/routes/") ChainingAndFiltering(Routes) val chainingOperator = '~'
  8. val directives = BuildingBlocksFor( "Constructing arbitrarily complex routes" ) val

    whatTheyDo = List( Filter("Pass or reject"), Extract( "Make request values available as extractions"), Transform( "Modify the request or the response"), Complete("Produce a response") ) IntroTo(Directives)
  9. ExamplesOf(DirectivesThat(Filter || Extract || Complete))) pathPrefix("things") { pathEnd { get

    { complete(OK, List(Thing("Apple"), Thing("iPhone"))) } ~ post { entity(as[Thing]) { thing 㱺 complete(Created) } } } ~ path(LongNumber) { id 㱺 get { complete(OK, Thing(s"Thing # $id")) } } }
  10. ExamplesOf(DirectivesThat(Transform(Requests || Responses))) (decompressRequest() & compressResponseIfRequested(())) { pathPrefix("api") { respondWithHeaders(`Cache-Control`(`no-store`))

    complete(OK, "Not a very useful API...") } } } FurtherReading( "http://spray.io/documentation/1.2.2/spray-routing/key-concepts/directives/")
  11. val route: Route = (get | put | post) {

    complete("W00t!") } val getPutOrPost = get | put | post val route: Route = getPutOrPost { complete("W00t!") } TheEasyWayToBuild(CustomDirectives)
  12. val supportedJsonLibs = List( PlayJsonSupport, Json4sSupport, LiftJsonSupport ) val alsoSupported

    = List( PlayTwirlSupport, InsertYourCustomMarshallerHere ) MarshallingWith(...)