Skip to content

Commit

Permalink
Use --proxyUrl to allow for more control over the scheme, host, and p…
Browse files Browse the repository at this point in the history
…ort. (#352)

Use case, per @kaladay:

This allows a workstation with a static IP to
1. serve as the proxy and
2. host the corresponding bundle for a third party, e.g. a demo machine
   in another room, a colleague in another office

Co-authored-by: Kevin Day <[email protected]>
  • Loading branch information
zburke and kaladay committed Sep 13, 2024
1 parent 218ced7 commit acbd72e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
3 changes: 2 additions & 1 deletion doc/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,7 @@ Option | Description | Type | Notes
`--stripesConfig` | Stripes config JSON | string | supports stdin
`--tenant` | Specify a tenant ID | string |
`--startProxy` | Start a local proxy server between the platform and okapi | boolean | default: false
`--proxyHost` | Scheme and host name for the proxy server | string | default: http://localhost
`--proxyPort` | Port number for the proxy server | number | default: 3010

Examples:
Expand All @@ -1150,7 +1151,7 @@ $ stripes serve --existing-build output
```
Serve a platform with a local proxy server that points to a remote okapi server:
```
$ stripes serve --startProxy --proxyPort 3010 --okapi http://some-okapi-server.folio.org
$ stripes serve --startProxy --proxyHost http://localhost --proxyPort 3010 --okapi http://some-okapi-server.folio.org
```
Serve an app (in app context) with a mock backend server":
```
Expand Down
6 changes: 6 additions & 0 deletions lib/commands/common-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ module.exports.serverOptions = {
default: false,
group: 'Server Options:',
},
proxyHost: {
type: 'string',
describe: 'Proxy scheme and host',
default: 'http://localhost',
group: 'Server Options:',
},
proxyPort: {
type: 'number',
describe: 'Proxy server port',
Expand Down
7 changes: 3 additions & 4 deletions lib/commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ const serveBuildOptions = Object.assign({}, buildOptions);
delete serveBuildOptions.publicPath;

function replaceArgvOkapiWithProxyURL(argv) {
const proxyURL = `http://localhost:${argv.proxyPort}`;
argv.okapi = proxyURL;
argv.okapi = `${argv.proxyHost}:${argv.proxyPort}`;

if (argv.stripesConfig?.okapi) {
argv.stripesConfig.okapi.url = proxyURL;
argv.stripesConfig.okapi.url = argv.okapi;
}
}

Expand Down Expand Up @@ -52,7 +51,7 @@ function serveCommand(argv) {

if (argv.startProxy) {
console.info('starting proxy');
childProcess.fork(path.resolve(context.cliRoot, './lib/run-proxy.js'), [argv.okapi, argv.proxyPort, argv.port]);
childProcess.fork(path.resolve(context.cliRoot, './lib/run-proxy.js'), [argv.okapi, argv.port, argv.proxyHost, argv.proxyPort]);
// if we're using a proxy server - we need to pass the proxy host as okapi to Stripes platform
replaceArgvOkapiWithProxyURL(argv);
}
Expand Down
7 changes: 4 additions & 3 deletions lib/run-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();

const OKAPI = process.argv[2];
const PROXY_PORT = process.argv[3];
const PORT = process.argv[4];
const PORT = process.argv[3];
const PROXY_HOST = process.argv[4];
const PROXY_PORT = process.argv[5];

app.use(
'/',
Expand All @@ -14,7 +15,7 @@ app.use(
changeOrigin: true,
on: {
proxyRes: (proxyRes) => {
proxyRes.headers['Access-Control-Allow-Origin'] = `http://localhost:${PORT}`;
proxyRes.headers['Access-Control-Allow-Origin'] = `${PROXY_HOST}:${PORT}`;
proxyRes.headers['Access-Control-Allow-Credentials'] = 'true';
},
},
Expand Down

0 comments on commit acbd72e

Please sign in to comment.