Skip to content

Commit

Permalink
feat: add configurable paths for app proxy plugin (#2567)
Browse files Browse the repository at this point in the history
  • Loading branch information
odinr authored Nov 13, 2024
1 parent 831f250 commit 71d57c2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .changeset/tidy-buttons-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@equinor/fusion-framework-cli': minor
---

Added functionality for allow providing paths for `config`, `manifest` and bundle in `app-proxy-plugin`. This is useful for plugins that need to load resources from a specific path.

This was required since the AppClient was changed in [#2520](https://github.com/equinor/fusion-framework/pull/2520) which broke the ability to load resources from the plugin.
1 change: 1 addition & 0 deletions packages/cli/src/bin/create-dev-serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export const createDevServer = async (options: {
version: String(pkg.packageJson.version),
generateConfig,
generateManifest,
manifestPath: `persons/me/apps/${appKey}`,
},
}),
// Restart the server when config changes or the dev portal source is updated
Expand Down
47 changes: 39 additions & 8 deletions packages/cli/src/lib/plugins/app-proxy/app-proxy-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AppManifest } from '@equinor/fusion-framework-app';
import { ClientRequest, IncomingMessage, ServerResponse } from 'node:http';

import { ApiAppConfig } from '../../../schemas.js';
import { join } from 'node:path';

/**
* Preserve token for executing proxy assets
Expand Down Expand Up @@ -47,8 +48,30 @@ export type AppProxyPluginOptions = {
version: string;
/** callback function for generating configuration for the application */
generateConfig: () => Promise<ApiAppConfig>;

/**
* string path to the app config
* @example `persons/me/apps/${app.key}/builds/${app.version}/config`
* @default `apps/${app.key}/builds/${app.version}/config`
*/
configPath?: string;

/** callback function for generating manifest for the application */
generateManifest: () => Promise<AppManifest>;

/**
* string path to the app manifest
* @example `persons/me/apps/${app.key}`
* @default `apps/${app.key}`
*/
manifestPath?: string;

/**
* string path to the app bundle
* @default `bundles/apps/${app.key}/${app.version}`
* @example `bundles/apps/${app.key}/${app.version}`
*/
bundlePath?: string;
};
};

Expand Down Expand Up @@ -128,22 +151,30 @@ export const appProxyPlugin = (options: AppProxyPluginOptions): Plugin => {
if (!app) return;

// serve app config if request matches the current app and version
const configPath = `${proxyPath}/apps/${app.key}/builds/${app.version}/config`;
const configPath = join(
proxyPath,
app.configPath ?? `apps/${app.key}/builds/${app.version}/config`,
);
server.middlewares.use(configPath, async (_req, res) => {
res.setHeader('content-type', 'application/json');
res.end(JSON.stringify(await app.generateConfig()));
});

// serve app manifest if request matches the current app
// todo this should have version
const manifestPath = `${proxyPath}/apps/${app.key}`;
server.middlewares.use(manifestPath, async (_req, res) => {
res.setHeader('content-type', 'application/json');
res.end(JSON.stringify(await app.generateManifest()));
});
const manifestPath = join(proxyPath, app.manifestPath ?? `apps/${app.key}`);
server.middlewares.use(
[proxyPath, manifestPath, app.key].join('/'),
async (_req, res) => {
res.setHeader('content-type', 'application/json');
res.end(JSON.stringify(await app.generateManifest()));
},
);

// serve local bundles if request matches the current app and version
const bundlePath = `${proxyPath}/bundles/apps/${app.key}/${app.version}`;
const bundlePath = join(
proxyPath,
app.bundlePath ?? `bundles/apps/${app.key}/${app.version}`,
);
server.middlewares.use(async (req, _res, next) => {
if (req.url?.match(bundlePath)) {
// remove proxy path from url
Expand Down

0 comments on commit 71d57c2

Please sign in to comment.