Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial module for request-scoped objects
The work is mostly based in the code from this pull request on play-framework project [1]. [1] - playframework/playframework#6798
- Loading branch information
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name := "play-request-tracer" | ||
|
||
organization := "com.alexitc" | ||
|
||
scalaVersion := "2.12.2" | ||
|
||
val playVersion = "2.6.3" | ||
|
||
libraryDependencies += "com.typesafe.play" % "play_2.12" % playVersion | ||
libraryDependencies += "com.google.inject" % "guice" % "4.1.0" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sbt.version=0.13.16 |
89 changes: 89 additions & 0 deletions
89
src/main/scala/com/alexitc/play/api/inject/guice/RequestScopedRouter.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.alexitc.play.api.inject.guice | ||
|
||
import com.google.inject.{AbstractModule, Injector, Module} | ||
import play.api.mvc.RequestHeader | ||
import play.api.routing.Router | ||
|
||
import scala.reflect.ClassTag | ||
|
||
/** | ||
* A router that creates a new instance of a router from the injector on every request, also the request-scoped | ||
* dependencies defined in the provided modules. | ||
* | ||
* To use, extend this class: | ||
* | ||
* {{{ | ||
* class MyRequestScopedRouter @Inject()(injector: Injector) | ||
* extends RequestScopedRouter[router.Routes](injector, modules = Seq(new MyRequestModule)) | ||
* }}} | ||
* | ||
* Then add the router to your configuration, e.g. "play.http.router = com.example.MyRequestScopedRouter" | ||
*/ | ||
class RequestScopedRouter[T <: Router]( | ||
injector: Injector, | ||
prefix: String = "/", | ||
modules: Seq[_ >: AbstractModule] = Seq.empty)( | ||
implicit routerClassTag: ClassTag[T]) | ||
extends Router | ||
with RequestScopedRouterProvider { | ||
|
||
def this(injector: Injector, routerClass: Class[T], prefix: String, modules: Array[Module]) { | ||
this(injector, prefix, modules)(ClassTag(routerClass)) | ||
} | ||
|
||
/** | ||
* Provide a router that is scoped to this request. | ||
*/ | ||
def routerForRequest(request: RequestHeader): Router = { | ||
val injectorWithRequest = injector | ||
.createChildInjector(new RequestScopedModule(request)) | ||
|
||
val requestScopedModules = modules.map { clazz => injectorWithRequest.getInstance(clazz.asInstanceOf[Class[AbstractModule]]) } | ||
|
||
val childInjector = injectorWithRequest | ||
.createChildInjector(requestScopedModules: _*) | ||
|
||
childInjector | ||
.getInstance(routerClassTag.runtimeClass) | ||
.asInstanceOf[T] | ||
.withPrefix(prefix) | ||
} | ||
|
||
def routes: Router.Routes = Function.unlift { request => | ||
val requestScopedRouter = routerForRequest(request) | ||
requestScopedRouter.routes.lift(request) | ||
} | ||
|
||
def withPrefix(prefix: String): RequestScopedRouter[T] = new RequestScopedRouter(injector, prefix, modules) | ||
|
||
/** | ||
* Since we don't have a request yet, we can't get the documentation, so this just returns an empty Seq. To get the | ||
* route documentation, call routerForRequest(request).documentation | ||
* | ||
* @return A list of method, path pattern and controller/method invocations for each route. | ||
*/ | ||
def documentation: Seq[(String, String, String)] = Seq.empty | ||
} | ||
|
||
/** | ||
* A provider that returns a router scoped to a specific request. Useful when you want to access the router's | ||
* documentation (which is not available from RequestScopedRouter). | ||
*/ | ||
trait RequestScopedRouterProvider { | ||
|
||
/** | ||
* Provide a router that is scoped to this request. | ||
*/ | ||
def routerForRequest(request: RequestHeader): Router | ||
} | ||
|
||
class RequestScopedModule(request: RequestHeader) extends AbstractModule { | ||
|
||
override def configure(): Unit = { | ||
bind(classOf[RequestHeader]) | ||
.toInstance(request) | ||
|
||
bind(classOf[play.mvc.Http.RequestHeader]) | ||
.toInstance(new play.core.j.RequestHeaderImpl(request)) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
version in ThisBuild := "0.1.0" | ||
|