Skip to content

Commit

Permalink
feat: make url stateful
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhoue-Joe <suzhou@amazon.com>

feat: optimize code

Signed-off-by: SuZhoue-Joe <suzhou@amazon.com>

feat: remove useless change

Signed-off-by: SuZhoue-Joe <suzhou@amazon.com>

feat: optimize url listener

Signed-off-by: SuZhoue-Joe <suzhou@amazon.com>

feat: make formatUrlWithWorkspaceId extensible

Signed-off-by: SuZhoue-Joe <suzhou@amazon.com>

feat: modify the async format to be sync function

Signed-off-by: SuZhoue-Joe <suzhou@amazon.com>

feat: use path to maintain workspace info

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

feat: optimize code

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

feat: optimize code

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

feat: optimize code

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

feat: optimize code

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

feat: optimize code

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

feat: optimize code

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

feat: format

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

feat: format

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

feat: format

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

feat: format

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

feat: format

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

feat: format

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

feat: format

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>
  • Loading branch information
SuZhou-Joe committed Mar 7, 2024
1 parent 99ae1a5 commit 86ac4a8
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 9 deletions.
24 changes: 19 additions & 5 deletions src/core/public/http/base_path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,28 @@ import { modifyUrl } from '@osd/std';
export class BasePath {
constructor(
private readonly basePath: string = '',
public readonly serverBasePath: string = basePath
public readonly serverBasePath: string = basePath,
private readonly workspaceBasePath: string = ''
) {}

public get = () => {
return `${this.basePath}${this.workspaceBasePath}`;
};

public getBasePath = () => {
return this.basePath;
};

public prepend = (path: string): string => {
if (!this.get()) return path;
return modifyUrl(path, (parts) => {
if (!parts.hostname && parts.pathname && parts.pathname.startsWith('/')) {
parts.pathname = `${this.get()}${parts.pathname}`;
}
});
};

public prependWithoutWorkspacePath = (path: string): string => {
if (!this.basePath) return path;
return modifyUrl(path, (parts) => {
if (!parts.hostname && parts.pathname && parts.pathname.startsWith('/')) {
Expand All @@ -50,16 +64,16 @@ export class BasePath {
};

public remove = (path: string): string => {
if (!this.basePath) {
if (!this.get()) {
return path;
}

if (path === this.basePath) {
if (path === this.get()) {
return '/';
}

if (path.startsWith(`${this.basePath}/`)) {
return path.slice(this.basePath.length);
if (path.startsWith(`${this.get()}/`)) {
return path.slice(this.get().length);
}

return path;
Expand Down
3 changes: 2 additions & 1 deletion src/core/public/http/http_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export class HttpService implements CoreService<HttpSetup, HttpStart> {
const opensearchDashboardsVersion = injectedMetadata.getOpenSearchDashboardsVersion();
const basePath = new BasePath(
injectedMetadata.getBasePath(),
injectedMetadata.getServerBasePath()
injectedMetadata.getServerBasePath(),
injectedMetadata.getWorkspaceBasePath()
);
const fetchService = new Fetch({ basePath, opensearchDashboardsVersion });
const loadingCount = this.loadingCount.setup({ fatalErrors });
Expand Down
11 changes: 8 additions & 3 deletions src/core/public/http/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,17 @@ export type HttpStart = HttpSetup;
*/
export interface IBasePath {
/**
* Gets the `basePath` string.
* Gets the `basePath + workspace` string.
*/
get: () => string;

/**
* Prepends `path` with the basePath.
* Prepends `path` with the basePath + workspace.
*/
prepend: (url: string) => string;

/**
* Removes the prepended basePath from the `path`.
* Removes the prepended basePath + workspace from the `path`.
*/
remove: (url: string) => string;

Expand All @@ -113,6 +113,11 @@ export interface IBasePath {
* See {@link BasePath.get} for getting the basePath value for a specific request
*/
readonly serverBasePath: string;

/**
* Prepends `path` with the basePath.
*/
prependWithoutWorkspacePath: (url: string) => string;
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/core/public/injected_metadata/injected_metadata_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
UserProvidedValues,
} from '../../server/types';
import { AppCategory, Branding } from '../';
import { getWorkspaceIdFromUrl } from '../utils';

export interface InjectedPluginMetadata {
id: PluginName;
Expand Down Expand Up @@ -153,6 +154,15 @@ export class InjectedMetadataService {
getSurvey: () => {
return this.state.survey;
},

getWorkspaceBasePath: () => {
const workspaceId = getWorkspaceIdFromUrl(window.location.href);
if (workspaceId) {
return `/w/${workspaceId}`;
}

return '';
},
};
}
}
Expand Down Expand Up @@ -188,6 +198,7 @@ export interface InjectedMetadataSetup {
};
getBranding: () => Branding;
getSurvey: () => string | undefined;
getWorkspaceBasePath: () => string;
}

/** @internal */
Expand Down
1 change: 1 addition & 0 deletions src/core/public/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@
export { shareWeakReplay } from './share_weak_replay';
export { Sha256 } from './crypto';
export { MountWrapper, mountReactNode } from './mount';
export { getWorkspaceIdFromUrl } from './workspace';
15 changes: 15 additions & 0 deletions src/core/public/utils/workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

export const getWorkspaceIdFromUrl = (url: string): string => {
const regexp = /\/w\/([^\/]*)/;
const urlObject = new URL(url);
const matchedResult = urlObject.pathname.match(regexp);
if (matchedResult) {
return matchedResult[1];
}

return '';
};
15 changes: 15 additions & 0 deletions src/plugins/workspace/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,24 @@
*/

import { Plugin } from '../../../core/public';
import { getWorkspaceIdFromUrl } from '../../../core/public/utils';

export class WorkspacePlugin implements Plugin<{}, {}, {}> {
private getWorkpsaceIdFromURL(): string | null {
return getWorkspaceIdFromUrl(window.location.href);
}
public async setup() {
/**
* Retrive workspace id from url
*/
const workspaceId = this.getWorkpsaceIdFromURL();

if (workspaceId) {
/**
* Enter a workspace
*/
}

return {};
}

Expand Down
41 changes: 41 additions & 0 deletions src/plugins/workspace/server/workspaces_service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { URL } from 'node:url';
import { HttpServiceSetup, Plugin } from 'opensearch-dashboards/server';

export interface WorkspacesSetupDeps {
http: HttpServiceSetup;
}

export class WorkspacesService implements Plugin<{}, {}> {
private proxyWorkspaceTrafficToRealHandler(setupDeps: WorkspacesSetupDeps) {
/**
* Proxy all {basePath}/w/{workspaceId}{osdPath*} paths to
* {basePath}{osdPath*}
*/
setupDeps.http.registerOnPreRouting((request, response, toolkit) => {
const regexp = /\/w\/([^\/]*)/;
const matchedResult = request.url.pathname.match(regexp);
if (matchedResult) {
const requestUrl = new URL(request.url.toString());
requestUrl.pathname = requestUrl.pathname.replace(regexp, '');
return toolkit.rewriteUrl(requestUrl.toString());
}
return toolkit.next();
});
}

public async setup(setupDeps: WorkspacesSetupDeps) {
this.proxyWorkspaceTrafficToRealHandler(setupDeps);

return {};
}

public async start(deps: {}) {
return {};
}

public async stop() {}
}

0 comments on commit 86ac4a8

Please sign in to comment.