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
+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)
}