Skip to content

Commit

Permalink
Update proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
matt8707 committed Apr 18, 2024
1 parent d8ec18b commit 4d91223
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 63 deletions.
58 changes: 26 additions & 32 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,26 @@ import express from 'express';
import { createProxyMiddleware } from 'http-proxy-middleware';
import dotenv from 'dotenv';

dotenv.config();

let logTarget;

const app = express();

// environment
dotenv.config();
const ADDON = process.env.ADDON === 'true';
const PORT = process.env.PORT;
const HASS_PORT = process.env.HASS_PORT;
const EXPOSED_PORT = process.env.EXPOSED_PORT;
const { PORT, HASS_PORT, EXPOSED_PORT } = process.env;

// dynamically set target for proxy middleware
function customRouter(req) {
// dynamically set target
const entryMiddleware = async (req, res, next) => {
// default
let target = process.env.HASS_URL;

if (ADDON) {
// headers
const source = req.headers['x-hass-source'];
const forwardedProto = req.headers['x-forwarded-proto'];
const forwardedHost = req.headers['x-forwarded-host'];
const host = req.headers['host'];
const {
'x-hass-source': source,
'x-forwarded-proto': forwardedProto,
'x-forwarded-host': forwardedHost,
host
} = req.headers;

// ingress
if (source && forwardedProto && forwardedHost) {
Expand All @@ -33,7 +31,8 @@ function customRouter(req) {

// exposed port
else if (host && EXPOSED_PORT && HASS_PORT) {
target = `http://${host.replace(EXPOSED_PORT, HASS_PORT)}`;
const proto = req.secure ? 'https' : 'http';
target = `${proto}://${host.replace(EXPOSED_PORT, HASS_PORT)}`;
}
}

Expand All @@ -42,27 +41,22 @@ function customRouter(req) {
throw new Error('Proxy target could not be determined');
}

// log actual target instead of placeholder `...` because
// the router gets invoked before headers are processed
if (!logTarget) {
logTarget = `... -> ${target}`;
console.log(logTarget);
}

return target;
}
// add header for +page.server.ts
req.headers['X-Proxy-Target'] = target;
req.target = target;
next();
};

// production proxy
app.use(
['/local/', '/api/'],
createProxyMiddleware({
target: '...',
router: customRouter,
changeOrigin: true
})
);
const proxy = createProxyMiddleware({
pathFilter: ['/local/', '/api/'],
router: (req) => req.target,
changeOrigin: true
});

app.use(entryMiddleware, proxy);

// let sveltekit handle everything else
// let SvelteKit handle everything else, including serving prerendered pages and static assets
app.use(handler);

app.listen(PORT, () => {
Expand Down
33 changes: 2 additions & 31 deletions src/routes/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,8 @@ export async function load({ request }): Promise<{
loadFile('./data/dashboard.yaml')
]);

// 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');

// 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;
// hassUrl from env or server.js
configuration.hassUrl = process.env.HASS_URL || request.headers.get('X-Proxy-Target');

// initialize keys if missing
dashboard.views = dashboard.views || [];
Expand Down

0 comments on commit 4d91223

Please sign in to comment.