DB connection and transactor as separate class with config and IO
This commit is contained in:
@@ -0,0 +1,3 @@
|
|||||||
|
username = "postgres"
|
||||||
|
password = "mysecretpassword"
|
||||||
|
url = "jdbc:postgresql://localhost:5432/weather-tool"
|
||||||
@@ -2,25 +2,16 @@ package app
|
|||||||
|
|
||||||
import cats.effect._
|
import cats.effect._
|
||||||
import cats.implicits.catsSyntaxTuple2Parallel
|
import cats.implicits.catsSyntaxTuple2Parallel
|
||||||
import db.Main.transactor
|
import db.{DBConnection, DataService, FileService, PostgresService}
|
||||||
import db.{DataService, FileService, PostgresService}
|
|
||||||
import doobie.Transactor
|
|
||||||
import fetch.{FetchService, FileFetchScheduler}
|
import fetch.{FetchService, FileFetchScheduler}
|
||||||
import server.Server
|
import server.Server
|
||||||
|
|
||||||
object Main extends IOApp {
|
object Main extends IOApp {
|
||||||
|
|
||||||
def transactor[F[_] : Async]: Transactor[F] = Transactor.fromDriverManager[F](
|
|
||||||
"org.postgresql.Driver",
|
|
||||||
"jdbc:postgresql://localhost:5432/weather-tool",
|
|
||||||
"postgres",
|
|
||||||
"mysecretpassword"
|
|
||||||
)
|
|
||||||
def run(args: List[String]): IO[ExitCode] = {
|
def run(args: List[String]): IO[ExitCode] = {
|
||||||
val xa = transactor[IO]
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
postgresService <- PostgresService.of(xa)
|
transactor <- DBConnection.transactor[IO]
|
||||||
|
postgresService <- PostgresService.of(transactor)
|
||||||
|
_ <- postgresService.createWeatherTable // create table if it does not exists
|
||||||
fileService <- FileService.of
|
fileService <- FileService.of
|
||||||
dataService <- DataService.of(fileService, postgresService)
|
dataService <- DataService.of(fileService, postgresService)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
import cats.effect.{Async, IO}
|
||||||
|
import doobie.Transactor
|
||||||
|
import pureconfig._
|
||||||
|
import pureconfig.generic.auto._
|
||||||
|
import pureconfig.error.ConfigReaderFailures
|
||||||
|
|
||||||
|
case class PostgresConfig(username: String, password: String, url: String)
|
||||||
|
|
||||||
|
object DBConnection {
|
||||||
|
private def loadPostgresConfig: Either[ConfigReaderFailures, PostgresConfig] = {
|
||||||
|
ConfigSource.resources("postgres.conf").load[PostgresConfig]
|
||||||
|
}
|
||||||
|
|
||||||
|
def transactor[F[_] : Async]: IO[Transactor[F]] = {
|
||||||
|
loadPostgresConfig match {
|
||||||
|
case Right(config) => IO(Transactor.fromDriverManager[F](
|
||||||
|
"org.postgresql.Driver",
|
||||||
|
config.url,
|
||||||
|
config.username,
|
||||||
|
config.password
|
||||||
|
))
|
||||||
|
case Left(errors) => IO.raiseError(new RuntimeException(s"Failed to load config: $errors"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -75,7 +75,7 @@ class PostgresService(transactor: Transactor[IO], log: Logger[IO]) extends DataS
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def createWeatherTable(): IO[Int] = {
|
def createWeatherTable: IO[Int] = {
|
||||||
for {
|
for {
|
||||||
createTableSql <- getResourceContent("/db/create_weather_table.sql")
|
createTableSql <- getResourceContent("/db/create_weather_table.sql")
|
||||||
result <- Update0(createTableSql, None).run.transact(transactor)
|
result <- Update0(createTableSql, None).run.transact(transactor)
|
||||||
|
|||||||
Reference in New Issue
Block a user