2020-05-16 10:10:23 +00:00
|
|
|
import javax.inject.{Inject, Provider, Singleton}
|
|
|
|
|
|
|
|
import com.example.user.UserDAO
|
|
|
|
import com.example.user.slick.SlickUserDAO
|
|
|
|
import com.google.inject.AbstractModule
|
|
|
|
import com.typesafe.config.Config
|
|
|
|
import play.api.inject.ApplicationLifecycle
|
|
|
|
import play.api.{Configuration, Environment}
|
|
|
|
import slick.jdbc.JdbcBackend.Database
|
|
|
|
|
|
|
|
import scala.concurrent.Future
|
|
|
|
import com.example.user.CarDAO
|
|
|
|
import com.example.Car.slick.SlickCarDAO
|
|
|
|
import com.example.services.LibraryService
|
|
|
|
import com.example.user.slick.services.SlickLibraryService
|
2020-08-22 10:48:11 +00:00
|
|
|
import com.example.services.TaskLibraryService
|
|
|
|
import com.example.user.slick.services.TaskSlickLibraryService
|
|
|
|
import monix.execution.Scheduler
|
|
|
|
import util.{Schedulers, SchedulersImpl}
|
|
|
|
import slick.jdbc.JdbcProfile
|
2020-05-16 10:10:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This module handles the bindings for the API to the Slick implementation.
|
|
|
|
*
|
|
|
|
* https://www.playframework.com/documentation/latest/ScalaDependencyInjection#Programmatic-bindings
|
|
|
|
*/
|
|
|
|
class Module(environment: Environment, configuration: Configuration)
|
|
|
|
extends AbstractModule {
|
|
|
|
override def configure(): Unit = {
|
|
|
|
bind(classOf[Database]).toProvider(classOf[DatabaseProvider])
|
2020-08-22 10:48:11 +00:00
|
|
|
bind(classOf[JdbcProfile]).toProvider(classOf[JdbcProfileProvider])
|
2020-05-16 10:10:23 +00:00
|
|
|
bind(classOf[UserDAO]).to(classOf[SlickUserDAO])
|
|
|
|
bind(classOf[CarDAO]).to(classOf[SlickCarDAO])
|
|
|
|
bind(classOf[LibraryService]).to(classOf[SlickLibraryService])
|
2020-08-22 10:48:11 +00:00
|
|
|
bind(classOf[TaskLibraryService]).to(classOf[TaskSlickLibraryService])
|
|
|
|
bind(classOf[Scheduler]).toProvider(classOf[SchedulerProvider])
|
|
|
|
bind(classOf[Schedulers]).toProvider(classOf[AppSchedulersProvider])
|
2020-05-16 10:10:23 +00:00
|
|
|
bind(classOf[DBCloseHook]).asEagerSingleton()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Singleton
|
|
|
|
class DatabaseProvider @Inject() (config: Config) extends Provider[Database] {
|
|
|
|
lazy val get = Database.forConfig("myapp.database", config)
|
|
|
|
}
|
|
|
|
|
2020-08-22 10:48:11 +00:00
|
|
|
@Singleton
|
|
|
|
class SchedulerProvider() extends Provider[Scheduler] {
|
|
|
|
lazy val get = Scheduler.io()
|
|
|
|
}
|
|
|
|
|
|
|
|
@Singleton
|
|
|
|
class AppSchedulersProvider() extends Provider[Schedulers] {
|
|
|
|
lazy val get =
|
|
|
|
new SchedulersImpl(
|
|
|
|
dbScheduler = Scheduler.io(),
|
|
|
|
cpuScheduler = Scheduler.global
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Singleton
|
|
|
|
class JdbcProfileProvider() extends Provider[JdbcProfile] {
|
|
|
|
lazy val get: JdbcProfile = _root_.slick.jdbc.H2Profile
|
|
|
|
}
|
|
|
|
|
|
|
|
@Singleton
|
2020-05-16 10:10:23 +00:00
|
|
|
class DBCloseHook @Inject() (db: Database, lifecycle: ApplicationLifecycle) {
|
2020-08-22 10:48:11 +00:00
|
|
|
|
2020-05-16 10:10:23 +00:00
|
|
|
lifecycle.addStopHook { () => Future.successful(db.close()) }
|
|
|
|
}
|