Blaze serves also static index.html and bundle.js

This commit is contained in:
Guntis Smaukstelis
2023-05-14 21:27:57 +03:00
parent 3e41326f91
commit bbf1eb0c01
6 changed files with 28 additions and 16 deletions
+19 -13
View File
@@ -9,12 +9,13 @@ import server.ValidateRoutes.{AggKey, CityList, DateTimeRange, ValidDate}
import io.circe.{Json, Printer}
import org.http4s._
import org.http4s.dsl.io._
import org.http4s.server.Router
import org.http4s.server.{Router, staticcontent}
import org.http4s.server.blaze.BlazeServerBuilder
import io.circe.syntax._
import parse.Aggregate.AggregateValueImplicits.aggregateValueEncoder
import parse.Aggregate.{AggregateKey, UserQuery}
import fs2.Stream
import org.http4s.server.staticcontent.FileService
object Server {
@@ -27,15 +28,15 @@ object Server {
}
}
private val appRoutes = HttpRoutes.of[IO] {
private val apiRoutes = HttpRoutes.of[IO] {
// http://localhost:3000/query/20230414_2200-20230501_1230/Liepāja,Rēzekne/tempMax/max
// http://0.0.0.0:8080/api/query/20230414_2200-20230501_1230/Liepāja,Rēzekne/tempMax/max
case GET -> Root / "query" / DateTimeRange(from, to) / CityList(cities) / field / AggKey(key) =>
DBService.getInRange(from, to)
.map(Parser.queryData(UserQuery(cities, field, key), _))
.flatMap(result => Ok(result.asJson.pretty))
// http://localhost:3000/fetch/date/20230423
// http://0.0.0.0:8080/api/fetch/date/20230423
case GET -> Root / "fetch" / "date" / ValidDate(date) =>
val result = for {
fetchResultEither <- FetchService.fetchFromDate(date).attempt
@@ -57,33 +58,38 @@ object Server {
).pretty)
}
// http://localhost:3000/show/all_dates
// http://0.0.0.0:8080/api/show/all_dates
case GET -> Root / "show" / "all_dates" =>
DBService.getDates().flatMap(dates =>
Ok(dates.asJson.pretty)
)
// http://localhost:3000/show/date/20230423
// http://0.0.0.0:8080/api/show/date/20230423
case GET -> Root / "show" / "date" / ValidDate(date) =>
DBService.getDateFileNames(date).flatMap(fileNames =>
Ok(fileNames.asJson.pretty)
)
// http://localhost:3000/help
case GET -> Root / "help" =>
// http://0.0.0.0:8080/api/help
case GET -> Root / "help" => {
val host = "weather-tool.fly.dev"
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",
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",
).asJson,
).pretty)
}
}
private val httpApp = Router("/" -> appRoutes).orNotFound
private val httpApp = Router(
"/" -> staticcontent.fileService[IO](FileService.Config("./web/dist")),
"/api" -> apiRoutes
).orNotFound
def run: Stream[IO, ExitCode] =
BlazeServerBuilder[IO]