Connect to local/fly database, insert new weather data

This commit is contained in:
Guntis Smaukstelis
2023-12-10 18:47:56 +02:00
parent 526426139c
commit f956d658d5
3 changed files with 54 additions and 17 deletions
+7 -6
View File
@@ -8,15 +8,16 @@ import server.Server
object Main extends IOApp { object Main extends IOApp {
def run(args: List[String]): IO[ExitCode] = { 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 { for {
// transactor <- DBConnection.transactor[IO] transactor <- DBConnection.transactor[IO]
// postgresService <- PostgresService.of(transactor) postgresService <- PostgresService.of(transactor)
// _ <- postgresService.createWeatherTable // create table if it does not exists _ <- 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)
dataService <- DataService.of(fileService) // dataService <- DataService.of(fileService)
fetch <- FetchService.of fetch <- FetchService.of
fileFetchScheduler <- FileFetchScheduler.of(dataService, fetch) fileFetchScheduler <- FileFetchScheduler.of(dataService, fetch)
+41 -5
View File
@@ -6,22 +6,58 @@ import pureconfig._
import pureconfig.generic.auto._ import pureconfig.generic.auto._
import pureconfig.error.ConfigReaderFailures 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 { object DBConnection {
private def loadPostgresConfig: Either[ConfigReaderFailures, PostgresConfig] = { private def getDatabaseUrl: String =
ConfigSource.resources("postgres.conf").load[PostgresConfig] 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]] = { def transactor[F[_] : Async]: IO[Transactor[F]] = {
loadPostgresConfig match { parseUrl(getDatabaseUrl) match {
case Right(config) => IO(Transactor.fromDriverManager[F]( case Right(config) => IO(Transactor.fromDriverManager[F](
"org.postgresql.Driver", "org.postgresql.Driver",
config.url, config.url,
config.username, config.username,
config.password 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"))
// }
// }
//}
+6 -6
View File
@@ -22,7 +22,7 @@ trait DataServiceTrait {
object DataService { object DataService {
def of( def of(
fileService: FileService, fileService: FileService,
// postgresService: PostgresService postgresService: PostgresService
): IO[DataService] = { ): IO[DataService] = {
for { for {
log <- Slf4jLogger.create[IO] log <- Slf4jLogger.create[IO]
@@ -32,13 +32,13 @@ object DataService {
.map(content => (fileName, content))) .map(content => (fileName, content)))
state = contents.toMap state = contents.toMap
stateRef <- Ref.of[IO, Map[String, List[String]]](state) stateRef <- Ref.of[IO, Map[String, List[String]]](state)
// } yield new DataService(fileService, postgresService, new FileNameService(), log, stateRef) } yield new DataService(fileService, postgresService, new FileNameService(), log, stateRef)
} yield new DataService(fileService, new FileNameService(), log, stateRef) // } yield new DataService(fileService, new FileNameService(), log, stateRef)
} }
} }
class DataService private( class DataService private(
fileService: FileService, fileService: FileService,
// postgresService: PostgresService, postgresService: PostgresService,
fileNameService: FileNameService, fileNameService: FileNameService,
log: Logger[IO], log: Logger[IO],
private val state: Ref[IO, Map[String, List[String]]] private val state: Ref[IO, Map[String, List[String]]]
@@ -55,8 +55,8 @@ class DataService private(
} }
def save(fileName: String, content: String): IO[String] = { def save(fileName: String, content: String): IO[String] = {
// TODO remove unsafeRunSync // TODO replace unsafeRunSync to redeemWith
// postgresService.save(fileName, content).unsafeRunSync() postgresService.save(fileName, content).unsafeRunSync()
// TODO delete this // TODO delete this
fileService.save(fileName, content).redeemWith( fileService.save(fileName, content).redeemWith(