From cb7143ed3d998978a46a70d701a9332af07d69ec Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 16 Oct 2024 08:15:11 +0100 Subject: [PATCH] should be all good now (and added api status notifications) --- .../backend/src/controller/app.controller.ts | 7 +++++++ projects/common/src/utils/api-utils.ts | 6 +++++- projects/common/src/utils/utils.ts | 16 ++++++++++++++++ projects/website/src/app/(pages)/page.tsx | 14 ++++++++------ .../website/src/components/api/api-health.tsx | 1 + 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/projects/backend/src/controller/app.controller.ts b/projects/backend/src/controller/app.controller.ts index 94c4932..174253d 100644 --- a/projects/backend/src/controller/app.controller.ts +++ b/projects/backend/src/controller/app.controller.ts @@ -12,6 +12,13 @@ export default class AppController { }; } + @Get("/health") + public async getHealth() { + return { + status: "OK", + }; + } + @Get("/statistics") public async getStatistics() { return await AppService.getAppStatistics(); diff --git a/projects/common/src/utils/api-utils.ts b/projects/common/src/utils/api-utils.ts index 8dc00cc..700d455 100644 --- a/projects/common/src/utils/api-utils.ts +++ b/projects/common/src/utils/api-utils.ts @@ -11,7 +11,11 @@ type ApiHealth = { */ export async function getApiHealth(url: string): Promise { try { - await ky.get(url); + await ky + .get(url, { + cache: "no-cache", + }) + .json(); return { online: true, }; diff --git a/projects/common/src/utils/utils.ts b/projects/common/src/utils/utils.ts index 423ad75..fba847c 100644 --- a/projects/common/src/utils/utils.ts +++ b/projects/common/src/utils/utils.ts @@ -1,3 +1,5 @@ +import ky from "ky"; + /** * Checks if we're in production */ @@ -24,3 +26,17 @@ export function delay(ms: number) { export function getPageFromRank(rank: number, itemsPerPage: number) { return Math.floor(rank / itemsPerPage) + 1; } + +/** + * Fetches data from the given url. + * + * @param url the url to fetch + */ +export async function kyFetch(url: string): Promise { + try { + return await ky.get(url).json(); + } catch (error) { + console.error(`Error fetching data from ${url}:`, error); + return undefined; + } +} diff --git a/projects/website/src/app/(pages)/page.tsx b/projects/website/src/app/(pages)/page.tsx index 5f50844..d53bea8 100644 --- a/projects/website/src/app/(pages)/page.tsx +++ b/projects/website/src/app/(pages)/page.tsx @@ -1,14 +1,14 @@ import { Button } from "@/components/ui/button"; import Link from "next/link"; -import ky from "ky"; import { config } from "../../../config"; import { AppStatistics } from "@ssr/common/types/backend/app-statistics"; import Statistic from "@/components/home/statistic"; +import { kyFetch } from "@ssr/common/utils/utils"; export const dynamic = "force-dynamic"; // Always generate the page on load export default async function HomePage() { - const statistics = await ky.get(config.siteApi + "/statistics").json(); + const statistics = await kyFetch(config.siteApi + "/statistics"); return (
@@ -21,10 +21,12 @@ export default async function HomePage() {

ScoreSaber Reloaded is a website that allows you to track your ScoreSaber data over time.

-
-

Site Statistics

- -
+ {statistics && ( +
+

Site Statistics

+ +
+ )}
diff --git a/projects/website/src/components/api/api-health.tsx b/projects/website/src/components/api/api-health.tsx index d820228..87eb0fb 100644 --- a/projects/website/src/components/api/api-health.tsx +++ b/projects/website/src/components/api/api-health.tsx @@ -34,6 +34,7 @@ export function ApiHealth() { title: `The API is now ${online ? "Online" : "Offline"}!`, description: online ? "The API has recovered connectivity." : "The API has lost connectivity.", variant: online ? "success" : "destructive", + duration: 10_000, // 10 seconds }); }