Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rest api access when using script/develop_and_serve #23144

Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "Home Assistant Frontend",
"build": {
"dockerfile": "Dockerfile",
"context": ".."
"context": "..",
"options": [ "--add-host=host.docker.internal:host-gateway" ]
MindFreeze marked this conversation as resolved.
Show resolved Hide resolved
},
"appPort": "8124:8123",
"postCreateCommand": "sudo apt update && sudo apt upgrade -y && sudo apt install -y libpcap-dev",
Expand Down
20 changes: 14 additions & 6 deletions script/develop_and_serve
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/usr/bin/env bash
#
# This script can be used to develop and test the frontend without having to
# link the build in a running core instance through the frontend/development_repo setting.
Expand Down Expand Up @@ -60,11 +60,19 @@ else
fi
echo Core is used from ${coreUrl}

if [ ! -d "./hassio/build" ]; then
echo Building hassio
# the hassio rest app doesn't need the HASS_URL override,
# it will use the url from the current login session
hassio/script/build_hassio
fi

# build the frontend so it connects to the passed core
HASS_URL="$coreUrl" ./script/develop &

# serve the frontend
yarn dlx serve -l $frontendPort ./hass_frontend -s &

# keep the script running while serving
wait
if [ -n "$DEVCONTAINER" ]; then
nodeCoreUrl="${coreUrl/localhost/host.docker.internal}"
else
nodeCoreUrl="$coreUrl"
fi
yarn exec node ./script/serve.js "$nodeCoreUrl" $frontendPort
34 changes: 34 additions & 0 deletions script/serve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import express from "express";
import path from "path";
import { fileURLToPath } from "url";

const coreUrl = process.argv[2];
const port = parseInt(process.argv[3] ?? "8123");

const repoDir = path.join(fileURLToPath(import.meta.url), "../..");

import { createProxyMiddleware } from "http-proxy-middleware";

const coreProxy = createProxyMiddleware({
target: coreUrl,
changeOrigin: true,
});

const app = express();
app.use("/", express.static(path.join(repoDir, "hass_frontend")));
app.use("/api/hassio/app", express.static(path.join(repoDir, "hassio/build")));
app.use("/api", coreProxy);
app.get("*", (req, res) => {
res.sendFile(path.join(repoDir, "hass_frontend/index.html"));
});
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
Dismissed Show dismissed Hide dismissed

var server = app.listen(port, () => {
console.log(
`Running at http://localhost:${port}, connected to core on ${coreUrl}`
);
});

process.on("SIGINT", function () {
console.log("Shutting down file server");
server.close();
});
Loading