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
**/.DS_Store
!**/project/assembly.sbt
!**/web/dist/index.html
!**/web/dist/bundle.js
# flyctl launch added from .idea/.gitignore
# Default ignored files
+1 -1
View File
@@ -1,6 +1,6 @@
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
VOLUME /data
-1
View File
@@ -49,7 +49,6 @@ object FetchService {
}
def fetchSingleFile(fileName: String): IO[(String, String)] = {
println("start fetching")
fetchFiles(List(fileName)).flatMap { results =>
results.headOption match {
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 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]
+3
View File
@@ -0,0 +1,3 @@
window.onload = () => {
console.log("this comes from bundle.js")
}
+2
View File
@@ -6,5 +6,7 @@
</head>
<body>
<h1>Hello, Weather!</h1>
<script type="text/javascript" src="./bundle.js"></script>
</body>
</html>