WIP
This commit is contained in:
45
src/lib/server/serviceData.ts
Normal file
45
src/lib/server/serviceData.ts
Normal file
@@ -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<Array<unknown>> = [];
|
||||||
|
|
||||||
|
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<ServiceData>).then((value) => {
|
||||||
|
serviceData[i][j] = value;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function initServicePolling() {
|
||||||
|
setInterval(pollServices, 30000);
|
||||||
|
}
|
||||||
@@ -1,10 +1,15 @@
|
|||||||
import type { ServiceConfig } from '$lib/config';
|
import type { ServiceConfig } from '$lib/config';
|
||||||
|
|
||||||
interface ServiceHandlerArgs {
|
export interface ServiceHandlerArgs {
|
||||||
fetch: typeof fetch;
|
|
||||||
config: ServiceConfig;
|
config: ServiceConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ServiceHandler = (
|
export type ServiceStatus = 'online' | 'offline';
|
||||||
input: ServiceHandlerArgs
|
|
||||||
) => Record<string, unknown> | Promise<Record<string, unknown>>;
|
export interface ServiceData {
|
||||||
|
status?: ServiceStatus;
|
||||||
|
|
||||||
|
[x: string]: unknown;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ServiceHandler = (input: ServiceHandlerArgs) => ServiceData | Promise<ServiceData>;
|
||||||
|
|||||||
Reference in New Issue
Block a user