From f956d658d54c31a75d53d3638d63eb04b8846b5f Mon Sep 17 00:00:00 2001 From: Guntis Smaukstelis Date: Sun, 10 Dec 2023 18:47:56 +0200 Subject: [PATCH] Connect to local/fly database, insert new weather data --- src/main/scala/app/Main.scala | 13 ++++---- src/main/scala/db/DBConnection.scala | 46 +++++++++++++++++++++++++--- src/main/scala/db/DataService.scala | 12 ++++---- 3 files changed, 54 insertions(+), 17 deletions(-) diff --git a/src/main/scala/app/Main.scala b/src/main/scala/app/Main.scala index 74f2ef0..40b802b 100644 --- a/src/main/scala/app/Main.scala +++ b/src/main/scala/app/Main.scala @@ -8,15 +8,16 @@ import server.Server object Main extends IOApp { def run(args: List[String]): IO[ExitCode] = { - println(System.getenv("DATABASE_URL")) + // postgres://weather_tool:XID547uldXliJBa@weather-tool-db.flycast:5432/weather_tool?sslmode=disable +// println(System.getenv("DATABASE_URL")) for { -// transactor <- DBConnection.transactor[IO] -// postgresService <- PostgresService.of(transactor) -// _ <- postgresService.createWeatherTable // create table if it does not exists + transactor <- DBConnection.transactor[IO] + postgresService <- PostgresService.of(transactor) + _ <- postgresService.createWeatherTable // create table if it does not exists fileService <- FileService.of -// dataService <- DataService.of(fileService, postgresService) - dataService <- DataService.of(fileService) + dataService <- DataService.of(fileService, postgresService) +// dataService <- DataService.of(fileService) fetch <- FetchService.of fileFetchScheduler <- FileFetchScheduler.of(dataService, fetch) diff --git a/src/main/scala/db/DBConnection.scala b/src/main/scala/db/DBConnection.scala index 93bec56..d569bcf 100644 --- a/src/main/scala/db/DBConnection.scala +++ b/src/main/scala/db/DBConnection.scala @@ -6,22 +6,58 @@ import pureconfig._ import pureconfig.generic.auto._ import pureconfig.error.ConfigReaderFailures -case class PostgresConfig(username: String, password: String, url: String) +import scala.util.Try + +case class PostgresConfig(url: String, username: String, password: String) object DBConnection { - private def loadPostgresConfig: Either[ConfigReaderFailures, PostgresConfig] = { - ConfigSource.resources("postgres.conf").load[PostgresConfig] + private def getDatabaseUrl: String = + sys.env.getOrElse("DATABASE_URL", + "postgres://postgres:mysecretpassword@localhost:5432/weather-tool") + + private def parseUrl(url: String): Either[String, PostgresConfig] = { + Try { + val uri = new java.net.URI(if (url.startsWith("jdbc:")) url.substring(5) else url) + val userInfo = uri.getUserInfo.split(":") + val username = userInfo(0) + val password = userInfo(1) + val dbUrl = s"jdbc:postgresql://${uri.getHost}:${uri.getPort}${uri.getPath}" + + println(s"u: $username, p: $password, u: $url") + + PostgresConfig(dbUrl, username, password) + }.toEither.left.map(_.getMessage) } def transactor[F[_] : Async]: IO[Transactor[F]] = { - loadPostgresConfig match { + parseUrl(getDatabaseUrl) 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")) + case Left(error) => IO.raiseError(new RuntimeException(s"Failed to parse DATABASE_URL: $error")) } } } + +//case class PostgresConfig(url: String, username: String, password: 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")) +// } +// } +//} diff --git a/src/main/scala/db/DataService.scala b/src/main/scala/db/DataService.scala index d808127..be7709f 100644 --- a/src/main/scala/db/DataService.scala +++ b/src/main/scala/db/DataService.scala @@ -22,7 +22,7 @@ trait DataServiceTrait { object DataService { def of( fileService: FileService, -// postgresService: PostgresService + postgresService: PostgresService ): IO[DataService] = { for { log <- Slf4jLogger.create[IO] @@ -32,13 +32,13 @@ object DataService { .map(content => (fileName, content))) state = contents.toMap stateRef <- Ref.of[IO, Map[String, List[String]]](state) -// } yield new DataService(fileService, postgresService, new FileNameService(), log, stateRef) - } yield new DataService(fileService, new FileNameService(), log, stateRef) + } yield new DataService(fileService, postgresService, new FileNameService(), log, stateRef) +// } yield new DataService(fileService, new FileNameService(), log, stateRef) } } class DataService private( fileService: FileService, -// postgresService: PostgresService, + postgresService: PostgresService, fileNameService: FileNameService, log: Logger[IO], private val state: Ref[IO, Map[String, List[String]]] @@ -55,8 +55,8 @@ class DataService private( } def save(fileName: String, content: String): IO[String] = { - // TODO remove unsafeRunSync -// postgresService.save(fileName, content).unsafeRunSync() + // TODO replace unsafeRunSync to redeemWith + postgresService.save(fileName, content).unsafeRunSync() // TODO delete this fileService.save(fileName, content).redeemWith(