Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added AppLoader and CassandraRepositoryComponents
  • Loading branch information
manuelkiessling committed Jan 18, 2016
1 parent cb7a9b4 commit 4e707cb
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
22 changes: 22 additions & 0 deletions app/AppLoader.scala
@@ -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
)
}
33 changes: 33 additions & 0 deletions app/components/CassandraRepositoryComponents.scala
@@ -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)
}
}
2 changes: 2 additions & 0 deletions conf/application.conf
@@ -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.
Expand Down

0 comments on commit 4e707cb

Please sign in to comment.