Skip to content

Commit d8f7ec8

Browse files
authored
Merge pull request #2236 from dominikvagner/add-dev-proxy
feat(config): add new dev-proxy cmd to fec bin
2 parents 73ed77b + 687e9bf commit d8f7ec8

File tree

8 files changed

+522
-38
lines changed

8 files changed

+522
-38
lines changed

packages/config/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
- [fec node scripts](#fec-node-scripts)
3131
- [Usage](#usage-1)
3232
- [Patch etc hosts](#patch-etc-hosts)
33+
- [Dev proxy](#dev-proxy)
3334
- [Static](#static)
3435
- [Inventory example](#inventory-example)
3536
- [In inventory UI repository changes](#in-inventory-ui-repository-changes)
@@ -380,6 +381,52 @@ Use binary in your `package.json` scripts section:
380381
## Patch etc hosts
381382
This is a required step for first time setup. It will allow your localhost to map to [env].foo.redhat.com. This is required to run only once on your machine. **Your OS may require running the script as sudo**!
382383

384+
## Dev proxy
385+
386+
A script that starts up a complete development environment with webpack build, static asset serving and a containerized development proxy for environment proxying.
387+
This command provides an alternative to the traditional webpack dev server approach by using a customized Caddy server container to handle proxy routing, which should be more reliable and faster due to HTTP2 support and better websocket handling.
388+
The used containerized proxy and its documentation can be found in this repo: [frontend-development-proxy](https://github.com/RedHatInsights/frontend-development-proxy).
389+
390+
**Usage:**
391+
```bash
392+
fec dev-proxy [options]
393+
```
394+
395+
**Options:**
396+
- `--port, -p`: Proxy server port (default: 1337)
397+
- `--staticPort, -sp`: Static assets server port (default: 8003)
398+
- `--clouddotEnv`: Set platform environment ('stage', 'prod', 'dev', 'ephemeral')
399+
400+
**Requirements:**
401+
- Docker or Podman installed and available
402+
- Valid FEC configuration file (fec.config.js)
403+
- Network access to Red Hat proxy services (for stage environments)
404+
405+
**Example:**
406+
```bash
407+
# Start development proxy on default ports
408+
fec dev-proxy
409+
410+
# Start with custom port and target stage environment
411+
fec dev-proxy --port 3000 --clouddotEnv stage
412+
413+
# Start with custom static assets port
414+
fec dev-proxy --staticPort 9000
415+
```
416+
417+
The command will:
418+
1. Pull and start the development proxy container
419+
2. Start webpack in watch mode to build your assets
420+
3. Serve static assets via http-server
421+
4. Configure routing based on your FEC config (configuration is the same as for the dev command, [Custom routes](#custom-routes))
422+
5. Start a local Chrome UI server _(this can be disabled by setting `SPAFallback` var to `false` in the FEC config)_
423+
6. Display the development URL when ready
424+
425+
Your application will be available at `https://[env].foo.redhat.com:[port]` where `[env]` is determined by your configuration or the `--clouddotEnv` flag.
426+
427+
**Limitations:**
428+
This command currently doesn't work in DinD environments. The new proxy can still be used there when launched manually with different network settings for running alongside frontends started with the static command. [1](https://github.com/RedHatInsights/frontend-development-proxy?tab=readme-ov-file#dind-docker-in-docker-ci)
429+
383430
## Static
384431

385432
A script that will run webpack build and serve webpack output through `http-serve` server. **This is not supposed to replace webpack dev server!**

packages/config/src/bin/common.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { LogType, fecLogger } from '@redhat-cloud-services/frontend-components-config-utilities';
1+
import inquirer from 'inquirer';
2+
import { FrontendCRD } from '@redhat-cloud-services/frontend-components-config-utilities/feo/feo-types';
3+
import { fecLogger, LogType } from '@redhat-cloud-services/frontend-components-config-utilities';
4+
import { hasFEOFeaturesEnabled, readFrontendCRD } from '@redhat-cloud-services/frontend-components-config-utilities/feo/crd-check';
25

36
const { resolve } = require('path');
47
const { statSync } = require('fs');
@@ -45,5 +48,63 @@ export function getWebpackConfigPath(path: string, cwd: string) {
4548
}
4649
}
4750

51+
export async function setEnv(cwd: string) {
52+
return inquirer
53+
.prompt([
54+
{
55+
type: 'list',
56+
name: 'clouddotEnv',
57+
message: 'Which platform environment you want to use?',
58+
choices: ['stage', 'prod', 'dev', 'ephemeral'],
59+
},
60+
])
61+
.then(async (answers) => {
62+
const { clouddotEnv } = answers;
63+
64+
if (clouddotEnv === 'ephemeral') {
65+
const answer = await inquirer.prompt([
66+
{
67+
type: 'input',
68+
name: 'clouddotEnv',
69+
message: 'Please provide the gateway route of your ephemeral environment:',
70+
},
71+
]);
72+
process.env.EPHEMERAL_TARGET = answer.clouddotEnv;
73+
}
74+
process.env.CLOUDOT_ENV = clouddotEnv ? clouddotEnv : 'stage';
75+
process.env.FEC_ROOT_DIR = cwd;
76+
});
77+
}
78+
79+
export function getCdnPath(fecConfig: any, webpackConfig: any, cwd: string): string {
80+
let cdnPath: string;
81+
const { insights } = require(`${cwd}/package.json`);
82+
const frontendCRDPath = fecConfig.frontendCRDPath ?? `${cwd}/deploy/frontend.yaml`;
83+
const frontendCRDRef: { current?: FrontendCRD } = { current: undefined };
84+
let FEOFeaturesEnabled = false;
85+
86+
try {
87+
frontendCRDRef.current = readFrontendCRD(frontendCRDPath);
88+
FEOFeaturesEnabled = hasFEOFeaturesEnabled(frontendCRDRef.current);
89+
} catch (e) {
90+
fecLogger(
91+
LogType.warn,
92+
`FEO features are not enabled. Unable to find frontend CRD file at ${frontendCRDPath}. If you want FEO features for local development, make sure to have a "deploy/frontend.yaml" file in your project or specify its location via "frontendCRDPath" attribute.`,
93+
);
94+
}
95+
96+
if (FEOFeaturesEnabled && fecConfig.publicPath === 'auto' && frontendCRDRef.current) {
97+
cdnPath = `${frontendCRDRef.current?.objects[0]?.spec.frontend.paths[0]}/`.replace(/\/\//, '/');
98+
} else if (fecConfig.publicPath === 'auto') {
99+
cdnPath = `/${fecConfig.deployment || 'apps'}/${insights.appname}/`;
100+
} else {
101+
cdnPath = webpackConfig.output.publicPath;
102+
}
103+
104+
return cdnPath ?? '';
105+
}
106+
48107
module.exports.validateFECConfig = validateFECConfig;
49108
module.exports.getWebpackConfigPath = getWebpackConfigPath;
109+
module.exports.setEnv = setEnv;
110+
module.exports.getCdnPath = getCdnPath;

0 commit comments

Comments
 (0)