Skip to content

Commit

Permalink
Merge pull request #309 from matt8707/headers
Browse files Browse the repository at this point in the history
Get hassUrl from headers
  • Loading branch information
matt8707 authored Jan 28, 2024
2 parents 57ed153 + e2862d6 commit ca62859
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
2 changes: 0 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@ function customRouter(req) {
// ingress
if (source && forwardedProto && forwardedHost) {
target = `${forwardedProto}://${forwardedHost}`;
process.env.HASS_URL = target;
}

// exposed port
else if (host && EXPOSED_PORT && HASS_PORT) {
target = `http://${host.replace(EXPOSED_PORT, HASS_PORT)}`;
process.env.HASS_URL = target;
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/lib/Socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ERR_INVALID_HTTPS_TO_HTTP
} from 'home-assistant-js-websocket';
import type { SaveTokensFunc } from 'home-assistant-js-websocket';
import { states, connection, config, authCallback, connected } from '$lib/Stores';
import { states, connection, config, connected } from '$lib/Stores';

export const options = {
hassUrl: undefined as string | undefined,
Expand Down Expand Up @@ -74,7 +74,6 @@ export async function authentication(options: { hassUrl?: string }) {

// clear auth query string
if (location.search.includes('auth_callback=1')) {
authCallback.set(true);
history.replaceState(null, '', location.pathname);
}
} catch (_error) {
Expand Down
5 changes: 0 additions & 5 deletions src/lib/Stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export const config = writable<HassConfig>();
export const states = writable<HassEntities>();

export const connected = writable<boolean>();
export const authCallback = writable(false);

// user
export const configuration = writable<Configuration>();
Expand All @@ -20,10 +19,7 @@ export const onStates = readable([
'active',
'auto',
'cool',
'cooling',
'dry',
'drying',
'fan',
'fan_only',
'heat',
'heat_cool',
Expand All @@ -32,7 +28,6 @@ export const onStates = readable([
'on',
'open',
'playing',
'preheating',
'unlocked',
// vacuum
'cleaning',
Expand Down
41 changes: 39 additions & 2 deletions src/routes/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async function loadFile(file: string) {
/**
* Server load function
*/
export async function load(): Promise<{
export async function load({ request }): Promise<{
configuration: Configuration;
dashboard: Dashboard;
theme: any;
Expand All @@ -42,7 +42,44 @@ export async function load(): Promise<{
loadFile('./data/dashboard.yaml')
]);

configuration.hassUrl = process.env.HASS_URL;
// determine hassUrl
const ADDON = process.env.ADDON === 'true';
const HASS_PORT = process.env.HASS_PORT;
const EXPOSED_PORT = process.env.EXPOSED_PORT;

let hassUrl = process.env.HASS_URL;

if (ADDON) {
// headers
const source = request.headers.get('x-hass-source');
const forwardedProto = request.headers.get('x-forwarded-proto');
const forwardedHost = request.headers.get('x-forwarded-host');
const host = request.headers.get('host');

console.log({
source: source,
forwardedProto: forwardedProto,
forwardedHost: forwardedHost,
host: host
});

// ingress
if (source && forwardedProto && forwardedHost) {
hassUrl = `${forwardedProto}://${forwardedHost}`;
}

// exposed port
else if (host && EXPOSED_PORT && HASS_PORT) {
hassUrl = `http://${host.replace(EXPOSED_PORT, HASS_PORT)}`;
}
}

// hassUrl should be defined now
if (!hassUrl) {
throw new Error('hassUrl could not be determined');
}

configuration.hassUrl = hassUrl;

// initialize keys if missing
dashboard.views = dashboard.views || [];
Expand Down
6 changes: 4 additions & 2 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
filterDashboard,
disableMenuButton,
clickOriginatedFromMenu,
authCallback
connection
} from '$lib/Stores';
import { authentication, options } from '$lib/Socket';
import { onDestroy, onMount } from 'svelte';
Expand Down Expand Up @@ -155,6 +155,8 @@
$drawerSearch = undefined;
}
}
$: console.log('DEBUG:', $configuration?.hassUrl);
</script>

<svelte:window on:keydown={handleKeydown} />
Expand All @@ -180,7 +182,7 @@
{#await import('$lib/Main/Index.svelte') then Main}
<svelte:component this={Main.default} {view} />
{/await}
{:else if $authCallback || options?.hassUrl}
{:else if $connection}
{#await import('$lib/Main/Intro.svelte') then Intro}
<svelte:component this={Intro.default} {data} />
{/await}
Expand Down

0 comments on commit ca62859

Please sign in to comment.