Created
January 14, 2019 21:03
-
-
Save megri/526aa3ce13f9fa1e118627a4de711ef4 to your computer and use it in GitHub Desktop.
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
import cats.effect._ | |
import cats.implicits._ | |
import com.twitter.finagle.{Http, ListeningServer} | |
import doobie._ | |
import doobie.hikari._ | |
import io.catbird.util._ | |
import io.catbird.util.effect._ | |
object App extends IOApp { | |
val transactor: Resource[IO, HikariTransactor[IO]] = { | |
val postgresUser = sys.env("POSTGRES_USER") | |
val postgresPassword = sys.env("POSTGRES_PASSWORD") | |
val postgresDatabase = sys.env("POSTGRES_DB") | |
val postgresHost = sys.env("POSTGRES_HOST") | |
for { | |
ce <- ExecutionContexts.fixedThreadPool[IO](8) // connect EC | |
te <- ExecutionContexts.cachedThreadPool[IO] // transaction EC | |
xa <- HikariTransactor.newHikariTransactor[IO]( | |
"org.postgresql.Driver", | |
s"jdbc:postgresql://$postgresHost/$postgresDatabase", | |
postgresUser, | |
postgresPassword, | |
ce, | |
te | |
) | |
} yield xa | |
} | |
def server(transactor: Transactor[IO]): Resource[IO, ListeningServer] = { | |
val dataAccess = new DataAccess(transactor) | |
val routes = new Routes(dataAccess) | |
val startServer = IO(Http.server.serve(":8080", routes.service)) | |
Resource.make(startServer)(server => rerunnableToIO(Rerunnable.fromFuture(server.close()))) | |
} | |
override def run(args: List[String]): IO[ExitCode] = { | |
val serverBootstrap = for { | |
xa <- transactor | |
s <- server(xa) | |
} yield s | |
serverBootstrap.use(_ => IO.never).as(ExitCode.Success) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment