Skip to content

Commit

Permalink
[stable-4.9] webpack dev: Change to API_PROXY instead of API_PROXY_HO…
Browse files Browse the repository at this point in the history
…ST & API_PROXY_PORT (#5381) (#5404)

* Change to API_PROXY=http://localhost:5001 instead of API_PROXY_HOST & API_PROXY_PORT (#5381)

to allow running against https backends

and ignore self-signed https certificates,
and spoof the Origin, Host & Referer headers correctly

(cherry picked from commit 7b57440)

* fixup webpack config
  • Loading branch information
himdel authored Jan 3, 2025
1 parent c0ac769 commit ef71bc8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ cd ansible-hub-ui
npm run start-standalone
```

(Or `API_PROXY=http://localhost:5001 npm run start-standalone`; run `API_PROXY=https://my-server.example.com npm run start-standalone` to run with external backend.)

Standalone mode only requires a running instance of the galaxy API for the UI to connect to.


Expand Down
20 changes: 10 additions & 10 deletions config/standalone.dev.webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const webpackBase = require('./webpack.base.config');
const { webpackBase, proxy } = require('./webpack.base.config');

// Used for getting the correct host when running in a container
const proxyHost = process.env.API_PROXY_HOST || 'localhost';
const proxyPort = process.env.API_PROXY_PORT || '55001';
const proxyTarget = process.env.API_PROXY || 'http://localhost:55001';

const apiBasePath = process.env.API_BASE_PATH || '/api/galaxy/';
const uiExternalLoginURI = process.env.UI_EXTERNAL_LOGIN_URI || '/login';

Expand All @@ -28,11 +28,11 @@ module.exports = webpackBase({
// Value for webpack.devServer.proxy
// https://webpack.js.org/configuration/dev-server/#devserverproxy
// used to get around CORS requirements when running in dev mode
WEBPACK_PROXY: {
'/api/': `http://${proxyHost}:${proxyPort}`,
'/pulp/api/': `http://${proxyHost}:${proxyPort}`,
'/v2/': `http://${proxyHost}:${proxyPort}`,
'/extensions/v2/': `http://${proxyHost}:${proxyPort}`,
'/static/rest_framework/': `http://${proxyHost}:${proxyPort}`,
},
WEBPACK_PROXY: [
proxy('/api/', proxyTarget),
proxy('/pulp/api/', proxyTarget),
proxy('/v2/', proxyTarget),
proxy('/extensions/v2/', proxyTarget),
proxy('/static/rest_framework/', proxyTarget),
],
});
2 changes: 1 addition & 1 deletion config/standalone.prod.webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const webpackBase = require('./webpack.base.config');
const { webpackBase } = require('./webpack.base.config');

// Compile configuration for stnadalone mode
module.exports = webpackBase({
Expand Down
28 changes: 21 additions & 7 deletions config/webpack.base.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,21 @@ const defaultConfigs = [
{ name: 'WEBPACK_PUBLIC_PATH', default: undefined, scope: 'webpack' },
];

module.exports = (inputConfigs) => {
const proxy = (route, target) => {
const u = new URL(target);
return {
context: [route],
target,
secure: false,
router: (req) => {
req.headers.host = u.host;
req.headers.origin = u.origin;
req.headers.referer = u.href;
},
};
};

const webpackBase = (inputConfigs) => {
const customConfigs = {};
const globals = {};

Expand Down Expand Up @@ -82,12 +96,7 @@ module.exports = (inputConfigs) => {
}`,
),
port: customConfigs.UI_PORT,
proxy: Object.entries(customConfigs.WEBPACK_PROXY).map(
([k, v]) => ({
context: [k],
target: v,
}),
),
proxy: customConfigs.WEBPACK_PROXY,
server: { type: customConfigs.UI_USE_HTTPS ? 'https' : 'http' },
static: { directory: resolve(__dirname, '../dist') },
},
Expand Down Expand Up @@ -167,3 +176,8 @@ module.exports = (inputConfigs) => {
},
};
};

module.exports = {
proxy,
webpackBase,
};

0 comments on commit ef71bc8

Please sign in to comment.