From e0dd7d2bdeb902b6c167a47336ea8d0fbca52849 Mon Sep 17 00:00:00 2001 From: Akis Date: Sat, 18 Feb 2023 18:19:18 +0200 Subject: [PATCH] new data loading (only announcements for now) --- src/hooks.server.ts | 28 ++++++++++++++++++++++++++++ src/routes/+page.server.ts | 22 +++++----------------- src/stores.ts | 3 +++ 3 files changed, 36 insertions(+), 17 deletions(-) create mode 100644 src/stores.ts diff --git a/src/hooks.server.ts b/src/hooks.server.ts index 97c6008..c46f43b 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -5,6 +5,13 @@ import type { Provider } from "@auth/core/providers"; import type { Profile } from "@auth/core/types"; import { redirect, type Handle } from "@sveltejs/kit"; import { sequence } from "@sveltejs/kit/hooks"; +import { announcements } from "./stores"; +import axios from "axios"; +import { Agent } from "https"; + +const agent = new Agent({ + family: 4 +}); const hasAuth = !env.AUTH_CLIENT_ID || @@ -51,3 +58,24 @@ export const handle: Handle = sequence( return result; } ); + +const updateMap = async () => { + try { + const res = await axios( + env.KUMA_URL, + { httpsAgent: agent, timeout: 10000 } + ); + + if (res.status === 200) { + announcements.set(res.data); + } else { + announcements.set({ error: true, message: "Error: " + res.status }); + } + } catch (err) { + announcements.set({ error: true, message: "Error: " + err }); + } +}; + +updateMap(); + +setInterval(updateMap, 30000); \ No newline at end of file diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index 13739b3..d61f5ad 100644 --- a/src/routes/+page.server.ts +++ b/src/routes/+page.server.ts @@ -1,11 +1,6 @@ import type { PageServerLoad } from "./$types"; -import axios from "axios"; -import { Agent } from "https"; -import { env } from "$env/dynamic/private"; - -const agent = new Agent({ - family: 4 -}); +import { announcements } from "../stores"; +import { get } from "svelte/store"; export const load = (async () => { const meta = { @@ -13,15 +8,8 @@ export const load = (async () => { description: "Open source development and hosted services." }; - try { - const res = await axios(env.KUMA_URL, { httpsAgent: agent }); - - if (res.status === 200) { - return { announcements: res.data, ...meta }; - } else { - return { error: true, message: "Error: " + res.status }; - } - } catch (err) { - return { error: true, message: "Error: " + err }; + return { + announcements: get(announcements), + ...meta } }) satisfies PageServerLoad; diff --git a/src/stores.ts b/src/stores.ts new file mode 100644 index 0000000..b97b945 --- /dev/null +++ b/src/stores.ts @@ -0,0 +1,3 @@ +import { writable, type Writable } from "svelte/store"; + +export const announcements: Writable<{}> = writable({});