From 851229ce9b27f088354c9afc098973e3de593afd Mon Sep 17 00:00:00 2001 From: Guntis Smaukstelis Date: Sun, 14 May 2023 23:01:36 +0300 Subject: [PATCH] Set up cors and env variables --- src/main/scala/server/Server.scala | 5 ++++- web/.env.development | 1 + web/.env.production | 1 + web/src/App.tsx | 5 +++++ web/src/FetchedDates.tsx | 35 ++++++++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 web/.env.development create mode 100644 web/.env.production create mode 100644 web/src/FetchedDates.tsx diff --git a/src/main/scala/server/Server.scala b/src/main/scala/server/Server.scala index 4ac5860..eb51deb 100644 --- a/src/main/scala/server/Server.scala +++ b/src/main/scala/server/Server.scala @@ -15,6 +15,7 @@ import io.circe.syntax._ import parse.Aggregate.AggregateValueImplicits.aggregateValueEncoder import parse.Aggregate.{AggregateKey, UserQuery} import fs2.Stream +import org.http4s.server.middleware.CORS import org.http4s.server.staticcontent.FileService @@ -86,9 +87,11 @@ object Server { } } + val apiRoutesCors = CORS(apiRoutes) + private val httpApp = Router( "/" -> staticcontent.fileService[IO](FileService.Config("./web/dist")), - "/api" -> apiRoutes + "/api" -> apiRoutesCors ).orNotFound def run: Stream[IO, ExitCode] = diff --git a/web/.env.development b/web/.env.development new file mode 100644 index 0000000..59148f7 --- /dev/null +++ b/web/.env.development @@ -0,0 +1 @@ +VITE_API_HOST=http://localhost:8080 diff --git a/web/.env.production b/web/.env.production new file mode 100644 index 0000000..602ea08 --- /dev/null +++ b/web/.env.production @@ -0,0 +1 @@ +VITE_API_HOST=https://weather-tool.fly.dev \ No newline at end of file diff --git a/web/src/App.tsx b/web/src/App.tsx index 447caab..bab8dcf 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -2,11 +2,16 @@ import type { Component } from 'solid-js'; import logo from './assets/logo.svg'; import styles from './css/App.module.css'; +import { FetchedDates } from './FetchedDates'; + +console.log("env:", import.meta.env.MODE); +console.log("api host:", import.meta.env.VITE_API_HOST); const App: Component = () => { return (
+ logo

Weather tool web diff --git a/web/src/FetchedDates.tsx b/web/src/FetchedDates.tsx new file mode 100644 index 0000000..6be6a55 --- /dev/null +++ b/web/src/FetchedDates.tsx @@ -0,0 +1,35 @@ +import { createResource } from "solid-js"; + +const apiHost = import.meta.env.VITE_API_HOST; + +export function FetchedDates() { + const fetchDates = async () => { + const response = await fetch(`${apiHost}/api/show/all_dates`); + const json = await response.json(); + return json; + } + + const [datesResource] = createResource(fetchDates); + + return ( +

+ { datesResource.loading && ( +
Loading dates
+ )} + { datesResource.error && ( +
Error while loading dates: ${datesResource.error}
+ )} + { datesResource() && ( +
    + { (datesResource() as any).map((date: any) => item(date))} +
+ )} +
+ ); +} + +function item(date: any) { + return ( +
  • {date}
  • + ) +} \ No newline at end of file