Refactors loading function
* Initialize service component and handler in hooks * Dynamically watch the config instead of a reload on each page * Uses derived store for public server config * Moves service data in its own page data property * Reverts to a array to store group/services
This commit is contained in:
@@ -1,44 +1,25 @@
|
||||
import { dev } from '$app/environment';
|
||||
import { config, stripPrivateFields, type Config, type ServiceConfig } from '$lib/config';
|
||||
import { clientConfig, serverConfig } from '$lib/server/config';
|
||||
import { get } from 'svelte/store';
|
||||
import type { PageServerLoad } from './$types';
|
||||
import * as yml from 'js-yaml';
|
||||
import { readFile } from 'fs/promises';
|
||||
import { getService } from '$lib/services/services';
|
||||
import { getServiceHandler } from '$lib/services/services';
|
||||
|
||||
async function reloadConfig(): Promise<Config> {
|
||||
if (dev) {
|
||||
return config;
|
||||
}
|
||||
try {
|
||||
const dynamic = yml.load(await readFile('/dynamic/config.yml', 'utf8'));
|
||||
return { ...config, ...dynamic };
|
||||
} catch (err) {
|
||||
return config;
|
||||
}
|
||||
}
|
||||
export const load: PageServerLoad = ({ fetch }) => {
|
||||
const config = get(clientConfig);
|
||||
const serviceData: Array<Array<unknown>> = [];
|
||||
|
||||
export const load: PageServerLoad = async ({ fetch, depends }) => {
|
||||
depends('app:state');
|
||||
|
||||
const serverConfig = await reloadConfig();
|
||||
const clientConfig = stripPrivateFields(serverConfig);
|
||||
const groups: Array<string> = Object.entries(serverConfig.services).map(([k, v]) => k);
|
||||
for (const group of groups) {
|
||||
const serverGroup = serverConfig.services[group];
|
||||
const clientGroup = clientConfig.services[group];
|
||||
|
||||
const services = Object.entries(serverGroup.items).map(([k, v]) => k);
|
||||
|
||||
for (const service of services) {
|
||||
const serverService = serverGroup.items[service] as ServiceConfig;
|
||||
const clientService = clientGroup.items[service] as ServiceConfig;
|
||||
|
||||
const [handler, type] = getService(serverService.type || '');
|
||||
|
||||
clientService.type = type;
|
||||
Object.assign(clientService, handler({ fetch, config: serverService }));
|
||||
for (const [i, group] of get(serverConfig).services.entries()) {
|
||||
const groupData: Array<unknown> = [];
|
||||
serviceData.push(groupData);
|
||||
for (const [j, service] of group.items.entries()) {
|
||||
const handler = getServiceHandler(service.type || '');
|
||||
if (handler == undefined) {
|
||||
config.services[i].items[j].type = undefined;
|
||||
groupData.push(undefined);
|
||||
} else {
|
||||
groupData.push(handler({ fetch, config: service }));
|
||||
}
|
||||
}
|
||||
}
|
||||
clientConfig.timestamp = new Date();
|
||||
return { config: clientConfig, cool: true };
|
||||
|
||||
return { config, serviceData };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user