Makes dynamic configuration work in production

If a static file is mounted in the Docker image, it will be read at
each page load and sent to the client.
This commit is contained in:
2023-08-11 11:00:18 +02:00
parent 99b91a7ebb
commit 8660345bf6
7 changed files with 47 additions and 24 deletions

View File

@@ -1,14 +0,0 @@
title: 'Hello World !!'
subtitle: 'I am a new pilot.'
services:
- title: '/Cloud'
subtitle: 'Private Cloud Utilities'
icon: 'fas fa-cloud'
items:
- title: 'NAS'
subtitle: 'Network Attached Storage'
icon: 'fas fa-hard-drive'
target: '_blank'
url: '/NAS'
keywords: 'cloud storage files'

1
src/config.yml Symbolic link
View File

@@ -0,0 +1 @@
../static/config.yml

View File

@@ -1,4 +1,4 @@
import configData from './config.yml';
import configData from '../config.yml';
export type Brand = {
logo?: string;

View File

@@ -0,0 +1,20 @@
import { dev } from '$app/environment';
import { config, type Config } from '$lib/config';
import type { PageServerLoad } from './$types';
import { readFileSync } from 'fs';
import * as yml from 'js-yaml';
export const load: PageServerLoad = () => {
if (dev) {
return { config: config };
}
try {
const dynamic = yml.load(readFileSync('/dynamic/config.yml', 'utf8'));
return { config: { ...config, ...dynamic } as Config };
} catch (err) {
console.debug("could not read '/dynamic/config.yml': " + err);
return { config: config };
}
};

View File

@@ -1,8 +1,10 @@
<script lang="ts">
import { config } from '../config';
import type { PageData } from './$types';
export let data: PageData;
</script>
<h1>{config.title}</h1>
{#if config.subtitle}
<h2>{config.subtitle}</h2>
<h1>{data.config.title}</h1>
{#if data.config.subtitle}
<h2>{data.config.subtitle}</h2>
{/if}