diff --git a/.env.example b/.env.example index 61f645c5..3a5fe342 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,4 @@ +PUBLIC_UI_VERSION=dev PUBLIC_URI=http://localhost:5173 # use local frontend with local backend API_URI=http://localhost:3030 # server reachable diff --git a/.github/workflows/dockercicd.yml b/.github/workflows/dockercicd.yml index 9707ed73..5b02457f 100644 --- a/.github/workflows/dockercicd.yml +++ b/.github/workflows/dockercicd.yml @@ -15,6 +15,8 @@ jobs: permissions: contents: read packages: write + env: + PUBLIC_UI_VERSION: ${{ github.ref }} steps: - uses: actions/checkout@v4 diff --git a/.github/workflows/testingci.yml b/.github/workflows/testingci.yml index 88be8a50..0a95c455 100644 --- a/.github/workflows/testingci.yml +++ b/.github/workflows/testingci.yml @@ -9,6 +9,8 @@ jobs: runs-on: ubuntu-latest permissions: contents: read + env: + PUBLIC_UI_VERSION: ${{ github.sha }} steps: - uses: actions/checkout@v4 diff --git a/src/lib/components/footer/main.svelte b/src/lib/components/footer/main.svelte index 4e39d690..31cd6572 100644 --- a/src/lib/components/footer/main.svelte +++ b/src/lib/components/footer/main.svelte @@ -1,10 +1,24 @@ + + diff --git a/src/lib/functions/api/fetchversion.js b/src/lib/functions/api/fetchversion.js new file mode 100644 index 00000000..a809b8ea --- /dev/null +++ b/src/lib/functions/api/fetchversion.js @@ -0,0 +1,37 @@ +import { error } from '@sveltejs/kit'; +import { createApiUrl } from '$lib/functions/api/createurl.js'; + +/** + * @param {typeof fetch} [fetcher] + * @returns {Promise} + */ +export async function fetchVersion(fetcher = fetch) { + /** @type {URL} */ + let apiUrl; + try { + apiUrl = createApiUrl('versionz'); + } catch (/** @type {any} */ err) { + // Internal Server Error + throw error(500, `Failed to create API URL: ${err.message}`); + } + + /** @type {Response} */ + let response; + try { + response = await fetcher(apiUrl, { + method: 'GET', // POST doesn't cache on CDN + headers: { + Accept: 'text/plain', + 'Accept-Encoding': 'gzip, deflate, br' + } + }); + } catch (/** @type {any} */ err) { + // Bad Gateway + throw error(502, `Failed to fetch version: ${err.message}`); + } + + /** @type {string} */ + const version = await response.text(); + + return version; +} diff --git a/src/routes/+layout.js b/src/routes/+layout.js new file mode 100644 index 00000000..431aeaec --- /dev/null +++ b/src/routes/+layout.js @@ -0,0 +1,11 @@ +import { PUBLIC_UI_VERSION } from '$env/static/public'; +import { fetchVersion } from '$lib/functions/api/fetchversion'; + +/** @type {import('./$types').LayoutLoad} */ +export async function load({ fetch }) { + const apiVersion = await fetchVersion(fetch); + return { + uiVersion: PUBLIC_UI_VERSION ?? 'dev', + apiVersion: apiVersion + }; +} diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index ae311a17..256c46dc 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -3,6 +3,10 @@ import Preconnect from '$lib/components/preconnect/main.svelte'; import ThemeToggle from '$lib/components/themetoggle/main.svelte'; import Footer from '$lib/components/footer/main.svelte'; + + let { data } = $props(); + const uiVersion = $derived(data.uiVersion); + const apiVersion = $derived(data.apiVersion); @@ -15,4 +19,4 @@ -