Server which responds to get requests

This commit is contained in:
Guntis Smaukstelis
2023-04-13 22:32:00 +03:00
parent 40156d6283
commit 260b6ef23a
8 changed files with 390 additions and 8 deletions
+1 -1
View File
@@ -41,7 +41,7 @@ object FetchData extends IOApp.Simple {
}
def run: IO[Unit] = {
val fileNames = FileName.generateLast5Hours()
val fileNames = FileName.generateLastNHours(10)
// fileNames.foreach(println)
val urls = fileNames.map(baseUrl / _)
BlazeClientBuilder[IO](global).resource.use { client =>
+6 -5
View File
@@ -5,16 +5,17 @@ import java.time.{LocalDateTime, Duration}
object FileName {
private val interval = Duration.ofMinutes(30)
private val formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm")s
private val formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm")
def generateLastHour(): List[String] = {
val now = LocalDateTime.now
generate(now.minusHours(1), now)
}
def generateLast5Hours(): List[String] = {
val now = roundToInterval(LocalDateTime.now, false)
generate(now.minusHours(5), now)
// TODO side effect
def generateLastNHours(hours: Int, now: LocalDateTime = LocalDateTime.now): List[String] = {
val roundNow = roundToInterval(now, false)
generate(roundNow.minusHours(hours), roundNow)
}
private def roundToInterval(time: LocalDateTime, roundUp: Boolean): LocalDateTime = {
@@ -33,7 +34,7 @@ object FileName {
Iterator.iterate(roundStartTime) { time =>
time.plus(interval)
}.takeWhile(!_.isAfter(roundEndTime))
.filter(_.getMinute == 30)
.filter(_.getMinute == 30) // current server accepts only 2:30 3:30 4:30, etc. Later will be available each 10min or even 1min
.map(time => s"${formatter.format(time)}.csv")
.toList
}
+48 -2
View File
@@ -1,5 +1,51 @@
package server
class Server {
// TODO proly BlazeServerBuilder which accepts requests from client and serves parsed data
import cats.effect._
import org.http4s._
import org.http4s.dsl.io._
import org.http4s.implicits._
import org.http4s.server.Router
import org.http4s.server.blaze.BlazeServerBuilder
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import scala.util.Try
object Server extends IOApp {
private val formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmm")
private val appRoutes = HttpRoutes.of[IO] {
// http://localhost:3000/202304131030-202304132030/riga,liepaja/tempAvg
case GET -> Root / timestampRange / cities / weatherParam =>
// TODO proly better to Validated with chained errors
val res = for {
(from, to) <- timestampRange.split("-").toList
.map(str => Try(LocalDateTime.parse(str, formatter)).toOption) match {
case List(Some(from), Some(to)) => Some(from, to)
case _ => None
}
cityList <- cities.split(",").toList match {
case list => Some(list)
case Nil => None
}
param <- if (weatherParam.isEmpty) None else Some(weatherParam)
}
// TODO yield to parser method which accepts those args
yield s"Timestamp From: $from, Timestamp To: $to, Cities: ${cityList.mkString(", ")}, Weather Param: $param"
res match {
case Some(responseTxt) => Ok(responseTxt)
case _ => BadRequest(s"Invalid request format")
}
}
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)
}