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 import com.example.services.TaskLibraryService import com.example.user.slick.services.TaskSlickLibraryService import monix.execution.Scheduler import util.{Schedulers, SchedulersImpl} import slick.jdbc.JdbcProfile /** * 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]) bind(classOf[JdbcProfile]).toProvider(classOf[JdbcProfileProvider]) bind(classOf[UserDAO]).to(classOf[SlickUserDAO]) bind(classOf[CarDAO]).to(classOf[SlickCarDAO]) bind(classOf[LibraryService]).to(classOf[SlickLibraryService]) bind(classOf[TaskLibraryService]).to(classOf[TaskSlickLibraryService]) bind(classOf[Scheduler]).toProvider(classOf[SchedulerProvider]) bind(classOf[Schedulers]).toProvider(classOf[AppSchedulersProvider]) bind(classOf[DBCloseHook]).asEagerSingleton() } } @Singleton class DatabaseProvider @Inject() (config: Config) extends Provider[Database] { lazy val get = Database.forConfig("myapp.database", config) } @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 class DBCloseHook @Inject() (db: Database, lifecycle: ApplicationLifecycle) { lifecycle.addStopHook { () => Future.successful(db.close()) } }