Permalink
Browse files

Added AppLoader and CassandraRepositoryComponents

  • Loading branch information...
1 parent cb7a9b4 commit 4e707cb784e7adbc7c8204f033404690986c9448 @manuelkiessling committed Jan 18, 2016
Showing with 57 additions and 0 deletions.
  1. +22 −0 app/AppLoader.scala
  2. +33 −0 app/components/CassandraRepositoryComponents.scala
  3. +2 −0 conf/application.conf
View
@@ -0,0 +1,22 @@
+import components.CassandraRepositoryComponents
+import play.api.ApplicationLoader.Context
+import play.api.routing.Router
+import play.api.{Application, ApplicationLoader, BuiltInComponentsFromContext}
+import router.Routes
+
+class AppLoader extends ApplicationLoader {
+ override def load(context: ApplicationLoader.Context): Application =
+ new AppComponents(context).application
+}
+
+class AppComponents(context: Context) extends BuiltInComponentsFromContext(context) with CassandraRepositoryComponents {
+
+ lazy val applicationController = new controllers.Application(productsRepository)
+ lazy val assets = new controllers.Assets(httpErrorHandler)
+
+ override def router: Router = new Routes(
+ httpErrorHandler,
+ applicationController,
+ assets
+ )
+}
@@ -0,0 +1,33 @@
+package components
+
+import cassandra.{CassandraConnector, CassandraConnectionUri}
+import com.datastax.driver.core.Session
+import models.ProductModel
+import play.api.inject.ApplicationLifecycle
+import play.api.{Configuration, Environment, Mode}
+import repositories.{Repository, ProductsRepository}
+import scala.concurrent.Future
+
+trait CassandraRepositoryComponents {
+ // These will be filled by Play's built-in components; should be `def` to avoid initialization problems
+ def environment: Environment
+ def configuration: Configuration
+ def applicationLifecycle: ApplicationLifecycle
+
+ lazy private val cassandraSession: Session = {
+ val uriString = environment.mode match {
+ case Mode.Prod => "cassandra://localhost:9042/prod"
+ case _ => "cassandra://localhost:9042/test"
+ }
+ val session: Session = CassandraConnector.createSessionAndInitKeyspace(
+ CassandraConnectionUri(uriString)
+ )
+ // Shutdown the client when the app is stopped or reloaded
+ applicationLifecycle.addStopHook(() => Future.successful(session.close()))
+ session
+ }
+
+ lazy val productsRepository: Repository[ProductModel, Int] = {
+ new ProductsRepository(cassandraSession)
+ }
+}
@@ -1,6 +1,8 @@
# This is the main configuration file for the application. # This is the main configuration file for the application.
# ~~~~~ # ~~~~~
+play.application.loader="AppLoader"
+
# Secret key # Secret key
# ~~~~~ # ~~~~~
# The secret key is used to secure cryptographics functions. # The secret key is used to secure cryptographics functions.

0 comments on commit 4e707cb

Please sign in to comment.