-
Notifications
You must be signed in to change notification settings - Fork 13.3k
feat: Apps-Engine services #28964
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
d-gubert
wants to merge
37
commits into
develop
Choose a base branch
from
feat/apps-engine-services
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: Apps-Engine services #28964
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
a3cc5c9
Import services from apps-to-service branch
d-gubert 5b84b57
Merge remote-tracking branch 'origin/develop' into feat/apps-engine-s…
d-gubert fe7df73
Merge AppsService into AppsEngineService
d-gubert 0f33e60
Adapt api service to microservice serialization
d-gubert 255e54a
Fix types and usages
d-gubert 170b2d3
Merge branch 'develop' into feat/apps-engine-services
d-gubert 68f4d71
Fix startup lock
d-gubert af4f5a7
Improve apps statistics service types
d-gubert ecfdc3a
Deserializing AppFabricationFulfillment
d-gubert 4eae25a
Improve comments in AppApiService
d-gubert 6f803ae
Rename apps services
d-gubert f004770
Register services
d-gubert 9d0cfe4
Merge remote-tracking branch 'origin/develop' into feat/apps-engine-s…
d-gubert 4d3557b
Merge branch 'develop' into feat/apps-engine-services
d-gubert fbf92cb
Merge branch 'develop' into feat/apps-engine-services
d-gubert 44d733b
Merge branch 'develop' into feat/apps-engine-services
d-gubert f91052a
Merge branch 'develop' into feat/apps-engine-services
d-gubert e3c5183
Merge branch 'develop' into feat/apps-engine-services
d-gubert 6af3553
Merge branch 'develop' into feat/apps-engine-services
d-gubert 92abdc6
Merge branch 'develop' into feat/apps-engine-services
d-gubert 06607df
Merge branch 'develop' into feat/apps-engine-services
534748a
Merge branch 'develop' into feat/apps-engine-services
74e4de1
Merge branch 'develop' into feat/apps-engine-services
7557790
Merge branch 'develop' into feat/apps-engine-services
ba404cb
Merge branch 'develop' into feat/apps-engine-services
d-gubert 9c1ba65
Merge branch 'develop' into feat/apps-engine-services
052d76c
Merge branch 'develop' into feat/apps-engine-services
1673d94
feat: register apps engine services
c9a7a02
Merge branch 'develop' into feat/apps-engine-services
98d7c4c
feat: get statistics from apps-engine service
2075a78
Merge remote-tracking branch 'origin/develop' into feat/apps-engine-s…
d-gubert 0ebe968
Apply change request
d-gubert 8b0cc7b
Merge remote-tracking branch 'origin/develop' into feat/apps-engine-s…
d-gubert 7c694fe
Merge remote-tracking branch 'origin/develop' into feat/apps-engine-s…
d-gubert 2a2f1db
Fix linting errors
d-gubert b630bb3
Typecheck hiccup
d-gubert fccb9cf
Further fix linting
d-gubert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 7 additions & 10 deletions
17
apps/meteor/app/statistics/server/lib/getAppsStatistics.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,14 @@ | ||
| import { AppStatus } from '@rocket.chat/apps-engine/definition/AppStatus'; | ||
| import { AppsStatistics } from '@rocket.chat/core-services'; | ||
|
|
||
| import { Apps } from '../../../../ee/server/apps'; | ||
| import { Info } from '../../../utils/rocketchat.info'; | ||
|
|
||
| export function getAppsStatistics() { | ||
| export async function getAppsStatistics() { | ||
| const { totalActive, totalFailed, totalInstalled } = await AppsStatistics.getStatistics(); | ||
|
|
||
| return { | ||
| engineVersion: Info.marketplaceApiVersion, | ||
| totalInstalled: Apps.isInitialized() && Apps.getManager().get().length, | ||
| totalActive: Apps.isInitialized() && Apps.getManager().get({ enabled: true }).length, | ||
| totalFailed: | ||
| Apps.isInitialized() && | ||
| Apps.getManager() | ||
| .get({ disabled: true }) | ||
| .filter(({ app: { status } }) => status !== AppStatus.MANUALLY_DISABLED).length, | ||
| totalInstalled, | ||
| totalActive, | ||
| totalFailed, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| import * as util from 'util'; | ||
|
|
||
| import type { RequestMethod } from '@rocket.chat/apps-engine/definition/accessors'; | ||
| import type { IApiEndpoint, IApiRequest } from '@rocket.chat/apps-engine/definition/api'; | ||
| import type { AppsApiServiceResponse, IAppsApiService, IRequestWithPrivateHash } from '@rocket.chat/core-services'; | ||
| import { ServiceClass } from '@rocket.chat/core-services'; | ||
| import type { Serialized } from '@rocket.chat/core-typings'; | ||
| import type { Request, NextFunction } from 'express'; | ||
| import { Router } from 'express'; | ||
|
|
||
| import type { AppServerOrchestrator } from '../orchestrator'; | ||
| import { OrchestratorFactory } from './orchestratorFactory'; | ||
|
|
||
| /** | ||
| * This type is used to replace the Express Response object, as in the service it won't be | ||
| * possible to get an instance of the original Response generated by Express. | ||
| * | ||
| * We use the `resolve` function to return the response to the caller. | ||
| */ | ||
| type PromiseResponse = { | ||
| resolve: (response: AppsApiServiceResponse) => void; | ||
| }; | ||
|
|
||
| type IAppsApiRequestHandler = (req: IRequestWithPrivateHash, res: PromiseResponse, next: NextFunction) => void; | ||
|
|
||
| interface IAppsApiRouter { | ||
| (req: Serialized<Request> | IRequestWithPrivateHash, res: PromiseResponse, next: NextFunction): void; | ||
| all(path: string, ...handlers: IAppsApiRequestHandler[]): IAppsApiRouter; | ||
| } | ||
|
|
||
| export class AppsApiService extends ServiceClass implements IAppsApiService { | ||
| protected name = 'apps-api'; | ||
|
|
||
| private apps: AppServerOrchestrator; | ||
|
|
||
| protected appRouters: Map<string, IAppsApiRouter>; | ||
|
|
||
| constructor() { | ||
| super(); | ||
| this.appRouters = new Map(); | ||
| this.apps = OrchestratorFactory.getOrchestrator(); | ||
| } | ||
|
|
||
| /* ---- ENDPOINT COMMUNICATION METHODS ---- */ | ||
|
|
||
| /** | ||
| * This method triggers the execution of a public route registered by an app. | ||
| * | ||
| * It is supposed to be called by the ENDPOINT COMMUNICATOR in the core, as it is | ||
| * the component that interfaces directly with the Express server. | ||
| * | ||
| * The returning promise will ALWAYS resolve, even if there's an error in the handler. | ||
| * | ||
| * The way we indicate an error to the caller is by returning a status code. | ||
| * | ||
| * We expect the caller to appropriately respond to their HTTP request based on | ||
| * the status code, headers and body returned | ||
| * | ||
| * @param req A dry request object, containing only information and no functions | ||
| * @returns A promise that resolve to AppsApiServiceResponse type | ||
| */ | ||
| public async handlePublicRequest(req: Serialized<Request>): Promise<AppsApiServiceResponse> { | ||
| return new Promise<AppsApiServiceResponse>((resolve) => { | ||
| const notFound = () => resolve({ statusCode: 404, body: 'Not found' }); | ||
|
|
||
| const router = this.appRouters.get(req.params.appId); | ||
|
|
||
| if (router) { | ||
| return router(req, { resolve }, notFound); | ||
| } | ||
|
|
||
| notFound(); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * This method triggers the execution of a private route registered by an app. | ||
| * | ||
| * It is supposed to be called by the ENDPOINT COMMUNICATOR in the core, as it is | ||
| * the component that interfaces directly with the Express server. | ||
| * | ||
| * The returning promise will ALWAYS resolve, even if there's an error in the handler. | ||
| * | ||
| * The way we indicate an error to the caller is by returning a status code. | ||
| * | ||
| * We expect the caller to appropriately respond to their HTTP request based on | ||
| * the status code, headers and body returned | ||
| * | ||
| * @param req A dry request object, containing only information and no functions | ||
| * @returns A promise that resolves when the request is done | ||
| */ | ||
| public async handlePrivateRequest(req: IRequestWithPrivateHash): Promise<AppsApiServiceResponse> { | ||
| return new Promise((resolve) => { | ||
| const notFound = () => resolve({ statusCode: 404, body: 'Not found' }); | ||
|
|
||
| const router = this.appRouters.get(req.params.appId); | ||
|
|
||
| if (router) { | ||
| req._privateHash = req.params.hash; | ||
| return router(req, { resolve }, notFound); | ||
| } | ||
|
|
||
| notFound(); | ||
| }); | ||
| } | ||
|
|
||
| /* ---- BRIDGE METHODS ---- */ | ||
|
|
||
| public async registerApi(endpoint: IApiEndpoint, appId: string): Promise<void> { | ||
| let router = this.appRouters.get(appId); | ||
|
|
||
| if (!router) { | ||
| // eslint-disable-next-line new-cap | ||
| router = Router() as unknown as IAppsApiRouter; | ||
| this.appRouters.set(appId, router); | ||
| } | ||
|
|
||
| const method = 'all'; | ||
|
|
||
| let routePath = endpoint.path.trim(); | ||
| if (!routePath.startsWith('/')) { | ||
| routePath = `/${routePath}`; | ||
| } | ||
|
|
||
| if (router[method] instanceof Function) { | ||
| router[method](routePath, this.authMiddleware(!!endpoint.authRequired), this._appApiExecutor(endpoint, appId)); | ||
| } | ||
| } | ||
|
|
||
| public async unregisterApi(appId: string): Promise<void> { | ||
| this.appRouters.delete(appId); | ||
| } | ||
|
|
||
| /* ---- PRIVATE METHODS ---- */ | ||
|
|
||
| private authMiddleware(authRequired: boolean) { | ||
| return (req: IRequestWithPrivateHash, res: PromiseResponse, next: NextFunction): void => { | ||
| if (!req.user && authRequired) { | ||
| return res.resolve({ | ||
| statusCode: 401, | ||
| body: 'Unauthorized', | ||
| }); | ||
| } | ||
|
|
||
| next(); | ||
| }; | ||
| } | ||
|
|
||
| private _appApiExecutor(endpoint: IApiEndpoint, appId: string) { | ||
| return (req: IRequestWithPrivateHash, { resolve }: PromiseResponse): void => { | ||
| // Microservice serializer converts Buffers to Uint8Arrays, so we need to convert it back | ||
| const content = util.types.isTypedArray(req.body) ? Buffer.from(req.body) : req.body; | ||
|
|
||
| const request: IApiRequest = { | ||
| content, | ||
| method: req.method.toLowerCase() as RequestMethod, | ||
| headers: req.headers as { [key: string]: string }, | ||
| query: (req.query as { [key: string]: string }) || {}, | ||
| params: req.params || {}, | ||
| privateHash: req._privateHash, | ||
| user: req.user && this.apps.getConverters()?.get('users')?.convertToApp(req.user), | ||
d-gubert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| this.apps | ||
| .getManager() | ||
| ?.getApiManager() | ||
| .executeApi(appId, endpoint.path, request) | ||
| .then(({ status, headers = {}, content }) => { | ||
| resolve({ | ||
| statusCode: status, | ||
| headers, | ||
| body: content, | ||
| }); | ||
| }) | ||
| .catch((reason) => { | ||
| // Should we handle this as an error? | ||
| resolve({ | ||
| statusCode: 500, | ||
| body: reason.message, | ||
| }); | ||
| }); | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import type { IVisitor } from '@rocket.chat/apps-engine/definition/livechat'; | ||
| import type { IMessage } from '@rocket.chat/apps-engine/definition/messages'; | ||
| import type { IRoom } from '@rocket.chat/apps-engine/definition/rooms'; | ||
| import type { IUser } from '@rocket.chat/apps-engine/definition/users'; | ||
| import { ServiceClass } from '@rocket.chat/core-services'; | ||
| import type { IAppsConverterService } from '@rocket.chat/core-services'; | ||
|
|
||
| import type { AppServerOrchestrator } from '../orchestrator'; | ||
| import { OrchestratorFactory } from './orchestratorFactory'; | ||
|
|
||
| export class AppsConverterService extends ServiceClass implements IAppsConverterService { | ||
| protected name = 'apps-converter'; | ||
|
|
||
| private apps: AppServerOrchestrator; | ||
|
|
||
| constructor() { | ||
| super(); | ||
|
|
||
| this.apps = OrchestratorFactory.getOrchestrator(); | ||
| } | ||
|
|
||
| async convertRoomById(id: string): Promise<IRoom> { | ||
| return this.apps.getConverters()?.get('rooms').convertById(id); | ||
| } | ||
|
|
||
| async convertMessageById(id: string): Promise<IMessage> { | ||
| return this.apps.getConverters()?.get('messages').convertById(id); | ||
| } | ||
|
|
||
| async convertVistitorByToken(token: string): Promise<IVisitor> { | ||
| return this.apps.getConverters()?.get('visitors').convertByToken(token); | ||
| } | ||
|
|
||
| async convertUserToApp(user: any): Promise<IUser> { | ||
| return this.apps.getConverters()?.get('users').convertToApp(user); | ||
| } | ||
|
|
||
| async convertUserById(id: string): Promise<IUser> { | ||
| return this.apps.getConverters()?.get('users').convertById(id); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| export { AppsApiService } from './apiService'; | ||
| export { AppsConverterService } from './converterService'; | ||
| export { AppsEngineService } from './service'; | ||
| export { AppsManagerService } from './managerService'; | ||
| export { AppsStatisticsService } from './statisticsService'; | ||
| export { AppsVideoManagerService } from './videoManagerService'; |
17 changes: 17 additions & 0 deletions
17
apps/meteor/ee/server/apps/services/lib/transformAppFabricationFulfillment.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import type { AppFabricationFulfillment as AppsEngineAppFabricationFulfillment } from '@rocket.chat/apps-engine/server/compiler'; | ||
| import type { AppFabricationFulfillment, AppsEngineAppResult } from '@rocket.chat/core-services'; | ||
|
|
||
| import { transformProxiedAppToAppResult } from './transformProxiedAppToAppResult'; | ||
|
|
||
| export function transformAppFabricationFulfillment(fulfillment: AppsEngineAppFabricationFulfillment): AppFabricationFulfillment { | ||
| return { | ||
| appId: fulfillment.getApp().getID(), | ||
| appsEngineResult: transformProxiedAppToAppResult(fulfillment.getApp()) as AppsEngineAppResult, | ||
| licenseValidationResult: { | ||
| errors: fulfillment.getLicenseValidationResult().getErrors() as Record<string, string>, | ||
| warnings: fulfillment.getLicenseValidationResult().getWarnings() as Record<string, string>, | ||
| }, | ||
| storageError: fulfillment.getStorageError(), | ||
| appUserError: fulfillment.getAppUserError() as { username: string; message: string }, | ||
| }; | ||
| } |
14 changes: 14 additions & 0 deletions
14
apps/meteor/ee/server/apps/services/lib/transformProxiedAppToAppResult.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import type { ProxiedApp } from '@rocket.chat/apps-engine/server/ProxiedApp'; | ||
| import type { AppsEngineAppResult } from '@rocket.chat/core-services'; | ||
|
|
||
| export function transformProxiedAppToAppResult(app?: ProxiedApp): AppsEngineAppResult | undefined { | ||
| if (!app) { | ||
| return; | ||
| } | ||
|
|
||
| return { | ||
| appId: app.getID(), | ||
| currentStatus: app.getStatus(), | ||
| storageItem: app.getStorageItem(), | ||
| }; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.