Scala-Play-Slick-Demo/app/Module.scala
Rohan Sircar cb9524eac7 Many changes
Rewrote slick db service using monix task instead of future
Added cats IO to controller to make use of above db service
Added action methods that support effect wrappers other than future
Added play-flyway to auto-manage migrations
Moved migration files to appropriate directory as a result of above
Added cors filter to work with separately hosted frontend
Made logger log at debug
2020-08-22 16:18:11 +05:30

71 lines
2.3 KiB
Scala

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()) }
}