Skip to content

Instantly share code, notes, and snippets.

@richdougherty
Created November 7, 2017 19:12
Show Gist options
  • Save richdougherty/35694e699777019728f9a07b36f17fd5 to your computer and use it in GitHub Desktop.
Save richdougherty/35694e699777019728f9a07b36f17fd5 to your computer and use it in GitHub Desktop.
Plan for refactoring WebCommands
// Change DefaultHttpRequestHandler to take a pipeline
class DefaultHttpRequestHandler(
pipeline: Seq[Handler],
router: Router,
errorHandler: HttpErrorHandler,
configuration: HttpConfiguration,
filters: Seq[EssentialFilter]) extends HttpRequestHandler {
...
}
// Introduce a new type of Handler so it can be used in a pipeline
trait Handler
// Just a strawman interface - will definitely not look like this!
trait HandlerFunction {
def handle(rh: RequestHeader): (RequesthHeader, Handler)
}
// The following classes make up the default pipeline
// Move this out of Server class - this is what adds cookies, request ids, etc to
// all requests
class AddRequestAttributesHandler(requestFactory: RequestFactory) extends HandlerFunction {
def handle(rh: RequestHeader, next: Handler): (RequestHeader, Handler) = {
(requestFactory.copyRequestHeader(rh), next)
}
}
// Move this out of our *ModelConversion classes
class ForwardedHeaderHandler(configuration: ForwardedHeaderHandlerConfig) extends HandlerFunction {
def handle(rh: RequestHeader, next: Handler): (RequestHeader, Handler) = {
val newConnection: RemoteConnection = forwardedConnect(rh.connection, rh.headers)
(rh.withConnection(newConnection), next)
}
}
// We can use this for backwards-compatibility with WebCommands
// but then deprecate WebCommands - Evolutions can just make its
// own Handler and run in the handler pipeline
class WebCommandsHandler(webCommands: WebCommands, optDevContext: Option[DevContext]) extends HandlerFunction {
def handle(rh: RequestHeader, next: Handler): (RequestHeader, Handler) = {
val optResult: Option[Result] = optDevContext.flatMap { devContext =>
webCommands.handleWebCommand(request: RequestHeader, buildLink: BuildLink, path: File): Option[Result]
}
optResult.fold((rh, next)) { (rh, result => Action { result }) }
}
}
// Fun question - could Router be run inside a Handler too? :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment