Scala-Play-Slick-Demo/app/util/Util.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

59 lines
1.3 KiB
Scala

package util
import scala.concurrent.Future
import cats.effect.IO
import cats.implicits._
import scala.util.Success
import scala.util.Failure
import cats.effect.Effect
import play.api.mvc._
import scala.concurrent.ExecutionContext
object IOHttp {
/**
* Actions to convert an effect wrapper F such as cats IO
* or monix Task into a future implicitly
*
* @param ab
*/
implicit class ActionBuilderOps[+R[_], B](ab: ActionBuilder[R, B]) {
import cats.effect.implicits._
/**
* Action to convert an effect wrapper F such as cats IO
* or monix Task into a future implicitly
*
* @param ab
*/
def asyncFR[F[_]: Effect](cb: R[B] => F[Result]): Action[B] = ab.async {
c => cb(c).toIO.unsafeToFuture()
}
/**
* Action to convert an effect wrapper F such as cats IO
* or monix Task into a future implicitly
*
* @param ab
*/
def asyncF[F[_]: Effect](cb: => F[Result]): Action[AnyContent] =
ab.async { cb.toIO.unsafeToFuture() }
}
}
object RepoUtil {
def fromFuture[IOEffect[A], A](
f: => Future[A]
)(implicit ec: ExecutionContext): IO[A] =
IO.delay(f) >>= (f =>
IO.async[A] { cb =>
f.onComplete {
case Success(a) => cb(Right(a))
case Failure(ex) => cb(Left(ex))
}
}
)
}