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
|
2023-05-17 01:20:01 +03:00
|
|
|
import com.comcast.ip4s.IpLiteralSyntax
|
2023-04-23 22:41:38 +03:00
|
|
|
import db.DBService
|
|
|
|
|
import fetch.FetchService
|
2023-05-03 22:37:05 +03:00
|
|
|
import parse.{Parser, WeatherData}
|
2023-05-03 00:02:59 +03:00
|
|
|
import server.ValidateRoutes.{AggKey, CityList, DateTimeRange, ValidDate}
|
2023-05-14 15:48:52 +03:00
|
|
|
import io.circe.{Json, Printer}
|
2023-04-13 22:32:00 +03:00
|
|
|
import org.http4s._
|
|
|
|
|
import org.http4s.dsl.io._
|
2023-05-17 01:20:01 +03:00
|
|
|
import org.http4s.implicits._
|
2023-05-14 21:27:57 +03:00
|
|
|
import org.http4s.server.{Router, staticcontent}
|
2023-05-17 01:20:01 +03:00
|
|
|
import org.http4s.server.middleware.CORS
|
|
|
|
|
import org.http4s.server.middleware.CORSConfig
|
|
|
|
|
import org.http4s.server.staticcontent.FileService
|
|
|
|
|
import org.http4s.ember.server.EmberServerBuilder
|
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-05-14 15:48:52 +03:00
|
|
|
import fs2.Stream
|
2023-05-17 01:20:01 +03:00
|
|
|
|
|
|
|
|
import scala.concurrent.duration.DurationInt
|
2023-04-13 22:32:00 +03:00
|
|
|
|
|
|
|
|
|
2023-05-14 15:48:52 +03:00
|
|
|
object Server {
|
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-05-14 21:27:57 +03:00
|
|
|
private val apiRoutes = HttpRoutes.of[IO] {
|
2023-04-19 17:34:27 +03:00
|
|
|
|
2023-05-14 21:27:57 +03:00
|
|
|
// http://0.0.0.0:8080/api/query/20230414_2200-20230501_1230/Liepāja,Rēzekne/tempMax/max
|
2023-05-03 00:02:59 +03:00
|
|
|
case GET -> Root / "query" / DateTimeRange(from, to) / CityList(cities) / field / AggKey(key) =>
|
2023-05-20 20:20:14 +03:00
|
|
|
DBService.of.flatMap(_.getInRange(from, to)
|
2023-05-03 00:02:59 +03:00
|
|
|
.map(Parser.queryData(UserQuery(cities, field, key), _))
|
2023-05-20 20:20:14 +03:00
|
|
|
.flatMap(result => Ok(result.asJson.pretty)))
|
2023-04-19 14:22:28 +03:00
|
|
|
|
2023-05-16 14:54:02 +03:00
|
|
|
// http://0.0.0.0:8080/api/fetch/date/20230514
|
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)
|
2023-05-20 20:20:14 +03:00
|
|
|
dbService <- DBService.of
|
|
|
|
|
saveResults <- successDownloads.traverse { case (name, content) => dbService.save(name, content) }
|
2023-05-01 16:43:21 +03:00
|
|
|
(saveErrors, successSaves) = saveResults.partitionMap(identity)
|
2023-05-17 16:28:56 +03:00
|
|
|
// successes = successDownloads.map(s => s"fetched: ${s._1}") ++ successSaves.map(s => s"saved: $s")
|
|
|
|
|
successes = successSaves
|
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-05-14 21:27:57 +03:00
|
|
|
// http://0.0.0.0:8080/api/show/all_dates
|
2023-04-24 00:01:06 +03:00
|
|
|
case GET -> Root / "show" / "all_dates" =>
|
2023-05-20 20:20:14 +03:00
|
|
|
DBService.of.flatMap(_.getDates().flatMap(dates =>
|
2023-04-25 20:11:49 +03:00
|
|
|
Ok(dates.asJson.pretty)
|
2023-05-20 20:20:14 +03:00
|
|
|
))
|
2023-04-24 00:01:06 +03:00
|
|
|
|
2023-05-14 21:27:57 +03:00
|
|
|
// http://0.0.0.0:8080/api/show/date/20230423
|
2023-04-25 20:11:49 +03:00
|
|
|
case GET -> Root / "show" / "date" / ValidDate(date) =>
|
2023-05-20 20:20:14 +03:00
|
|
|
DBService.of.flatMap(_.getDateFileNames(date).flatMap(fileNames =>
|
2023-04-25 20:11:49 +03:00
|
|
|
Ok(fileNames.asJson.pretty)
|
2023-05-20 20:20:14 +03:00
|
|
|
))
|
2023-04-19 14:22:28 +03:00
|
|
|
|
2023-05-15 16:37:03 +03:00
|
|
|
// http://0.0.0.0:8080/api/show/file/20230423_12:30.csv
|
|
|
|
|
case GET -> Root / "show" / "file" / (fileName: String) =>
|
2023-05-20 20:20:14 +03:00
|
|
|
DBService.of.flatMap(_.getFileContent(fileName).flatMap(Ok(_)))
|
2023-05-15 16:37:03 +03:00
|
|
|
|
2023-05-14 21:27:57 +03:00
|
|
|
// http://0.0.0.0:8080/api/help
|
|
|
|
|
case GET -> Root / "help" => {
|
|
|
|
|
val host = "weather-tool.fly.dev"
|
2023-05-03 22:37:05 +03:00
|
|
|
Ok(Json.obj(
|
|
|
|
|
"aggregate fields" -> WeatherData.getKeys.asJson,
|
|
|
|
|
"aggregate keys" -> AggregateKey.getKeys.asJson,
|
|
|
|
|
"example urls" -> List(
|
2023-05-14 21:27:57 +03:00
|
|
|
s"https://$host/api/query/20230414_2200-20230501_1230/Liepāja,Rēzekne/tempMax/max",
|
|
|
|
|
s"https://$host/api/fetch/date/20230423",
|
|
|
|
|
s"https://$host/api/show/all_dates",
|
|
|
|
|
s"https://$host/api/show/date/20230423",
|
2023-05-03 22:37:05 +03:00
|
|
|
).asJson,
|
|
|
|
|
).pretty)
|
2023-05-14 21:27:57 +03:00
|
|
|
}
|
2023-04-13 22:32:00 +03:00
|
|
|
}
|
|
|
|
|
|
2023-05-17 01:20:01 +03:00
|
|
|
val corsConfig = CORSConfig.default
|
|
|
|
|
.withAnyOrigin(true)
|
|
|
|
|
.withAnyMethod(true)
|
|
|
|
|
.withAllowedMethods(Some(Set(Method.GET, Method.POST)))
|
|
|
|
|
.withAllowCredentials(false)
|
|
|
|
|
.withMaxAge(1.day)
|
|
|
|
|
|
|
|
|
|
val apiRoutesCors = CORS(apiRoutes, corsConfig)
|
2023-05-14 23:01:36 +03:00
|
|
|
|
2023-05-14 21:27:57 +03:00
|
|
|
private val httpApp = Router(
|
|
|
|
|
"/" -> staticcontent.fileService[IO](FileService.Config("./web/dist")),
|
2023-05-14 23:01:36 +03:00
|
|
|
"/api" -> apiRoutesCors
|
2023-05-14 21:27:57 +03:00
|
|
|
).orNotFound
|
2023-04-13 22:32:00 +03:00
|
|
|
|
2023-05-17 01:20:01 +03:00
|
|
|
def run: IO[ExitCode] =
|
|
|
|
|
EmberServerBuilder
|
|
|
|
|
.default[IO]
|
|
|
|
|
.withHost(ipv4"0.0.0.0")
|
|
|
|
|
.withPort(port"8080")
|
|
|
|
|
.withHttpApp(httpApp)
|
|
|
|
|
.build
|
|
|
|
|
.useForever
|
2023-04-13 16:58:56 +03:00
|
|
|
}
|