2023-04-13 16:58:56 +03:00
|
|
|
package server
|
|
|
|
|
|
2023-04-13 22:32:00 +03:00
|
|
|
import cats.effect._
|
2023-04-23 22:41:38 +03:00
|
|
|
import cats.implicits.toTraverseOps
|
|
|
|
|
import db.DBService
|
|
|
|
|
import fetch.FetchService
|
2023-05-03 00:02:59 +03:00
|
|
|
import parse.Parser
|
|
|
|
|
import server.ValidateRoutes.{AggKey, CityList, DateTimeRange, ValidDate}
|
2023-04-26 16:37:11 +03:00
|
|
|
import io.circe.{Encoder, Json, Printer}
|
2023-04-13 22:32:00 +03:00
|
|
|
import org.http4s._
|
|
|
|
|
import org.http4s.dsl.io._
|
|
|
|
|
import org.http4s.server.Router
|
|
|
|
|
import org.http4s.server.blaze.BlazeServerBuilder
|
2023-04-23 22:41:38 +03:00
|
|
|
import io.circe.syntax._
|
2023-05-03 00:02:59 +03:00
|
|
|
import parse.Aggregate.AggregateValueImplicits.aggregateValueEncoder
|
|
|
|
|
import parse.Aggregate.{AggregateKey, UserQuery}
|
2023-04-13 22:32:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
object Server extends IOApp {
|
2023-04-18 17:18:45 +03:00
|
|
|
|
2023-04-25 20:11:49 +03:00
|
|
|
// Define the extension method `pretty` for Json
|
|
|
|
|
implicit class JsonPrettyPrinter(json: Json) {
|
|
|
|
|
def pretty: String = {
|
|
|
|
|
val printer = Printer.spaces2.copy(dropNullValues = true)
|
|
|
|
|
printer.print(json)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-13 22:32:00 +03:00
|
|
|
|
2023-04-25 20:11:49 +03:00
|
|
|
private val appRoutes = HttpRoutes.of[IO] {
|
2023-04-19 17:34:27 +03:00
|
|
|
|
2023-05-03 00:02:59 +03:00
|
|
|
// http://localhost:3000/query/20230414_2200-20230501_1230/Liepāja,Rēzekne/tempMax/max
|
|
|
|
|
case GET -> Root / "query" / DateTimeRange(from, to) / CityList(cities) / field / AggKey(key) =>
|
2023-04-25 20:11:49 +03:00
|
|
|
DBService.getInRange(from, to)
|
2023-05-03 00:02:59 +03:00
|
|
|
.map(Parser.queryData(UserQuery(cities, field, key), _))
|
2023-04-25 20:11:49 +03:00
|
|
|
.flatMap(result => Ok(result.asJson.pretty))
|
2023-04-19 14:22:28 +03:00
|
|
|
|
2023-04-24 00:01:06 +03:00
|
|
|
// http://localhost:3000/fetch/date/20230423
|
2023-04-25 20:11:49 +03:00
|
|
|
case GET -> Root / "fetch" / "date" / ValidDate(date) =>
|
|
|
|
|
val result = for {
|
2023-05-01 16:43:21 +03:00
|
|
|
fetchResultEither <- FetchService.fetchFromDate(date).attempt
|
|
|
|
|
fetchServiceError = fetchResultEither.left.toOption.map(e => s"FetchServiceError: ${e.getMessage}").toList
|
|
|
|
|
fetchResult = fetchResultEither.getOrElse(List.empty)
|
|
|
|
|
(fetchErrors, successDownloads) = fetchResult.partitionMap(identity)
|
|
|
|
|
saveResults <- successDownloads.traverse { case (name, content) => DBService.save(name, content) }
|
|
|
|
|
(saveErrors, successSaves) = saveResults.partitionMap(identity)
|
2023-04-25 20:11:49 +03:00
|
|
|
successes = successDownloads.map(s => s"fetched: ${s._1}") ++ successSaves.map(s => s"saved: $s")
|
2023-05-01 16:43:21 +03:00
|
|
|
errors = fetchServiceError ++ fetchErrors.map(e => s"FetchError: ${e.getMessage}") ++ saveErrors.map(e => s"SaveError: ${e.getMessage}")
|
|
|
|
|
_ <- IO.println(s"errors: $errors")
|
|
|
|
|
_ <- IO.println(s"successes: $successes")
|
2023-04-25 20:11:49 +03:00
|
|
|
} yield (successes, errors)
|
2023-04-23 22:41:38 +03:00
|
|
|
|
2023-04-25 20:11:49 +03:00
|
|
|
result.flatMap { case (successes, errors) =>
|
|
|
|
|
Ok(Json.obj(
|
|
|
|
|
"errors" -> errors.asJson,
|
|
|
|
|
"successes" -> successes.asJson
|
|
|
|
|
).pretty)
|
2023-04-23 22:41:38 +03:00
|
|
|
}
|
2023-04-19 14:22:28 +03:00
|
|
|
|
2023-04-24 00:01:06 +03:00
|
|
|
// http://localhost:3000/show/all_dates
|
|
|
|
|
case GET -> Root / "show" / "all_dates" =>
|
2023-04-25 20:11:49 +03:00
|
|
|
DBService.getDates().flatMap(dates =>
|
|
|
|
|
Ok(dates.asJson.pretty)
|
|
|
|
|
)
|
2023-04-24 00:01:06 +03:00
|
|
|
|
|
|
|
|
// http://localhost:3000/show/date/20230423
|
2023-04-25 20:11:49 +03:00
|
|
|
case GET -> Root / "show" / "date" / ValidDate(date) =>
|
|
|
|
|
DBService.getDateFileNames(date).flatMap(fileNames =>
|
|
|
|
|
Ok(fileNames.asJson.pretty)
|
|
|
|
|
)
|
2023-04-19 14:22:28 +03:00
|
|
|
|
2023-04-25 20:11:49 +03:00
|
|
|
// http://localhost:3000/help
|
|
|
|
|
case GET -> Root / "help" =>
|
2023-04-26 16:37:11 +03:00
|
|
|
Ok(AggregateKey.getKeys.asJson.pretty)
|
2023-04-13 22:32:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private val httpApp = Router("/" -> appRoutes).orNotFound
|
|
|
|
|
|
|
|
|
|
override def run(args: List[String]): IO[ExitCode] =
|
|
|
|
|
BlazeServerBuilder[IO]
|
|
|
|
|
.bindHttp(3000, "localhost")
|
|
|
|
|
.withHttpApp(httpApp)
|
|
|
|
|
.serve
|
|
|
|
|
.compile
|
|
|
|
|
.drain
|
|
|
|
|
.as(ExitCode.Success)
|
2023-04-13 16:58:56 +03:00
|
|
|
}
|