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
+2
View File
@@ -25,6 +25,8 @@
**/logs **/logs
**/.DS_Store **/.DS_Store
!**/project/assembly.sbt !**/project/assembly.sbt
!**/web/dist/index.html
!**/web/dist/bundle.js
# flyctl launch added from .idea/.gitignore # flyctl launch added from .idea/.gitignore
# Default ignored files # Default ignored files
+1 -1
View File
@@ -1,6 +1,6 @@
FROM amazoncorretto:17-alpine FROM amazoncorretto:17-alpine
COPY ./web /web COPY ./web/dist/bundle.js ./web/dist/index.html /web/dist/
COPY ./target/scala-2.13/WeatherTool-assembly-0.1.1-SNAPSHOT.jar /app.jar COPY ./target/scala-2.13/WeatherTool-assembly-0.1.1-SNAPSHOT.jar /app.jar
VOLUME /data VOLUME /data
-1
View File
@@ -49,7 +49,6 @@ object FetchService {
} }
def fetchSingleFile(fileName: String): IO[(String, String)] = { def fetchSingleFile(fileName: String): IO[(String, String)] = {
println("start fetching")
fetchFiles(List(fileName)).flatMap { results => fetchFiles(List(fileName)).flatMap { results =>
results.headOption match { results.headOption match {
case Some(Right(result)) => IO.pure(result) case Some(Right(result)) => IO.pure(result)
+19 -13
View File
@@ -9,12 +9,13 @@ import server.ValidateRoutes.{AggKey, CityList, DateTimeRange, ValidDate}
import io.circe.{Json, Printer} import io.circe.{Json, Printer}
import org.http4s._ import org.http4s._
import org.http4s.dsl.io._ import org.http4s.dsl.io._
import org.http4s.server.Router import org.http4s.server.{Router, staticcontent}
import org.http4s.server.blaze.BlazeServerBuilder import org.http4s.server.blaze.BlazeServerBuilder
import io.circe.syntax._ import io.circe.syntax._
import parse.Aggregate.AggregateValueImplicits.aggregateValueEncoder import parse.Aggregate.AggregateValueImplicits.aggregateValueEncoder
import parse.Aggregate.{AggregateKey, UserQuery} import parse.Aggregate.{AggregateKey, UserQuery}
import fs2.Stream import fs2.Stream
import org.http4s.server.staticcontent.FileService
object Server { 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) => case GET -> Root / "query" / DateTimeRange(from, to) / CityList(cities) / field / AggKey(key) =>
DBService.getInRange(from, to) DBService.getInRange(from, to)
.map(Parser.queryData(UserQuery(cities, field, key), _)) .map(Parser.queryData(UserQuery(cities, field, key), _))
.flatMap(result => Ok(result.asJson.pretty)) .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) => case GET -> Root / "fetch" / "date" / ValidDate(date) =>
val result = for { val result = for {
fetchResultEither <- FetchService.fetchFromDate(date).attempt fetchResultEither <- FetchService.fetchFromDate(date).attempt
@@ -57,33 +58,38 @@ object Server {
).pretty) ).pretty)
} }
// http://localhost:3000/show/all_dates // http://0.0.0.0:8080/api/show/all_dates
case GET -> Root / "show" / "all_dates" => case GET -> Root / "show" / "all_dates" =>
DBService.getDates().flatMap(dates => DBService.getDates().flatMap(dates =>
Ok(dates.asJson.pretty) 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) => case GET -> Root / "show" / "date" / ValidDate(date) =>
DBService.getDateFileNames(date).flatMap(fileNames => DBService.getDateFileNames(date).flatMap(fileNames =>
Ok(fileNames.asJson.pretty) Ok(fileNames.asJson.pretty)
) )
// http://localhost:3000/help // http://0.0.0.0:8080/api/help
case GET -> Root / "help" => case GET -> Root / "help" => {
val host = "weather-tool.fly.dev"
Ok(Json.obj( Ok(Json.obj(
"aggregate fields" -> WeatherData.getKeys.asJson, "aggregate fields" -> WeatherData.getKeys.asJson,
"aggregate keys" -> AggregateKey.getKeys.asJson, "aggregate keys" -> AggregateKey.getKeys.asJson,
"example urls" -> List( "example urls" -> List(
"http://localhost:3000/query/20230414_2200-20230501_1230/Liepāja,Rēzekne/tempMax/max", s"https://$host/api/query/20230414_2200-20230501_1230/Liepāja,Rēzekne/tempMax/max",
"http://localhost:3000/fetch/date/20230423", s"https://$host/api/fetch/date/20230423",
"http://localhost:3000/show/all_dates", s"https://$host/api/show/all_dates",
"http://localhost:3000/show/date/20230423", s"https://$host/api/show/date/20230423",
).asJson, ).asJson,
).pretty) ).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] = def run: Stream[IO, ExitCode] =
BlazeServerBuilder[IO] BlazeServerBuilder[IO]
+3
View File
@@ -0,0 +1,3 @@
window.onload = () => {
console.log("this comes from bundle.js")
}
+3 -1
View File
@@ -5,6 +5,8 @@
<title>WeatherTool</title> <title>WeatherTool</title>
</head> </head>
<body> <body>
<h1>Hello, Weather!</h1> <h1>Hello, Weather!</h1>
<script type="text/javascript" src="./bundle.js"></script>
</body> </body>
</html> </html>