Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Added AppLoader and CassandraRepositoryComponents
- Loading branch information
1 parent
cb7a9b4
commit 4e707cb
Showing
3 changed files
with
57 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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 | |||
) | |||
} |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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) | |||
} | |||
} |
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