diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-12-09 03:57:12 +0100 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-12-09 03:57:12 +0100 |
| commit | 4dbef3fcd7a14437d55c555cf10d50de8e50d7d1 (patch) | |
| tree | 632589ecfcfb4dfddeafb71d0077257584b5e7ec /code/app/src/routes/(main)/+layout.server.ts | |
| parent | 914c75e0ceeb3e11ddd55e94bb461c26b0db5b7a (diff) | |
| download | greatoffice-4dbef3fcd7a14437d55c555cf10d50de8e50d7d1.tar.xz greatoffice-4dbef3fcd7a14437d55c555cf10d50de8e50d7d1.zip | |
feat: Move everything out of $lib
Diffstat (limited to 'code/app/src/routes/(main)/+layout.server.ts')
| -rw-r--r-- | code/app/src/routes/(main)/+layout.server.ts | 35 |
1 files changed, 18 insertions, 17 deletions
diff --git a/code/app/src/routes/(main)/+layout.server.ts b/code/app/src/routes/(main)/+layout.server.ts index cd41734..086d1c0 100644 --- a/code/app/src/routes/(main)/+layout.server.ts +++ b/code/app/src/routes/(main)/+layout.server.ts @@ -1,27 +1,27 @@ -import { api_base, CookieNames } from "$lib/configuration"; -import { log_debug, log_error } from "$lib/logger"; -import { error, redirect } from "@sveltejs/kit"; -import { Temporal } from "temporal-polyfill"; -import type { LayoutServerLoad } from "./$types"; +import {api_base, CookieNames} from "$configuration"; +import {log_debug, log_error} from "$help/logger"; +import {error, redirect} from "@sveltejs/kit"; +import {Temporal} from "temporal-polyfill"; +import type {LayoutServerLoad} from "./$types"; -export const load: LayoutServerLoad = async ({ route, cookies, locals }) => { +export const load: LayoutServerLoad = async ({route, cookies, locals}) => { const isBaseRoute = route.id === "/(main)"; const isPublicRoute = (route.id?.startsWith("/(main)/(public)") || isBaseRoute) ?? true; const sessionIsValid = (await cached_result<Response>("sessionCheck", 120, () => fetch(api_base("_/valid-session"), { headers: { - Cookie: CookieNames.session + "=" + cookies.get(CookieNames.session) - } + Cookie: CookieNames.session + "=" + cookies.get(CookieNames.session), + }, }).catch((e) => { log_error(e); throw error(503, { - message: "We are experiencing a service disruption! Have patience while we resolve the issue." - }) + message: "We are experiencing a service disruption! Have patience while we resolve the issue.", + }); }))).ok; log_debug("Base Layout loaded", { sessionIsValid, isPublicRoute, - routeId: route.id + routeId: route.id, }); if (sessionIsValid && isPublicRoute) { @@ -31,17 +31,18 @@ export const load: LayoutServerLoad = async ({ route, cookies, locals }) => { } return { - locale: locals.locale - } -} + locale: locals.locale, + }; +}; let resultCache = {}; + async function cached_result<T>(key: string, staleAfterSeconds: number, code: any) { if (!resultCache[key]) { resultCache[key] = { l: 0, - c: undefined as T - } + c: undefined as T, + }; } const staleEpoch = ((resultCache[key]?.l ?? 0) + staleAfterSeconds); const isStale = staleEpoch < Temporal.Now.instant().epochSeconds; @@ -54,7 +55,7 @@ async function cached_result<T>(key: string, staleAfterSeconds: number, code: an cacheKey: key, isStale, cache: resultCache[key], - staleEpoch + staleEpoch, }); return resultCache[key].c as T; |
