-
-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reverse proxy dynamic subpath (#540)
* Removed unused template * Use EJS for index page in dev. * Fix webpack; move default config * Dynamically render index * Built production works * Mostly working * Add some documentation * Fix up documentation * Link to subpath docs from main README * Add unit tests * Add missing (?) ejs package * TIL: Socket Namespaces * Update docs/subpath.md Co-authored-by: Chris Nesbitt-Smith <[email protected]> * Update src/apis/ConfigApis.js Co-authored-by: Chris Nesbitt-Smith <[email protected]> * Update docs/subpath.md Co-authored-by: Chris Nesbitt-Smith <[email protected]> * Add reverse proxy setup to Readme TOC * Remove trailing white line * Remove trailing semicolon from webpack config * Move require to the requirements block * Fix MD language for NGinx * Remove `favicons` comment * Add 'proxyquire' as a dev dependency * Ensure base path has always a trailing slash * Fix development assets layout * Add full practical example of reverse proxy * Proxy to nodejs service for development of UI * Fix missing mapping * Linter fixes -- need to start auto running it! * Update README.md Co-authored-by: Chris Nesbitt-Smith <[email protected]> * Update build/webpack.dev.conf.js Co-authored-by: Chris Nesbitt-Smith <[email protected]> * Update src/App.vue Co-authored-by: Chris Nesbitt-Smith <[email protected]> * Update test/lib/renderIndex.test.js Co-authored-by: Chris Nesbitt-Smith <[email protected]> * Make proxy URL configurable; UT fixes Co-authored-by: Daniel Lando <[email protected]> Co-authored-by: Chris Nesbitt-Smith <[email protected]>
- Loading branch information
1 parent
47c3abf
commit b10e8c9
Showing
20 changed files
with
600 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
// config/app.js | ||
module.exports = { | ||
title: 'ZWave To MQTT', | ||
storeDir: 'store', | ||
base: '/', | ||
port: 8091 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const appConfig = require('./app') | ||
|
||
appConfig.base = appConfig.base && appConfig.base.replace(/\/?$/, '/') | ||
|
||
const defaultConfig = { | ||
base: '/', | ||
title: 'ZWave To MQTT' | ||
} | ||
|
||
module.exports = { | ||
...defaultConfig, | ||
...appConfig | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# ZWave To MQTT Behind a Reverse Proxy | ||
|
||
There are two ways to enable ZWave To MQTT to sit behing a proxy that uses | ||
subpaths to serve the pages and services. | ||
|
||
You can use a header to signal where the external path is or you can configure | ||
the base path. In both cases these are dynamic configurations, so you can deploy | ||
without having to build again the frontend. | ||
|
||
## Using an HTTP header | ||
|
||
You can pass the external path by setting the `X-External-Path` header, for example | ||
suppose you had the following `nginx` configuration: | ||
|
||
```nginx | ||
map $http_upgrade $connection_upgrade { | ||
default upgrade; | ||
'' close; | ||
} | ||
server { | ||
listen 9000 default_server; | ||
listen [::]:9000 default_server; | ||
location /hassio/ingress/ { | ||
proxy_pass http://localhost:8091/; | ||
proxy_set_header X-External-Path /hassio/ingress; | ||
proxy_http_version 1.1; | ||
proxy_set_header Upgrade $http_upgrade; | ||
proxy_set_header Connection $connection_upgrade; | ||
} | ||
} | ||
``` | ||
|
||
This will tell the application to serve the application and relevant elements under | ||
`/some/deep/map`. | ||
|
||
In case you are using the [ingress of Home Assistant](https://www.home-assistant.io/blog/2019/04/15/hassio-ingress/) you will want to | ||
pick up the `X-Ingress-Path;` and map it, something along | ||
these lines: | ||
|
||
```nginx | ||
proxy_set_header X-External-Path $http_x_ingress_path; | ||
``` | ||
|
||
## Using the configuration | ||
|
||
You can simply change the `config/app.js` and set `base` to whatever is | ||
the subpath you will be serving this from. | ||
|
||
As an example, if your proxy is placing the app behind `/zwave/` your configuration | ||
would look like: | ||
|
||
```javascript | ||
module.exports = { | ||
title: 'ZWave to MQTT', | ||
storeDir: 'store', | ||
base: '/zwave/', | ||
port: 8091 | ||
} | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
const reqlib = require('app-root-path').require | ||
|
||
const webConfig = reqlib('/config/webConfig') | ||
|
||
function findFiles (folder, ext) { | ||
const folderPath = path.join(__dirname, '..', 'dist', folder) | ||
const folderFiles = fs.readdirSync(folderPath) | ||
return folderFiles | ||
.filter(function (file) { | ||
return path.extname(file).toLowerCase() === `.${ext.toLowerCase()}` | ||
}) | ||
.map(function (file) { | ||
return path.join(folder, file) | ||
}) | ||
} | ||
|
||
let cssFiles | ||
let jsFiles | ||
|
||
function basePath (config, headers) { | ||
return (headers['x-external-path'] || config.base).replace(/\/?$/, '/') | ||
} | ||
|
||
module.exports = function (req, res) { | ||
cssFiles = cssFiles || findFiles(path.join('static', 'css'), 'css') | ||
jsFiles = jsFiles || findFiles(path.join('static', 'js'), 'js') | ||
res.render('index.ejs', { | ||
config: { | ||
...webConfig, | ||
base: basePath(webConfig, req.headers) | ||
}, | ||
cssFiles, | ||
jsFiles | ||
}) | ||
} |
Oops, something went wrong.