Skip to content

Commit

Permalink
feat(proxy): add middleware registry
Browse files Browse the repository at this point in the history
  • Loading branch information
redallen committed Jul 22, 2021
1 parent c900960 commit 8498a9a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
4 changes: 2 additions & 2 deletions packages/config-utils/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ module.exports = ({
publicPath,
proxyVerbose,
useCloud = false,
target = ''
target = '',
registry = []
}) => {
const proxy = [];
const registry = [];
const majorEnv = env.split('-')[0];
if (target === '') {
target += 'https://';
Expand Down
50 changes: 38 additions & 12 deletions packages/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
- [Removed features with webpack 5](#removed-features-with-webpack-5)
- [useProxy](#useproxy)
- [Attributes](#attributes)
- [localChrome](#localchrome)
- [Custom routes](#custom-routes)
- [routes](#routes)
- [routesPath](#routespath)
- [localChrome](#localchrome)
- [registry](#registry)
- [Custom routes](#custom-routes)
- [routes](#routes)
- [routesPath](#routespath)
- [Custom proxy settings](#custom-proxy-settings)
- [standalone](#standalone)
- [Usage](#usage)
Expand Down Expand Up @@ -52,14 +53,15 @@ const { config: webpackConfig, plugins } = config({
|---------|----|-----------|
|[useProxy](#useproxy)|`boolean`|Enables webpack proxy.|
|[localChrome](#localChrome)|`string`|Path to your local chrome build folder.|
|[registry](#registry)|`(({ app, server, compiler, standaloneConfig }) => void)[]`|Express middleware to register.|
|[routes](#routes)|`object`|An object with additional routes.|
|[routesPath](#routespath)|`string`|A path to an object with additional routes.|
|[customProxy](#custom-proxy-settings)|`object[]`|An array of custom provided proxy configurations.|
|[env](#env)|`string`|Environment to proxy against such as ci-beta.|
|[useCloud](#use-cloud)|`boolean`|Toggle to use old fallback to cloud.redhat.com paths instead of console.redhat.com.|
|[target](#target)|`string`|Override `env` and `useCloud` to use a custom URI.|

### localChrome
#### localChrome

You can also easily run you application with a local build of Chrome by adding `localChrome: <absolute_path_to_chrome_build_folder>`.

Expand All @@ -79,11 +81,35 @@ INSIGHTS_CHROME=/Users/rvsiansk/insights-project/insights-chrome/build/

To check what the proxy is doing with your local chrome settings you can set `proxyVerbose: true`.

### Custom routes
#### Registry

You can extend [express middleware](https://expressjs.com/en/guide/using-middleware.html) before Webpack or standalone does by passing an array of callbacks. This can be useful to override specific test files independent of `standalone` config.

```js
const express = require('express');

registry: [
// App is the express app object.
// Server is the webpack-dev-server object with config. This will break with webpack-dev-server@v4, so tread lightly
// Compiler is the webpack compiler object. You probably don't need it...
// StandaloneConfig is the parsed standalone config given

// Example: override main.yml
({ app, server, compiler, standaloneConfig }) => app.get('(/beta)?/config/main.yml', (_req, res) => res.send('heyo'))

// Example: override entire cloudServicesConfig
({ app, server, compiler, standaloneConfig }) => {
const staticConfig = express.static('pathToLocalCloudServicesConfig');
app.use('(/beta)?/config', staticConfig);
}
]
```

#### Custom routes

If you want to serve files or api from different URL you can either pass `routes` to config or `routesPath` for file which exports these routes.

#### routes
##### routes

```JS
rootFolder: resolve(__dirname, '../'),
Expand All @@ -96,7 +122,7 @@ If you want to serve files or api from different URL you can either pass `routes
},
```

#### routesPath
##### routesPath

```JS
rootFolder: resolve(__dirname, '../'),
Expand All @@ -122,7 +148,7 @@ module.exports = {
};
```

### Custom proxy settings
#### Custom proxy settings

You can add an array of additional [custom proxy configuration.](https://webpack.js.org/configuration/dev-server/#devserverproxy).

Expand All @@ -144,14 +170,14 @@ const { config: webpackConfig, plugins } = config({

This configuration will redirect all API requests to QA environment, so you can check CI UI with QA data.

### Env
#### Env
A hyphenated string in the form of (qa|ci|stage|prod)-(stable|beta). Used to determine the proxy target (such as ci.console.redhat.com or console.stage.redhat.com) and branch to checkout of build repos. If "stage" is specific qa is used as the branch.

### Use cloud
#### Use cloud

If you want to run in legacy mode pass `useCloud: true` in your config, this way paths which does not match your proxy config (API for instance) will be passed to `cloud.redhat.com` (respective to your env `ci|qa|stage`). Without this all fallback routes will be redirected to `console.redhat.com`.

### Target
#### Target
Override for the target `env` and `useCloud` build. Useful for cross-environment testing.

## standalone
Expand Down
6 changes: 4 additions & 2 deletions packages/config/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ module.exports = ({
appUrl = [],
proxyVerbose,
useCloud,
target
target,
registry
} = {}) => {
const filenameMask = `js/[name]${useFileHash ? '.[chunkhash]' : ''}.js`;
if (betaEnv) {
Expand Down Expand Up @@ -192,7 +193,8 @@ module.exports = ({
appUrl,
publicPath,
proxyVerbose,
target
target,
registry
})
}
};
Expand Down

0 comments on commit 8498a9a

Please sign in to comment.