Refactors services with defaultConfig and polled data.
Much more simpler code base. Each service requires a poll functioon to poll its status. If type is defined, and the registered '+service.ts' exports a poll ServicePoller, it will be used. Otherwise, a generic poll mechanism is used.
This commit is contained in:
@@ -1,21 +1,45 @@
|
||||
import type { ServiceHandler } from './service';
|
||||
import type { ServiceConfig } from '$lib/config';
|
||||
import type { ServiceData, ServicePoller } from './service';
|
||||
|
||||
const services: Record<string, ServiceHandler> = {};
|
||||
|
||||
const components: Record<string, any> = {};
|
||||
|
||||
function registerService(type: string, handler: ServiceHandler) {
|
||||
services[type] = handler;
|
||||
interface ServiceRecord {
|
||||
poll: ServicePoller;
|
||||
config: Partial<ServiceConfig>;
|
||||
}
|
||||
|
||||
const services: Record<string, ServiceRecord> = {};
|
||||
|
||||
//eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const components: Record<string, any> = {};
|
||||
|
||||
async function pollURL(config: ServiceConfig): Promise<ServiceData> {
|
||||
try {
|
||||
const resp = await fetch(config.url);
|
||||
return { status: resp.ok ? 'online' : 'offline' };
|
||||
} catch (error) {
|
||||
console.warn(`could not poll '${config.url}': ` + error);
|
||||
return { status: 'offline' };
|
||||
}
|
||||
}
|
||||
|
||||
function registerService(type: string, service: Partial<ServiceRecord>) {
|
||||
const record: ServiceRecord = {
|
||||
poll: service.poll || pollURL,
|
||||
config: service.config || {}
|
||||
};
|
||||
|
||||
services[type] = record;
|
||||
}
|
||||
|
||||
//eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
function registerComponent(type: string, component: any) {
|
||||
components[type] = component;
|
||||
}
|
||||
|
||||
export function getServiceHandler(type: string): ServiceHandler | undefined {
|
||||
return services[type];
|
||||
export function getServiceRecord(type: string): ServiceRecord {
|
||||
return services[type] || { poll: pollURL, config: {} };
|
||||
}
|
||||
|
||||
//eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export function getServiceComponent(type: string): any {
|
||||
return components[type];
|
||||
}
|
||||
@@ -25,12 +49,13 @@ export async function initServices() {
|
||||
|
||||
for (const [modulePath, load] of Object.entries(services)) {
|
||||
try {
|
||||
const { handle } = (await load()) as any;
|
||||
if (handle == undefined) {
|
||||
throw new Error(`${modulePath} does not export 'handle'`);
|
||||
//eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const { poll, config } = (await load()) as any;
|
||||
if (poll == undefined && config == undefined) {
|
||||
throw new Error(`${modulePath} does not export 'poll' or 'config'`);
|
||||
}
|
||||
const typeName = modulePath.slice(18, -12);
|
||||
registerService(typeName, handle);
|
||||
registerService(typeName, { poll, config });
|
||||
} catch (err) {
|
||||
console.error(`Could not load service definition from '${modulePath}': ${err}`);
|
||||
}
|
||||
@@ -42,6 +67,7 @@ export async function initComponents() {
|
||||
|
||||
for (const [componentPath, load] of Object.entries(services)) {
|
||||
try {
|
||||
//eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const module = (await load()) as any;
|
||||
|
||||
if (module == undefined) {
|
||||
|
||||
Reference in New Issue
Block a user