platform/pods/server/src/server.ts
Andrey Sobolev 55b51b0fd0
UBERF-7922: Split Server Storage to middlewares (#6464)
Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
2024-09-09 13:40:49 +05:00

98 lines
2.9 KiB
TypeScript

/* eslint-disable @typescript-eslint/unbound-method */
//
// Copyright © 2022 Hardcore Engineering Inc.
//
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//
import { type Branding, type BrandingMap, type WorkspaceIdWithUrl } from '@hcengineering/core'
import { BackupClientSession, buildStorageFromConfig, getMetricsContext } from '@hcengineering/server'
import { type Pipeline, type StorageConfiguration } from '@hcengineering/server-core'
import { type Token } from '@hcengineering/server-token'
import { ClientSession, start as startJsonRpc, type ServerFactory, type Session } from '@hcengineering/server-ws'
import { serverAiBotId } from '@hcengineering/server-ai-bot'
import { createAIBotAdapter } from '@hcengineering/server-ai-bot-resources'
import { createServerPipeline, registerServerPlugins, registerStringLoaders } from '@hcengineering/server-pipeline'
registerStringLoaders()
/**
* @public
*/
export function start (
dbUrl: string,
opt: {
fullTextUrl: string
storageConfig: StorageConfiguration
rekoniUrl: string
port: number
brandingMap: BrandingMap
serverFactory: ServerFactory
indexProcessing: number // 1000
indexParallel: number // 2
enableCompression?: boolean
accountsUrl: string
}
): () => Promise<void> {
const metrics = getMetricsContext()
registerServerPlugins()
const externalStorage = buildStorageFromConfig(opt.storageConfig, dbUrl)
const pipelineFactory = createServerPipeline(
metrics,
dbUrl,
{ ...opt, externalStorage },
{
serviceAdapters: {
[serverAiBotId]: {
factory: createAIBotAdapter,
db: '%ai-bot',
url: dbUrl
}
}
}
)
const sessionFactory = (
token: Token,
pipeline: Pipeline,
workspaceId: WorkspaceIdWithUrl,
branding: Branding | null
): Session => {
if (token.extra?.mode === 'backup') {
return new BackupClientSession(token, pipeline, workspaceId, branding)
}
return new ClientSession(token, pipeline, workspaceId, branding)
}
const onClose = startJsonRpc(getMetricsContext(), {
pipelineFactory,
sessionFactory,
port: opt.port,
brandingMap: opt.brandingMap,
serverFactory: opt.serverFactory,
enableCompression: opt.enableCompression,
accountsUrl: opt.accountsUrl,
externalStorage
})
return async () => {
await externalStorage.close()
await onClose()
}
}