Files
WeatherTool/src/main/scala/server/Server.scala
T

96 lines
3.5 KiB
Scala
Raw Normal View History

package server
2023-04-13 22:32:00 +03:00
import cats.effect._
import cats.implicits.toTraverseOps
import db.DBService
import fetch.FetchService
2023-05-03 22:37:05 +03:00
import parse.{Parser, WeatherData}
import server.ValidateRoutes.{AggKey, CityList, DateTimeRange, ValidDate}
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
import io.circe.syntax._
import parse.Aggregate.AggregateValueImplicits.aggregateValueEncoder
import parse.Aggregate.{AggregateKey, UserQuery}
2023-04-13 22:32:00 +03:00
object Server extends IOApp {
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] {
// 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)
.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
// 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-25 20:11:49 +03:00
result.flatMap { case (successes, errors) =>
Ok(Json.obj(
"errors" -> errors.asJson,
"successes" -> successes.asJson
).pretty)
}
2023-04-19 14:22:28 +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)
)
// 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-05-03 22:37:05 +03:00
Ok(Json.obj(
"aggregate fields" -> WeatherData.getKeys.asJson,
"aggregate keys" -> AggregateKey.getKeys.asJson,
"example urls" -> List(
"http://localhost:3000/query/20230414_2200-20230501_1230/Liepāja,Rēzekne/tempMax/max",
"http://localhost:3000/fetch/date/20230423",
"http://localhost:3000/show/all_dates",
"http://localhost:3000/show/date/20230423",
).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]
2023-05-12 23:57:04 +03:00
.bindHttp(8080, "0.0.0.0")
2023-04-13 22:32:00 +03:00
.withHttpApp(httpApp)
.serve
.compile
.drain
.as(ExitCode.Success)
}