Skip to content

mtumilowicz/scala-akka-http-workshop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status License: GPL v3

scala-akka-http-workshop

preface

  • goals of this workshop
    • introduction to akka http
      • routing and marshalling
  • workshop plan:
    • playground with roles: developer, product owner
    • develop microservice with features
      • user could buy a venue if he can afford it
      • userA could buy a venue from userB if he can afford it

akka http

  • implements a full server/client-side HTTP stack on top of akka-actor and akka-stream
  • provides a DSL to describe HTTP "routes" and how they should be handled
    • Route is the central concept of Akka HTTP’s Routing DSL
      type Route = RequestContext => Future[RouteResult]
      
      • when a route receives a request (RequestContext) it can do one of these things
        1. requestContext.complete(...) - given response is sent to the client as reaction to the request
        2. requestContext.reject(...) - route does not want to handle the request
        3. requestContext.fail(...) -
      • RequestContext
        • wraps an HttpRequest instance to enrich it with additional information that are typically required by the routing logic (ex. ExecutionContext)
      • RouteResult
        • simple abstract data type (ADT) that models the possible non-error results of a Route
        • Complete, Rejected
    • Directive
      • a small building block used for creating route structures
        val route: Route = { ctx => ctx.complete("yeah") } // standard way to build route
        val route: Route = _.complete("yeah") // scala syntax
        val route = complete("yeah") // complete directive
        
      • example
        val route: Route =
        pathPrefix("venues") {
            concat(
                pathEnd {
                    get {
                        complete(handler.action)
                    }
                },
              ...
            )
            }
        
    • PathMatcher
      • mini-DSL is used to match incoming URL’s and extract values from them
      • used in the path directive
      • example
        path("foo" / "bar") // matches /foo/bar
        
  • marshalling and unmarshalling
    • marshalling - converting a higher-level (object) into lower-level representation, ex. a "wire format"
      • also called "serialization" or "pickling"
    • "unmarshalling" - reverse process to marshalling
      • also called "deserialization" or "unpickling"
    • is done separately from the route declarations
  • timeouts
    • idle-timeout - if a connection is open but no request/response is being written to it for over idle-timeout time, the connection will be automatically closed.
    • request timeouts - limits the maximum time it may take to produce an HttpResponse from a route
      • if that deadline is not met the server will automatically inject a Service Unavailable HTTP response and close the connection to prevent it from leaking and staying around indefinitely

Releases

No releases published

Packages

No packages published

Languages