diff --git a/src/lib/server/serviceData.ts b/src/lib/server/serviceData.ts new file mode 100644 index 0000000..9c54465 --- /dev/null +++ b/src/lib/server/serviceData.ts @@ -0,0 +1,45 @@ +import type { Config, ServiceConfig } from '$lib/config'; +import type { + ServiceData, + ServiceHandler, + ServiceHandlerArgs, + ServiceStatus +} from '$lib/services/service'; +import { getServiceHandler } from '$lib/services/services'; +import { serverConfig } from './config'; + +export const serviceData: Array> = []; + +function wrapGeneric(handler: ServiceHandler): ServiceHandler { + return handler; +} + +function pollServices() { + const config: Config = serverConfig(); + + for (const [i, group] of config.services.entries()) { + if (serviceData[i] == undefined) { + serviceData[i] = []; + } + for (const [j, service] of group.items.entries()) { + let handler = getServiceHandler(service.type || ''); + if (handler == undefined) { + handler = wrapGeneric(() => { + return {}; + }); + } + + if (handler.constructor.name !== 'AsyncFucntion') { + handler = wrapGeneric(handler); + } + + (handler({ config: service }) as Promise).then((value) => { + serviceData[i][j] = value; + }); + } + } +} + +export function initServicePolling() { + setInterval(pollServices, 30000); +} diff --git a/src/lib/services/service.ts b/src/lib/services/service.ts index 163c0cf..037b8fe 100644 --- a/src/lib/services/service.ts +++ b/src/lib/services/service.ts @@ -1,10 +1,15 @@ import type { ServiceConfig } from '$lib/config'; -interface ServiceHandlerArgs { - fetch: typeof fetch; +export interface ServiceHandlerArgs { config: ServiceConfig; } -export type ServiceHandler = ( - input: ServiceHandlerArgs -) => Record | Promise>; +export type ServiceStatus = 'online' | 'offline'; + +export interface ServiceData { + status?: ServiceStatus; + + [x: string]: unknown; +} + +export type ServiceHandler = (input: ServiceHandlerArgs) => ServiceData | Promise;