Connect to local/fly database, insert new weather data
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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"))
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user