Attempts to a broken dynamic service loading.
This commit is contained in:
@@ -1,12 +1,39 @@
|
||||
import { dev } from '$app/environment';
|
||||
import { clientConfig, type Config } from '$lib/config';
|
||||
import { config, stripPrivateFields, type Config } from '$lib/config';
|
||||
import type { PageServerLoad } from './$types';
|
||||
import * as yml from 'js-yaml';
|
||||
import { readFile } from 'fs/promises';
|
||||
import { getService } from '$lib/services/service';
|
||||
|
||||
|
||||
export const load: PageServerLoad = ({locals}) => {
|
||||
async function reloadConfig(): Promise<Config> {
|
||||
if (dev) {
|
||||
return { config: clientConfig };
|
||||
return config;
|
||||
}
|
||||
try {
|
||||
const dynamic = yml.load(await readFile('/dynamic/config.yml', 'utf8'));
|
||||
return { ...config, ...dynamic };
|
||||
} catch (err) {
|
||||
return config;
|
||||
}
|
||||
}
|
||||
|
||||
return {config: (locals as Record<string,Config>).config || clientConfig};
|
||||
function zip<T>(a: Array<T>, b: Array<T>): Array<Array<T>> {
|
||||
const size = Math.min(a.length, b.length);
|
||||
return a.slice(0, size).map((v, i) => [v, b[i]]);
|
||||
}
|
||||
|
||||
export const load: PageServerLoad = async ({ fetch, depends }) => {
|
||||
depends('app:state');
|
||||
|
||||
const serverConfig = await reloadConfig();
|
||||
const clientConfig = stripPrivateFields(serverConfig);
|
||||
|
||||
for (const [serverGroup, clientGroup] of zip(serverConfig.services, clientConfig.services)) {
|
||||
for (const [serverService, clientService] of zip(serverGroup.items, clientGroup.items)) {
|
||||
const handler = getService(serverService.type || '');
|
||||
Object.assign(clientService, handler({ fetch, config: serverService }));
|
||||
}
|
||||
}
|
||||
clientConfig.timestamp = new Date();
|
||||
return { config: clientConfig };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user