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

222 lines
9.3 KiB
Scala
Raw Normal View History

package server
2023-04-13 22:32:00 +03:00
import cats.effect._
import cats.implicits.toTraverseOps
2023-05-17 01:20:01 +03:00
import com.comcast.ip4s.IpLiteralSyntax
import data.DataService
2023-12-17 22:48:24 +02:00
import db.PostgresService
import fetch.csv.FetchService
2025-03-10 16:56:24 +02:00
import fetch.lvgmc
import fs2.io.file.Files
2025-02-14 12:58:19 +02:00
import server.ValidateRoutes.{AggFieldList, AggKey, CityList, DateTimeRange, Granularity, ValidateDate, ValidateDateTime, ValidateInt, ValidateMonths, ValidateZonedDateTime}
import io.circe.{Encoder, 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._
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
import io.circe.generic.auto._
import io.circe.generic.semiauto.deriveEncoder
import io.circe.syntax._
import parse.csv.Aggregate.AggregateValueImplicits.aggregateValueEncoder
import parse.csv.Aggregate.userQueryEncoder
import parse.csv.Aggregate.{AggregateKey, UserQuery}
import org.http4s.circe.jsonEncoder
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.slf4j.Slf4jLogger
import parse.csv.Aggregate
import fs2.io.file.{Files, Path}
2025-03-10 16:56:24 +02:00
import org.http4s.headers.`Content-Type`
2023-05-25 01:46:50 +03:00
import java.time.{ZoneOffset, ZonedDateTime}
2023-05-17 01:20:01 +03:00
import scala.concurrent.duration.DurationInt
2023-04-13 22:32:00 +03:00
object Server {
2025-02-13 16:11:50 +02:00
def of(postgresService: PostgresService, dataService: DataService, fetch: FetchService): IO[Server] = {
Slf4jLogger.create[IO].map {
2025-02-13 16:11:50 +02:00
new Server(postgresService, dataService, fetch, _)
}
}
}
2025-02-13 16:11:50 +02:00
class Server(postgresService: PostgresService, dataService: DataService, fetch: FetchService, log: Logger[IO]) {
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
private case class ResponseWrapper(result: Map[String, Option[Aggregate.AggregateValue]], query: UserQuery)
private val apiRoutes = HttpRoutes.of[IO] {
2025-03-10 16:56:24 +02:00
// http://0.0.0.0:8080/api/show/lvgmc-forecast/Latvija_LTV_pilsetas_tekosa_dn.csv
case GET -> Root / "show" / "lvgmc-forecast" / fileName =>
println("lvgmc-forecast GET")
lvgmc.FetchService.of.flatMap(f => f.fetchFile(fileName)).flatMap(bytes =>
Ok(bytes).map(_.withContentType(`Content-Type`(MediaType.text.csv)))
)
case GET -> Root / "show" / "gribName" => Ok("{\"fileName\":\"TODO replace this fake name\"}")
// http://0.0.0.0:8080/api/show/grib-all-structure
case GET -> Root / "show" / "grib-all-structure" =>
dataService.getAllFileStructure().flatMap(gribList => Ok(gribList.asJson))
// http://0.0.0.0:8080/api/show/grib-list
case GET -> Root / "show" / "grib-list" =>
2025-02-13 16:11:50 +02:00
dataService.getFileList().flatMap(fileList => Ok(fileList.asJson))
// http://0.0.0.0:8080/api/show/grib-name/harmonie_2025-02-01T1500Z_2025-02-01T180000Z.grib
case GET -> Root / "show" / "grib" / fileName =>
dataService.getGribStucture(fileName).flatMap(response => Ok(response.asJson.pretty))
2025-02-03 23:33:28 +02:00
case GET -> Root / "grib" / "binary-chunk" / ValidateInt(binaryOffset) / ValidateInt(binaryLength) / fileName =>
2025-02-13 16:11:50 +02:00
dataService.getBinaryChunk(binaryOffset, binaryLength, fileName).flatMap(buffer => Ok(buffer))
2025-03-05 23:25:42 +02:00
// http://0.0.0.0:8080/api/grib/delete-old-forecasts
case GET -> Root / "grib" / "delete-old-forecasts" =>
dataService.deleteOldForecasts().flatMap(result => Ok(result.asJson))
// http://0.0.0.0:8080/api/show/time
case GET -> Root / "show" / "time" =>
val nowUTC = ZonedDateTime.now(ZoneOffset.UTC)
val ageThreshold = nowUTC.minusHours(9)
Ok(ageThreshold.toString)
// http://0.0.0.0:8080/api/show/folder-structure
case GET -> Root / "show" / "folder-structure" => {
println("folder-structure")
case class FileInfo(name: String, isDirectory: Boolean)
case class DirectoryStructure(path: String, files: List[FileInfo])
case class FolderStructureResponse(
current: DirectoryStructure,
data: DirectoryStructure,
grib: DirectoryStructure
)
implicit val folderStructureResponseEncoder: Encoder[FolderStructureResponse] = deriveEncoder[FolderStructureResponse]
def getDirectoryStructure(path: Path): IO[DirectoryStructure] = {
for {
_ <- IO.println(path)
absolutePath <- IO(path.toString)
files <- Files[IO].list(path).compile.toList
fileInfos <- files.traverse { filePath =>
Files[IO].isDirectory(filePath).map { isDir =>
FileInfo(filePath.fileName.toString, isDir)
}
}
} yield DirectoryStructure(absolutePath, fileInfos)
}
val currentPath = Path(".")
val dataPath = Path("data")
val gribPath = Path("data/grib")
for {
current <- getDirectoryStructure(currentPath)
data <- getDirectoryStructure(dataPath)
grib <- getDirectoryStructure(gribPath)
response = FolderStructureResponse(current, data, grib)
result <- Ok(response.asJson)
} yield result
}
// http://0.0.0.0:8080/api/query/city/Liepāja,Rēzekne/20230414_2200-20230501_1230/hour/tempMax/max
case GET -> Root / "query" / "city" / CityList(cities) / DateTimeRange(from, to) / Granularity(granularity) / field / AggKey(key) =>
val userQuery = UserQuery(cities, field, key, granularity, from, to)
2023-12-17 22:48:24 +02:00
postgresService.query(userQuery)
.map(result => ResponseWrapper(result, userQuery))
.flatMap(responseWrapper => Ok(responseWrapper.asJson.pretty))
2023-04-19 14:22:28 +03:00
// http://0.0.0.0:8080/api/query/city/Kolka/20230414_2200-20230501_1230/allFields
case GET -> Root / "query" / "city" / (city: String) / DateTimeRange(from, to) / "allFields" =>
postgresService.queryCityAllFields(city, from, to).flatMap(result => Ok(result.asJson))
// http://0.0.0.0:8080/api/query/country/20230414_2200-20230501_1230/tempMax,tempMin,tempAvg,precipitation
case GET -> Root / "query" / "country" / DateTimeRange(from, to) / AggFieldList(fieldList) =>
postgresService.queryCountry(from, to, fieldList)
.flatMap(result => Ok(result.asJson.pretty))
2023-05-16 14:54:02 +03:00
// http://0.0.0.0:8080/api/fetch/date/20230514
2023-12-12 15:36:30 +02:00
case GET -> Root / "fetch" / "date" / ValidateDate(date) =>
2023-04-25 20:11:49 +03:00
val result = for {
fetchResultEither <- fetch.fetchFromDate(date).attempt
2023-05-01 16:43:21 +03:00
fetchServiceError = fetchResultEither.left.toOption.map(e => s"FetchServiceError: ${e.getMessage}").toList
fetchResult = fetchResultEither.getOrElse(List.empty)
(fetchErrors, successDownloads) = fetchResult.partitionMap(identity)
2023-12-11 00:12:52 +02:00
_ <- log.info(s"FETCHED SUCCESSFULLY files: ${successDownloads.size}")
2023-12-17 22:48:24 +02:00
saveResults <- successDownloads.traverse { case (name, content) => postgresService.save(name, content).attempt }
2023-05-01 16:43:21 +03:00
(saveErrors, successSaves) = saveResults.partitionMap(identity)
// 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}")
_ <- log.error(s"errors: $errors")
_ <- log.info(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
2023-08-11 16:22:15 +03:00
// http://0.0.0.0:8080/api/show/months/202304,202305,202306
case GET -> Root / "show" / "months" / ValidateMonths(monthList) =>
2023-12-17 22:48:24 +02:00
postgresService.getDatesByMonths(monthList).flatMap(dates =>
2023-08-11 16:22:15 +03:00
Ok(dates.asJson.pretty)
)
// http://0.0.0.0:8080/api/show/date/20230423
2023-12-12 15:36:30 +02:00
case GET -> Root / "show" / "date" / ValidateDate(date) =>
2023-12-17 22:48:24 +02:00
postgresService.getDateFileNames(date).flatMap(fileNames =>
2023-04-25 20:11:49 +03:00
Ok(fileNames.asJson.pretty)
)
2023-04-19 14:22:28 +03:00
2023-12-12 15:36:30 +02:00
// http://0.0.0.0:8080/api/show/datetime/20230423_1300
case GET -> Root / "show" / "datetime" / ValidateDateTime(datetime) =>
2023-12-17 22:48:24 +02:00
postgresService.getDateTimeEntries(datetime).flatMap(content => Ok(content.asJson))
2023-04-13 22:32:00 +03:00
}
private val corsConfig = CORSConfig.default
2023-05-17 01:20:01 +03:00
.withAnyOrigin(true)
.withAnyMethod(true)
.withAllowedMethods(Some(Set(Method.GET, Method.POST)))
.withAllowCredentials(false)
.withMaxAge(1.day)
private val apiRoutesCors = CORS(apiRoutes, corsConfig)
2023-05-14 23:01:36 +03:00
private val httpApp = Router(
"/" -> staticcontent.fileService[IO](FileService.Config("./web/dist")),
"/api" -> apiRoutesCors,
2025-02-14 12:58:19 +02:00
// TODO rewrite in more generic way
"/station" -> staticcontent.fileService[IO](FileService.Config("./web/dist")),
"/cities" -> staticcontent.fileService[IO](FileService.Config("./web/dist")),
"/latvia" -> staticcontent.fileService[IO](FileService.Config("./web/dist")),
"/database" -> staticcontent.fileService[IO](FileService.Config("./web/dist")),
"/harmonie" -> staticcontent.fileService[IO](FileService.Config("./web/dist")),
2025-03-10 16:56:24 +02:00
"/lvgmc-forecast" -> staticcontent.fileService[IO](FileService.Config("./web/dist")),
).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
}