From 5d8be9d77cf8e9b055f76303f4b91abc58069e50 Mon Sep 17 00:00:00 2001 From: Neshura Date: Wed, 3 Jan 2024 20:39:53 +0100 Subject: [PATCH] Integrate with Uptime Kuma Socket.io API --- src/lib/components/ServiceCard.svelte | 23 +++++++++--- src/lib/types/uptime-kuma-types.ts | 9 +++++ src/routes/services/+page.server.ts | 38 ++++++++++++++++++++ src/routes/services/+page.svelte | 50 +++++++++++++++++++++++++-- 4 files changed, 113 insertions(+), 7 deletions(-) create mode 100644 src/lib/types/uptime-kuma-types.ts create mode 100644 src/routes/services/+page.server.ts diff --git a/src/lib/components/ServiceCard.svelte b/src/lib/components/ServiceCard.svelte index 97005bf..6e1997a 100644 --- a/src/lib/components/ServiceCard.svelte +++ b/src/lib/components/ServiceCard.svelte @@ -5,10 +5,12 @@ import { quintInOut } from 'svelte/easing'; import { slide } from 'svelte/transition'; import { IconType, type Service } from '$lib/types/data-types'; - import { onMount } from 'svelte'; import { Skeleton } from '$lib/components/ui/skeleton'; + import type {Heartbeat} from "$lib/types/uptime-kuma-types"; - let { service, icons } = $props<{ service: Service; icons: Array }>(); + let { service, icons, monitor } = $props<{ service: Service; icons: Array, monitor: Heartbeat }>(); + + let status = $state(4); let hover = $state({ title: false, @@ -30,6 +32,12 @@ } $effect(() => { + if (typeof monitor !== "undefined") { + if (monitor.monitorID == 9) { + console.log("Vaultwarden: ", monitor.status) + } + status = monitor.status; + } if (icons.length != 0) { const rootSplit = service.icon.split('/'); const root = rootSplit[rootSplit.length - 1]; @@ -48,7 +56,9 @@
-

- Loading +

+ {status == 0 ? 'Offline' : status == 1 ? 'Online' : status == 2 ? 'Pending' : status == 3 ? 'Maint.' : 'Loading'}

{service.desc}

diff --git a/src/lib/types/uptime-kuma-types.ts b/src/lib/types/uptime-kuma-types.ts new file mode 100644 index 0000000..1ff8908 --- /dev/null +++ b/src/lib/types/uptime-kuma-types.ts @@ -0,0 +1,9 @@ +export type Heartbeat = { + readonly monitorID: number; + readonly status: number; + readonly time: string; + readonly msg: string; + readonly ping: number; + readonly important: boolean; + readonly duration: number; +}; diff --git a/src/routes/services/+page.server.ts b/src/routes/services/+page.server.ts new file mode 100644 index 0000000..f990af2 --- /dev/null +++ b/src/routes/services/+page.server.ts @@ -0,0 +1,38 @@ +import { io } from 'socket.io-client'; +import * as fs from 'fs'; + +export async function load() { + const credFile = './credentials.json'; + const socket = io('https://status.neshweb.net/'); + + let credentials: { username: string; password: string }; + if (fs.existsSync(credFile)) { + let buf = fs.readFileSync(credFile); + credentials = JSON.parse(buf.toString()); + } else { + console.error('Credentials File does not exist, Socket.io connection will not work.'); + } + + console.log(credentials); + + let token = ''; + + socket.on('connect', () => { + socket.emit( + 'login', + { username: credentials.username, password: credentials.password, token: '' }, + (res) => { + token = res.token; + console.log('Token is:', token); + } + ); + }); + + while (token == '') { + await new Promise((resolve) => setTimeout(resolve, 10)); + } + + return { + token + }; +} diff --git a/src/routes/services/+page.svelte b/src/routes/services/+page.svelte index d3a484a..4934ec2 100644 --- a/src/routes/services/+page.svelte +++ b/src/routes/services/+page.svelte @@ -3,11 +3,57 @@