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

Use --proxyUrl to allow control over the scheme, host, and port #352

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 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
Loading