diff --git a/packages/server-analysis-manager/src/components/builder/events/write-building.ts b/packages/server-analysis-manager/src/components/builder/events/write-building.ts index d7c9d0190..a80efe367 100644 --- a/packages/server-analysis-manager/src/components/builder/events/write-building.ts +++ b/packages/server-analysis-manager/src/components/builder/events/write-building.ts @@ -6,7 +6,7 @@ */ import type { ComponentContextWithCommand } from '@privateaim/server-kit'; -import { useAmqpClient } from '../../../core'; +import { useAmqpClient } from '@privateaim/server-kit'; import type { BuilderCommand } from '../constants'; import { BuilderEvent } from '../constants'; import type { BuilderBuildCommandContext, BuilderBuildPayload } from '../type'; diff --git a/packages/server-analysis-manager/src/components/builder/events/write-built.ts b/packages/server-analysis-manager/src/components/builder/events/write-built.ts index 1d84f6c54..4bf8920f4 100644 --- a/packages/server-analysis-manager/src/components/builder/events/write-built.ts +++ b/packages/server-analysis-manager/src/components/builder/events/write-built.ts @@ -6,7 +6,7 @@ */ import type { ComponentContextWithCommand } from '@privateaim/server-kit'; -import { useAmqpClient } from '../../../core'; +import { useAmqpClient } from '@privateaim/server-kit'; import type { BuilderCommand } from '../constants'; import { BuilderEvent } from '../constants'; import type { BuilderBuildCommandContext, BuilderBuildPayload } from '../type'; diff --git a/packages/server-analysis-manager/src/components/builder/events/write-checked.ts b/packages/server-analysis-manager/src/components/builder/events/write-checked.ts index 9ffbf5b5e..1a276b24a 100644 --- a/packages/server-analysis-manager/src/components/builder/events/write-checked.ts +++ b/packages/server-analysis-manager/src/components/builder/events/write-checked.ts @@ -5,7 +5,7 @@ * view the LICENSE file that was distributed with this source code. */ -import { useAmqpClient } from '../../../core'; +import { useAmqpClient } from '@privateaim/server-kit'; import { BuilderEvent } from '../constants'; import type { BuilderCheckCommandContext, BuilderCheckPayload } from '../type'; import { buildBuilderAggregatorQueuePayload } from '../utils'; diff --git a/packages/server-analysis-manager/src/components/builder/events/write-checking.ts b/packages/server-analysis-manager/src/components/builder/events/write-checking.ts index 931208ae7..c80adedfb 100644 --- a/packages/server-analysis-manager/src/components/builder/events/write-checking.ts +++ b/packages/server-analysis-manager/src/components/builder/events/write-checking.ts @@ -5,7 +5,7 @@ * view the LICENSE file that was distributed with this source code. */ -import { useAmqpClient } from '../../../core'; +import { useAmqpClient } from '@privateaim/server-kit'; import { BuilderEvent } from '../constants'; import type { BuilderCheckCommandContext, BuilderCheckPayload } from '../type'; import { buildBuilderAggregatorQueuePayload } from '../utils'; diff --git a/packages/server-analysis-manager/src/components/builder/events/write-failed.ts b/packages/server-analysis-manager/src/components/builder/events/write-failed.ts index 4eba8ec42..09231530f 100644 --- a/packages/server-analysis-manager/src/components/builder/events/write-failed.ts +++ b/packages/server-analysis-manager/src/components/builder/events/write-failed.ts @@ -6,7 +6,7 @@ */ import type { ComponentContextWithCommand, ComponentContextWithError } from '@privateaim/server-kit'; -import { useAmqpClient } from '../../../core'; +import { useAmqpClient } from '@privateaim/server-kit'; import type { BuilderCommand } from '../constants'; import { BuilderEvent } from '../constants'; import type { BuilderCommandContext } from '../type'; diff --git a/packages/server-analysis-manager/src/components/builder/events/write-none.ts b/packages/server-analysis-manager/src/components/builder/events/write-none.ts index 90d944c57..8ec37f602 100644 --- a/packages/server-analysis-manager/src/components/builder/events/write-none.ts +++ b/packages/server-analysis-manager/src/components/builder/events/write-none.ts @@ -5,7 +5,7 @@ * view the LICENSE file that was distributed with this source code. */ -import { useAmqpClient } from '../../../core'; +import { useAmqpClient } from '@privateaim/server-kit'; import { BuilderEvent } from '../constants'; import type { BuilderCheckCommandContext, BuilderCheckPayload } from '../type'; import { buildBuilderAggregatorQueuePayload } from '../utils'; diff --git a/packages/server-analysis-manager/src/components/builder/events/write-pushed.ts b/packages/server-analysis-manager/src/components/builder/events/write-pushed.ts index da016a54f..35f192b4d 100644 --- a/packages/server-analysis-manager/src/components/builder/events/write-pushed.ts +++ b/packages/server-analysis-manager/src/components/builder/events/write-pushed.ts @@ -6,7 +6,7 @@ */ import type { ComponentContextWithCommand } from '@privateaim/server-kit'; -import { useAmqpClient } from '../../../core'; +import { useAmqpClient } from '@privateaim/server-kit'; import type { BuilderCommand } from '../constants'; import { BuilderEvent } from '../constants'; import type { BuilderBuildCommandContext, BuilderBuildPayload } from '../type'; diff --git a/packages/server-analysis-manager/src/components/builder/events/write-pushing.ts b/packages/server-analysis-manager/src/components/builder/events/write-pushing.ts index 69fed8ebe..e324ecb0d 100644 --- a/packages/server-analysis-manager/src/components/builder/events/write-pushing.ts +++ b/packages/server-analysis-manager/src/components/builder/events/write-pushing.ts @@ -6,7 +6,7 @@ */ import type { ComponentContextWithCommand } from '@privateaim/server-kit'; -import { useAmqpClient } from '../../../core'; +import { useAmqpClient } from '@privateaim/server-kit'; import type { BuilderCommand } from '../constants'; import { BuilderEvent } from '../constants'; import type { BuilderBuildCommandContext, BuilderBuildPayload } from '../type'; diff --git a/packages/server-analysis-manager/src/components/builder/utils/logger.ts b/packages/server-analysis-manager/src/components/builder/utils/logger.ts index d46bb1515..c144c7911 100644 --- a/packages/server-analysis-manager/src/components/builder/utils/logger.ts +++ b/packages/server-analysis-manager/src/components/builder/utils/logger.ts @@ -7,7 +7,7 @@ import type { Logger } from '@privateaim/server-kit'; import { createLogger } from '@privateaim/server-kit'; -import { getWritableDirPath } from '../../../config'; +import { WRITABLE_DIRECTORY_PATH } from '../../../config'; import { ComponentName } from '../../constants'; let instance : Logger | undefined; @@ -24,7 +24,7 @@ export function useBuilderLogger() : Logger { }, }, // todo: allow customizing specific path - directory: getWritableDirPath(), + directory: WRITABLE_DIRECTORY_PATH, }); return instance; diff --git a/packages/server-analysis-manager/src/components/core/events/write-configured.ts b/packages/server-analysis-manager/src/components/core/events/write-configured.ts index 4ef4157ff..1879eccd5 100644 --- a/packages/server-analysis-manager/src/components/core/events/write-configured.ts +++ b/packages/server-analysis-manager/src/components/core/events/write-configured.ts @@ -5,7 +5,7 @@ * view the LICENSE file that was distributed with this source code. */ -import { useAmqpClient } from '../../../core'; +import { useAmqpClient } from '@privateaim/server-kit'; import { CoreEvent } from '../constants'; import type { CoreConfigureCommandContext } from '../type'; import { buildCoreAggregatorQueuePayload } from '../utils'; diff --git a/packages/server-analysis-manager/src/components/core/events/write-configuring.ts b/packages/server-analysis-manager/src/components/core/events/write-configuring.ts index 12f9ee321..48c23bc9c 100644 --- a/packages/server-analysis-manager/src/components/core/events/write-configuring.ts +++ b/packages/server-analysis-manager/src/components/core/events/write-configuring.ts @@ -5,7 +5,7 @@ * view the LICENSE file that was distributed with this source code. */ -import { useAmqpClient } from '../../../core'; +import { useAmqpClient } from '@privateaim/server-kit'; import { CoreEvent } from '../constants'; import type { CoreConfigureCommandContext } from '../type'; import { buildCoreAggregatorQueuePayload } from '../utils'; diff --git a/packages/server-analysis-manager/src/components/core/events/write-destroyed.ts b/packages/server-analysis-manager/src/components/core/events/write-destroyed.ts index f4d37fdf1..872e9af62 100644 --- a/packages/server-analysis-manager/src/components/core/events/write-destroyed.ts +++ b/packages/server-analysis-manager/src/components/core/events/write-destroyed.ts @@ -5,7 +5,7 @@ * view the LICENSE file that was distributed with this source code. */ -import { useAmqpClient } from '../../../core'; +import { useAmqpClient } from '@privateaim/server-kit'; import { CoreEvent } from '../constants'; import type { CoreDestroyCommandContext } from '../type'; import { buildCoreAggregatorQueuePayload } from '../utils'; diff --git a/packages/server-analysis-manager/src/components/core/events/write-destroying.ts b/packages/server-analysis-manager/src/components/core/events/write-destroying.ts index 08ce86bde..fdfab6de4 100644 --- a/packages/server-analysis-manager/src/components/core/events/write-destroying.ts +++ b/packages/server-analysis-manager/src/components/core/events/write-destroying.ts @@ -5,7 +5,7 @@ * view the LICENSE file that was distributed with this source code. */ -import { useAmqpClient } from '../../../core'; +import { useAmqpClient } from '@privateaim/server-kit'; import { CoreEvent } from '../constants'; import type { CoreDestroyCommandContext } from '../type'; import { buildCoreAggregatorQueuePayload } from '../utils'; diff --git a/packages/server-analysis-manager/src/components/core/events/write-failed.ts b/packages/server-analysis-manager/src/components/core/events/write-failed.ts index 88791dcda..aa8111df8 100644 --- a/packages/server-analysis-manager/src/components/core/events/write-failed.ts +++ b/packages/server-analysis-manager/src/components/core/events/write-failed.ts @@ -6,7 +6,7 @@ */ import type { ComponentContextWithError } from '@privateaim/server-kit'; -import { useAmqpClient } from '../../../core'; +import { useAmqpClient } from '@privateaim/server-kit'; import { CoreEvent } from '../constants'; import type { CoreCommandContext } from '../type'; import { buildCoreAggregatorQueuePayload } from '../utils'; diff --git a/packages/server-analysis-manager/src/components/core/utils/logger.ts b/packages/server-analysis-manager/src/components/core/utils/logger.ts index ac88d0ebb..07baf4fe3 100644 --- a/packages/server-analysis-manager/src/components/core/utils/logger.ts +++ b/packages/server-analysis-manager/src/components/core/utils/logger.ts @@ -7,7 +7,7 @@ import type { Logger } from '@privateaim/server-kit'; import { createLogger } from '@privateaim/server-kit'; -import { getWritableDirPath } from '../../../config'; +import { WRITABLE_DIRECTORY_PATH } from '../../../config'; import { ComponentName } from '../../constants'; let instance : Logger | undefined; @@ -24,7 +24,7 @@ export function useCoreLogger() : Logger { }, }, // todo: allow specifying custom path - directory: getWritableDirPath(), + directory: WRITABLE_DIRECTORY_PATH, }); return instance; diff --git a/packages/server-analysis-manager/src/components/utils/queue/router/module.ts b/packages/server-analysis-manager/src/components/utils/queue/router/module.ts index 7939a0e81..e5a2b04aa 100644 --- a/packages/server-analysis-manager/src/components/utils/queue/router/module.ts +++ b/packages/server-analysis-manager/src/components/utils/queue/router/module.ts @@ -8,13 +8,12 @@ import type { Component, } from '@privateaim/server-kit'; -import { isComponentCommandQueuePayload } from '@privateaim/server-kit'; +import { isComponentCommandQueuePayload, useAmqpClient, useLogger } from '@privateaim/server-kit'; import { ComponentName } from '../../../constants'; import { executeCoreCommand } from '../../../core'; import { executeBuilderCommand, } from '../../../index'; -import { useAmqpClient, useLogger } from '../../../../core'; import { ROUTER_QUEUE_ROUTING_KEY } from './constants'; export function buildComponentRouter() : Component { diff --git a/packages/server-storage/src/config/types.ts b/packages/server-analysis-manager/src/config/constants.ts similarity index 57% rename from packages/server-storage/src/config/types.ts rename to packages/server-analysis-manager/src/config/constants.ts index 818cb1a80..9bd971559 100644 --- a/packages/server-storage/src/config/types.ts +++ b/packages/server-analysis-manager/src/config/constants.ts @@ -5,8 +5,7 @@ * view the LICENSE file that was distributed with this source code. */ -import type { Client } from 'redis-extension'; +import path from 'node:path'; +import process from 'node:process'; -export type Config = { - redisDatabase: Client -}; +export const WRITABLE_DIRECTORY_PATH = path.join(process.cwd(), 'writable'); diff --git a/packages/server-analysis-manager/src/config/index.ts b/packages/server-analysis-manager/src/config/index.ts index 024c09fad..bac1ded1b 100644 --- a/packages/server-analysis-manager/src/config/index.ts +++ b/packages/server-analysis-manager/src/config/index.ts @@ -7,4 +7,5 @@ export * from './env'; export * from './module'; -export * from './paths'; +export * from './constants'; +export * from './types'; diff --git a/packages/server-analysis-manager/src/config/module.ts b/packages/server-analysis-manager/src/config/module.ts index e63cd8ae1..23415aa04 100644 --- a/packages/server-analysis-manager/src/config/module.ts +++ b/packages/server-analysis-manager/src/config/module.ts @@ -5,31 +5,22 @@ * view the LICENSE file that was distributed with this source code. */ -import type { TokenCreatorOptions } from '@authup/core-http-kit'; import type { Aggregator, Component } from '@privateaim/server-kit'; +import { guessAuthupTokenCreatorOptions } from '@privateaim/server-kit'; import { buildComponentRouter } from '../components'; -import { useEnv } from './env'; import { - configureAMQP, - configureCoreService, - configureStorageService, + configureAMQP, configureCoreService, configureStorageService, setupLogger, setupVault, } from './services'; - -export type Config = { - aggregators: {start: () => void}[] - components: {start: () => void}[] -}; +import type { Config } from './types'; export function createConfig() : Config { - const tokenCreator : TokenCreatorOptions = { - type: 'robotInVault', - name: 'system', - vault: useEnv('vaultConnectionString'), - }; + setupLogger(); + setupVault(); + configureAMQP(); + const tokenCreator = guessAuthupTokenCreatorOptions(); configureStorageService({ tokenCreator }); configureCoreService({ tokenCreator }); - configureAMQP(); const aggregators : Aggregator[] = []; diff --git a/packages/server-analysis-manager/src/config/paths.ts b/packages/server-analysis-manager/src/config/paths.ts deleted file mode 100644 index e2eb24aa4..000000000 --- a/packages/server-analysis-manager/src/config/paths.ts +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (c) 2022-2024. - * Author Peter Placzek (tada5hi) - * For the full copyright and license information, - * view the LICENSE file that was distributed with this source code. - */ - -import path from 'node:path'; -import type { Analysis } from '@privateaim/core'; - -let writableDirPath : string | undefined; - -export function getWritableDirPath() { - if (typeof writableDirPath !== 'undefined') { - return writableDirPath; - } - - writableDirPath = path.resolve(`${__dirname}../../../writable`); - return writableDirPath; -} - -export function generateTrainMinioBucketName(id: Analysis['id']) { - return `trains.${id}`; -} diff --git a/packages/server-analysis-manager/src/config/services/amqp.ts b/packages/server-analysis-manager/src/config/services/amqp.ts index 8bdd2bf68..ce061e772 100644 --- a/packages/server-analysis-manager/src/config/services/amqp.ts +++ b/packages/server-analysis-manager/src/config/services/amqp.ts @@ -6,11 +6,11 @@ */ import { Client } from 'amqp-extension'; -import { setAmqpFactory } from '../../core/amqp'; +import { setAmqpClientFactory } from '@privateaim/server-kit'; import { useEnv } from '../env'; export function configureAMQP() { - setAmqpFactory(() => new Client({ + setAmqpClientFactory(() => new Client({ connection: useEnv('rabbitMqConnectionString'), exchange: { name: 'pht', diff --git a/packages/server-analysis-manager/src/config/services/index.ts b/packages/server-analysis-manager/src/config/services/index.ts index 914d9b59e..70555db79 100644 --- a/packages/server-analysis-manager/src/config/services/index.ts +++ b/packages/server-analysis-manager/src/config/services/index.ts @@ -7,4 +7,6 @@ export * from './amqp'; export * from './core'; +export * from './logger'; export * from './storage'; +export * from './vault'; diff --git a/packages/server-analysis-manager/src/config/services/logger.ts b/packages/server-analysis-manager/src/config/services/logger.ts new file mode 100644 index 000000000..a96277e3f --- /dev/null +++ b/packages/server-analysis-manager/src/config/services/logger.ts @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2024. + * Author Peter Placzek (tada5hi) + * For the full copyright and license information, + * view the LICENSE file that was distributed with this source code. + */ + +import { createLogger, setLoggerFactory } from '@privateaim/server-kit'; +import { WRITABLE_DIRECTORY_PATH } from '../constants'; + +export function setupLogger(): void { + setLoggerFactory(() => createLogger({ + directory: WRITABLE_DIRECTORY_PATH, + })); +} diff --git a/packages/server-analysis-manager/src/config/services/vault.ts b/packages/server-analysis-manager/src/config/services/vault.ts new file mode 100644 index 000000000..d00062d2b --- /dev/null +++ b/packages/server-analysis-manager/src/config/services/vault.ts @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2024. + * Author Peter Placzek (tada5hi) + * For the full copyright and license information, + * view the LICENSE file that was distributed with this source code. + */ + +import { VaultClient, setVaultFactory } from '@privateaim/server-kit'; +import { useEnv } from '../env'; + +export function setupVault() { + const connectionString = useEnv('vaultConnectionString'); + if (typeof connectionString !== 'undefined') { + setVaultFactory(() => new VaultClient({ + connectionString, + })); + } +} diff --git a/packages/server-core/src/core/vault/index.ts b/packages/server-analysis-manager/src/config/types.ts similarity index 54% rename from packages/server-core/src/core/vault/index.ts rename to packages/server-analysis-manager/src/config/types.ts index 7424f1649..b567cfacb 100644 --- a/packages/server-core/src/core/vault/index.ts +++ b/packages/server-analysis-manager/src/config/types.ts @@ -4,5 +4,9 @@ * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ +import type { Aggregator, Component } from '@privateaim/server-kit'; -export * from './module'; +export type Config = { + aggregators: Aggregator[] + components: Component[] +}; diff --git a/packages/server-analysis-manager/src/core/index.ts b/packages/server-analysis-manager/src/core/index.ts index 18615206f..5efc4dd79 100644 --- a/packages/server-analysis-manager/src/core/index.ts +++ b/packages/server-analysis-manager/src/core/index.ts @@ -5,11 +5,9 @@ * view the LICENSE file that was distributed with this source code. */ -export * from './amqp'; export * from './core'; export * from './crypto'; export * from './docker'; export * from './harbor'; -export * from './logger'; export * from './storage'; export * from './utils'; diff --git a/packages/server-analysis-manager/src/core/logger/index.ts b/packages/server-analysis-manager/src/core/logger/index.ts deleted file mode 100644 index e564c7a3d..000000000 --- a/packages/server-analysis-manager/src/core/logger/index.ts +++ /dev/null @@ -1,8 +0,0 @@ -/* - * Copyright (c) 2023-2024. - * Author Peter Placzek (tada5hi) - * For the full copyright and license information, - * view the LICENSE file that was distributed with this source code. - */ - -export * from './module'; diff --git a/packages/server-analysis-manager/src/core/logger/module.ts b/packages/server-analysis-manager/src/core/logger/module.ts deleted file mode 100644 index f7f696f2e..000000000 --- a/packages/server-analysis-manager/src/core/logger/module.ts +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2022-2024. - * Author Peter Placzek (tada5hi) - * For the full copyright and license information, - * view the LICENSE file that was distributed with this source code. - */ - -import type { Logger } from '@privateaim/server-kit'; -import { createLogger } from '@privateaim/server-kit'; -import { getWritableDirPath } from '../../config'; - -let logger : undefined | any; - -/* -Levels -{ - error: 0, - warn: 1, - info: 2, - http: 3, - verbose: 4, - debug: 5, - silly: 6 -} - */ - -export function useLogger() : Logger { - if (typeof logger !== 'undefined') { - return logger; - } - - logger = createLogger({ - directory: getWritableDirPath(), - }); - - return logger; -} diff --git a/packages/server-analysis-manager/src/start.ts b/packages/server-analysis-manager/src/start.ts index 8a1a95666..eb80c388a 100644 --- a/packages/server-analysis-manager/src/start.ts +++ b/packages/server-analysis-manager/src/start.ts @@ -5,8 +5,8 @@ * view the LICENSE file that was distributed with this source code. */ +import { useLogger } from '@privateaim/server-kit'; import { createConfig, useEnv } from './config'; -import { useLogger } from './core'; import { createHttpServer } from './http'; const config = createConfig(); diff --git a/packages/server-core/src/aggregators/authup-service/entities/robot.ts b/packages/server-core/src/aggregators/authup-service/entities/robot.ts index a4283e73b..97c782b9d 100644 --- a/packages/server-core/src/aggregators/authup-service/entities/robot.ts +++ b/packages/server-core/src/aggregators/authup-service/entities/robot.ts @@ -8,13 +8,15 @@ import type { RobotEventContext } from '@authup/core-kit'; import { ServiceID } from '@privateaim/core'; import { useDataSource } from 'typeorm-extension'; -import { RegistryCommand } from '../../../components'; -import { buildRegistryPayload } from '../../../components/registry/utils/queue'; -import { useAmqpClient } from '../../../core'; +import { useAmqpClient } from '@privateaim/server-kit'; +import { RegistryCommand, buildRegistryPayload } from '../../../components'; import { RegistryProjectEntity } from '../../../domains'; export async function handleAuthupRobotEvent(context: RobotEventContext) { - if (context.event === 'created' || context.event === 'updated') { + if ( + context.event === 'created' || + context.event === 'updated' + ) { if (context.data.name === ServiceID.REGISTRY) { const dataSource = await useDataSource(); diff --git a/packages/server-core/src/aggregators/train-manager/module.ts b/packages/server-core/src/aggregators/train-manager/module.ts index b76a9c6c9..e078bda53 100644 --- a/packages/server-core/src/aggregators/train-manager/module.ts +++ b/packages/server-core/src/aggregators/train-manager/module.ts @@ -5,9 +5,10 @@ * view the LICENSE file that was distributed with this source code. */ -import { ComponentError, isComponentEventQueuePayload, useLogger } from '@privateaim/server-kit'; +import { + ComponentError, isComponentEventQueuePayload, useAmqpClient, useLogger, +} from '@privateaim/server-kit'; import { ComponentName } from '@privateaim/server-analysis-manager'; -import { useAmqpClient } from '../../core'; import type { Aggregator } from '../type'; import { handleTrainManagerBuilderEvent } from './builder'; diff --git a/packages/server-core/src/components/registry/handlers/cleanup.ts b/packages/server-core/src/components/registry/handlers/cleanup.ts index cb386a320..5bfe79db1 100644 --- a/packages/server-core/src/components/registry/handlers/cleanup.ts +++ b/packages/server-core/src/components/registry/handlers/cleanup.ts @@ -10,8 +10,7 @@ import { buildRegistryClientConnectionStringFromRegistry, } from '@privateaim/core'; import { useDataSource } from 'typeorm-extension'; -import { useLogger } from '@privateaim/server-kit'; -import { useAmqpClient } from '../../../core'; +import { useAmqpClient, useLogger } from '@privateaim/server-kit'; import { RegistryEntity, RegistryProjectEntity } from '../../../domains'; import { ComponentName } from '../../constants'; import { RegistryCommand } from '../constants'; diff --git a/packages/server-core/src/components/registry/handlers/helpers/remote-webhook.ts b/packages/server-core/src/components/registry/handlers/helpers/remote-webhook.ts index d60a39920..2a579f5a2 100644 --- a/packages/server-core/src/components/registry/handlers/helpers/remote-webhook.ts +++ b/packages/server-core/src/components/registry/handlers/helpers/remote-webhook.ts @@ -12,9 +12,8 @@ import type { import { ServiceID, } from '@privateaim/core'; -import { useLogger } from '@privateaim/server-kit'; +import { isAuthupClientUsable, useAuthupClient, useLogger } from '@privateaim/server-kit'; import { useEnv } from '../../../../config'; -import { hasAuthupClient, useAuthupClient } from '../../../../core'; import { findRobotCredentialsInVault } from '../../../../domains'; import { buildRegistryWebhookTarget } from '../utils'; @@ -25,7 +24,7 @@ export async function saveRemoteRegistryProjectWebhook( isProjectName?: boolean }, ) : Promise<{ id: number } | undefined> { - if (hasAuthupClient()) { + if (isAuthupClientUsable()) { await useAuthupClient().robot.integrity(ServiceID.REGISTRY); } diff --git a/packages/server-core/src/components/registry/handlers/setup.ts b/packages/server-core/src/components/registry/handlers/setup.ts index dcfacf1ce..510216742 100644 --- a/packages/server-core/src/components/registry/handlers/setup.ts +++ b/packages/server-core/src/components/registry/handlers/setup.ts @@ -12,8 +12,7 @@ import { RegistryProjectType, generateRegistryProjectId, } from '@privateaim/core'; import { useDataSource } from 'typeorm-extension'; -import { useLogger } from '@privateaim/server-kit'; -import { useAmqpClient } from '../../../core'; +import { useAmqpClient, useLogger } from '@privateaim/server-kit'; import { RegistryEntity, RegistryProjectEntity } from '../../../domains'; import { RegistryCommand } from '../constants'; import type { RegistrySetupPayload } from '../type'; diff --git a/packages/server-core/src/components/router/module.ts b/packages/server-core/src/components/router/module.ts index 3495caa37..314dfbc21 100644 --- a/packages/server-core/src/components/router/module.ts +++ b/packages/server-core/src/components/router/module.ts @@ -7,8 +7,7 @@ import type { Component } from '@privateaim/server-kit'; import type { ConsumeMessage } from 'amqp-extension'; -import { useLogger } from '@privateaim/server-kit'; -import { useAmqpClient } from '../../core'; +import { useAmqpClient, useLogger } from '@privateaim/server-kit'; import { ComponentName } from '../constants'; import { executeRegistryCommand } from '../registry'; import { ROUTER_QUEUE_ROUTING_KEY } from './constants'; diff --git a/packages/server-core/src/config/module.ts b/packages/server-core/src/config/module.ts index 7b7d9ae01..0d5a6fca5 100644 --- a/packages/server-core/src/config/module.ts +++ b/packages/server-core/src/config/module.ts @@ -5,8 +5,9 @@ * view the LICENSE file that was distributed with this source code. */ +import type { Aggregator, Component } from '@privateaim/server-kit'; +import { isAmqpClientUsable } from '@privateaim/server-kit'; import { buildAuthupAggregator, buildTrainManagerAggregator } from '../aggregators'; -import { hasAmqpClient } from '../core'; import { EnvironmentName, useEnv } from './env'; import { buildRouterComponent } from '../components'; @@ -16,8 +17,8 @@ import { } from './services'; export type Config = { - aggregators: {start: () => void}[] - components: {start: () => void}[] + aggregators: Aggregator[] + components: Component[] }; export function createConfig() : Config { @@ -40,7 +41,7 @@ export function createConfig() : Config { // --------------------------------------------- const aggregators : {start: () => void}[] = []; - if (!isTest && hasAmqpClient()) { + if (!isTest && isAmqpClientUsable()) { aggregators.push(buildAuthupAggregator()); aggregators.push(buildTrainManagerAggregator()); } @@ -48,7 +49,7 @@ export function createConfig() : Config { // --------------------------------------------- const components : {start: () => void}[] = []; - if (!isTest && hasAmqpClient()) { + if (!isTest && isAmqpClientUsable()) { components.push( buildRouterComponent(), ); diff --git a/packages/server-core/src/config/services/amqp.ts b/packages/server-core/src/config/services/amqp.ts index 1f0bca4d6..f5e6b506b 100644 --- a/packages/server-core/src/config/services/amqp.ts +++ b/packages/server-core/src/config/services/amqp.ts @@ -7,7 +7,7 @@ import { Client } from 'amqp-extension'; import { isBoolFalse, isBoolTrue } from '@privateaim/core'; -import { setAmqpFactory } from '../../core'; +import { setAmqpClientFactory } from '@privateaim/server-kit'; import { ConfigDefaults, useEnv } from '../env'; export function configureAmqp() { @@ -16,7 +16,7 @@ export function configureAmqp() { typeof connectionString !== 'undefined' && !isBoolFalse(connectionString) ) { - setAmqpFactory(() => new Client({ + setAmqpClientFactory(() => new Client({ connection: isBoolTrue(connectionString) ? ConfigDefaults.RABBITMQ : connectionString, exchange: { name: 'pht', diff --git a/packages/server-core/src/config/services/authup.ts b/packages/server-core/src/config/services/authup.ts index 9d2a048c9..f0b321cba 100644 --- a/packages/server-core/src/config/services/authup.ts +++ b/packages/server-core/src/config/services/authup.ts @@ -5,10 +5,11 @@ * view the LICENSE file that was distributed with this source code. */ -import type { TokenCreatorOptions } from '@authup/core-http-kit'; -import { Client, mountClientResponseErrorTokenHook } from '@authup/core-http-kit'; -import { useLogger } from '@privateaim/server-kit'; -import { hasVaultClient, setAuthupFactory, useVaultClient } from '../../core'; +import { + AuthupClient, + setAuthupClientFactory, + useLogger, +} from '@privateaim/server-kit'; import { useEnv } from '../env'; export function configureAuthup() { @@ -18,31 +19,7 @@ export function configureAuthup() { return; } - setAuthupFactory(() => { - const authupClient = new Client({ - baseURL, - }); - - let tokenCreator : TokenCreatorOptions; - if (hasVaultClient()) { - tokenCreator = { - type: 'robotInVault', - name: 'system', - vault: useVaultClient(), - }; - } else { - tokenCreator = { - type: 'user', - name: 'admin', - password: 'start123', - }; - } - - mountClientResponseErrorTokenHook(authupClient, { - baseURL, - tokenCreator, - }); - - return authupClient; - }); + setAuthupClientFactory(() => new AuthupClient({ + baseURL, + })); } diff --git a/packages/server-core/src/config/services/vault.ts b/packages/server-core/src/config/services/vault.ts index 37139974a..41a5fe6b6 100644 --- a/packages/server-core/src/config/services/vault.ts +++ b/packages/server-core/src/config/services/vault.ts @@ -5,9 +5,8 @@ * view the LICENSE file that was distributed with this source code. */ -import { VaultClient } from '@hapic/vault'; +import { VaultClient, setVaultFactory } from '@privateaim/server-kit'; import { isBoolFalse, isBoolTrue } from '@privateaim/core'; -import { setVaultFactory } from '../../core'; import { ConfigDefaults, useEnv } from '../env'; export function configureVault() { diff --git a/packages/server-core/src/core/amqp/module.ts b/packages/server-core/src/core/amqp/module.ts deleted file mode 100644 index 2833b5bbb..000000000 --- a/packages/server-core/src/core/amqp/module.ts +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (c) 2024. - * Author Peter Placzek (tada5hi) - * For the full copyright and license information, - * view the LICENSE file that was distributed with this source code. - */ - -import type { Client } from 'amqp-extension'; -import type { Factory } from 'singa'; -import { singa } from 'singa'; - -const instance = singa({ - name: 'amqp', -}); -export function setAmqpFactory(factory: Factory) { - instance.setFactory(factory); -} - -export function hasAmqpClient() { - return instance.has() || instance.hasFactory(); -} - -export function useAmqpClient() { - return instance.use(); -} diff --git a/packages/server-core/src/core/authup/index.ts b/packages/server-core/src/core/authup/index.ts index 9af4b8a1c..7c71c1d89 100644 --- a/packages/server-core/src/core/authup/index.ts +++ b/packages/server-core/src/core/authup/index.ts @@ -5,5 +5,4 @@ * view the LICENSE file that was distributed with this source code. */ -export * from './module'; export * from './setup'; diff --git a/packages/server-core/src/core/authup/setup.ts b/packages/server-core/src/core/authup/setup.ts index 6957db86e..9e58feb8f 100644 --- a/packages/server-core/src/core/authup/setup.ts +++ b/packages/server-core/src/core/authup/setup.ts @@ -12,10 +12,10 @@ import { REALM_MASTER_NAME } from '@authup/core-kit'; import { ServerError } from '@ebec/http'; import { isClientErrorWithStatusCode } from '@hapic/harbor'; import { PermissionKey, ServiceID } from '@privateaim/core'; -import { useLogger } from '@privateaim/server-kit'; -import { useAuthupClient } from './module'; +import { useAuthupClient, useLogger } from '@privateaim/server-kit'; export async function setupAuthupService(): Promise { + // todo: check if authup client is usable const authupClient = useAuthupClient(); // ------------------------------------------------- diff --git a/packages/server-core/src/core/harbor/module.ts b/packages/server-core/src/core/harbor/module.ts index cfb7d889b..020bc4372 100644 --- a/packages/server-core/src/core/harbor/module.ts +++ b/packages/server-core/src/core/harbor/module.ts @@ -8,8 +8,8 @@ import { parseConnectionString } from '@hapic/harbor'; import { getHostNameFromString } from '@privateaim/core'; import { useDataSource } from 'typeorm-extension'; +import { isAmqpClientUsable, useAmqpClient } from '@privateaim/server-kit'; import { RegistryCommand, buildRegistryPayload } from '../../components'; -import { hasAmqpClient, useAmqpClient } from '../amqp'; import { RegistryEntity } from '../../domains'; import { useEnv } from '../../config'; @@ -41,7 +41,7 @@ export async function setupHarborService() { await repository.save(entity); - if (!hasAmqpClient()) { + if (!isAmqpClientUsable()) { return; } diff --git a/packages/server-core/src/core/index.ts b/packages/server-core/src/core/index.ts index 4f8b5287a..c5db8afc2 100644 --- a/packages/server-core/src/core/index.ts +++ b/packages/server-core/src/core/index.ts @@ -5,8 +5,6 @@ * view the LICENSE file that was distributed with this source code. */ -export * from './amqp'; export * from './authup'; export * from './harbor'; export * from './utils'; -export * from './vault'; diff --git a/packages/server-core/src/core/vault/module.ts b/packages/server-core/src/core/vault/module.ts deleted file mode 100644 index f926a7b8d..000000000 --- a/packages/server-core/src/core/vault/module.ts +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (c) 2024. - * Author Peter Placzek (tada5hi) - * For the full copyright and license information, - * view the LICENSE file that was distributed with this source code. - */ - -import type { Factory } from 'singa'; -import { singa } from 'singa'; -import type { VaultClient } from '@hapic/vault'; - -const instance = singa({ - name: 'vault', -}); - -export function setVaultFactory(factory: Factory) { - instance.setFactory(factory); -} - -export function hasVaultClient() { - return instance.has() || instance.hasFactory(); -} - -export function useVaultClient() { - return instance.use(); -} diff --git a/packages/server-core/src/domains/analysis/commands/build-start.ts b/packages/server-core/src/domains/analysis/commands/build-start.ts index 9218418e2..073f32bd6 100644 --- a/packages/server-core/src/domains/analysis/commands/build-start.ts +++ b/packages/server-core/src/domains/analysis/commands/build-start.ts @@ -12,7 +12,7 @@ import { } from '@privateaim/core'; import { BuilderCommand, buildBuilderQueuePayload } from '@privateaim/server-analysis-manager'; import { useDataSource } from 'typeorm-extension'; -import { useAmqpClient } from '../../../core'; +import { useAmqpClient } from '@privateaim/server-kit'; import { RegistryEntity } from '../../registry'; import { AnalysisNodeEntity } from '../../anaylsis-node'; import { AnalysisEntity } from '../entity'; diff --git a/packages/server-core/src/domains/analysis/commands/build-status.ts b/packages/server-core/src/domains/analysis/commands/build-status.ts index 8cb236e94..f223087b8 100644 --- a/packages/server-core/src/domains/analysis/commands/build-status.ts +++ b/packages/server-core/src/domains/analysis/commands/build-status.ts @@ -7,7 +7,7 @@ import { BuilderCommand, buildBuilderQueuePayload } from '@privateaim/server-analysis-manager'; import { useDataSource } from 'typeorm-extension'; -import { useAmqpClient } from '../../../core'; +import { useAmqpClient } from '@privateaim/server-kit'; import { resolveAnalysis } from './utils'; import { AnalysisEntity } from '../entity'; diff --git a/packages/server-core/src/domains/registry-project/vault.ts b/packages/server-core/src/domains/registry-project/vault.ts index 06e1fc618..c4306367a 100644 --- a/packages/server-core/src/domains/registry-project/vault.ts +++ b/packages/server-core/src/domains/registry-project/vault.ts @@ -6,12 +6,12 @@ */ import { isClientErrorWithStatusCode } from '@hapic/harbor'; -import { hasVaultClient, useVaultClient } from '../../core'; +import { isVaultClientUsable, useVaultClient } from '@privateaim/server-kit'; import type { RegistryProjectVaultPayload } from './type'; import { isRegistryProjectVaultPayload } from './utils'; export async function creteRegistryProjectVaultEngine() { - if (!hasVaultClient()) { + if (!isVaultClientUsable()) { return; } @@ -29,7 +29,7 @@ export async function creteRegistryProjectVaultEngine() { } export async function removeRegistryProjectFromVault(name: string) { - if (!hasVaultClient()) { + if (!isVaultClientUsable()) { return; } @@ -50,7 +50,7 @@ export async function removeRegistryProjectFromVault(name: string) { } export async function saveRegistryProjectToVault(name: string, data: RegistryProjectVaultPayload) { - if (!hasVaultClient()) { + if (!isVaultClientUsable()) { return; } @@ -72,7 +72,7 @@ export async function saveRegistryProjectToVault(name: string, data: RegistryPro export async function findRegistryProjectInVault( name: string, ) : Promise { - if (!hasVaultClient()) { + if (!isVaultClientUsable()) { return undefined; } diff --git a/packages/server-core/src/domains/robot/vault.ts b/packages/server-core/src/domains/robot/vault.ts index da55e0562..c403acc63 100644 --- a/packages/server-core/src/domains/robot/vault.ts +++ b/packages/server-core/src/domains/robot/vault.ts @@ -8,14 +8,14 @@ import { isClientErrorWithStatusCode, } from 'hapic'; -import { hasVaultClient, useVaultClient } from '../../core'; +import { isVaultClientUsable, useVaultClient } from '@privateaim/server-kit'; import type { RobotVaultPayload } from './type'; import { isRobotVaultPayload } from './utils'; export async function findRobotCredentialsInVault( name: string, ) : Promise { - if (!hasVaultClient()) { + if (!isVaultClientUsable()) { return undefined; } diff --git a/packages/server-core/src/http/controllers/core/analysis/handlers/create.ts b/packages/server-core/src/http/controllers/core/analysis/handlers/create.ts index a5c5239e4..6c972f6f3 100644 --- a/packages/server-core/src/http/controllers/core/analysis/handlers/create.ts +++ b/packages/server-core/src/http/controllers/core/analysis/handlers/create.ts @@ -13,7 +13,7 @@ import type { Request, Response } from 'routup'; import { sendCreated } from 'routup'; import { useDataSource } from 'typeorm-extension'; import { useRequestEnv } from '@privateaim/server-http-kit'; -import { hasAmqpClient, useAmqpClient } from '../../../../../core'; +import { isAmqpClientUsable, useAmqpClient } from '@privateaim/server-kit'; import { runAnalysisValidation } from '../utils'; import { AnalysisEntity, ProjectEntity } from '../../../../../domains'; @@ -49,7 +49,7 @@ export async function createAnalysisRouteHandler(req: Request, res: Response) : const proposalRepository = dataSource.getRepository(ProjectEntity); await proposalRepository.save(result.relation.project); - if (hasAmqpClient()) { + if (isAmqpClientUsable()) { const message = buildCoreQueuePayload({ command: CoreCommand.CONFIGURE, data: { diff --git a/packages/server-core/src/http/controllers/core/analysis/handlers/delete.ts b/packages/server-core/src/http/controllers/core/analysis/handlers/delete.ts index 26a7f3b8f..2adb15049 100644 --- a/packages/server-core/src/http/controllers/core/analysis/handlers/delete.ts +++ b/packages/server-core/src/http/controllers/core/analysis/handlers/delete.ts @@ -13,7 +13,7 @@ import type { Request, Response } from 'routup'; import { sendAccepted, useRequestParam } from 'routup'; import { useDataSource } from 'typeorm-extension'; import { useRequestEnv } from '@privateaim/server-http-kit'; -import { hasAmqpClient, useAmqpClient } from '../../../../../core'; +import { isAmqpClientUsable, useAmqpClient } from '@privateaim/server-kit'; import { AnalysisEntity, ProjectEntity } from '../../../../../domains'; export async function deleteAnalysisRouteHandler(req: Request, res: Response) : Promise { @@ -49,7 +49,7 @@ export async function deleteAnalysisRouteHandler(req: Request, res: Response) : const proposalRepository = dataSource.getRepository(ProjectEntity); await proposalRepository.save(project); - if (hasAmqpClient()) { + if (isAmqpClientUsable()) { const message = buildCoreQueuePayload({ command: CoreCommand.DESTROY, data: { diff --git a/packages/server-core/src/http/controllers/core/node/handlers/create.ts b/packages/server-core/src/http/controllers/core/node/handlers/create.ts index 48c149576..e9e82137e 100644 --- a/packages/server-core/src/http/controllers/core/node/handlers/create.ts +++ b/packages/server-core/src/http/controllers/core/node/handlers/create.ts @@ -15,8 +15,8 @@ import type { Request, Response } from 'routup'; import { sendCreated } from 'routup'; import { useDataSource } from 'typeorm-extension'; import { useRequestEnv } from '@privateaim/server-http-kit'; +import { isAmqpClientUsable, useAmqpClient } from '@privateaim/server-kit'; import { RegistryCommand, buildRegistryPayload } from '../../../../../components'; -import { hasAmqpClient, useAmqpClient } from '../../../../../core'; import { createNodeRobot, runNodeValidation } from '../utils'; import { NodeEntity, RegistryEntity, RegistryProjectEntity } from '../../../../../domains'; @@ -68,7 +68,7 @@ export async function createNodeRouteHandler(req: Request, res: Response) : Prom entity.registry_project_id = registryProject.id; - if (hasAmqpClient()) { + if (isAmqpClientUsable()) { const client = useAmqpClient(); await client.publish(buildRegistryPayload({ command: RegistryCommand.PROJECT_LINK, diff --git a/packages/server-core/src/http/controllers/core/node/handlers/delete.ts b/packages/server-core/src/http/controllers/core/node/handlers/delete.ts index 96422ae91..43926eb5c 100644 --- a/packages/server-core/src/http/controllers/core/node/handlers/delete.ts +++ b/packages/server-core/src/http/controllers/core/node/handlers/delete.ts @@ -12,8 +12,8 @@ import type { Request, Response } from 'routup'; import { sendAccepted, useRequestParam } from 'routup'; import { useDataSource } from 'typeorm-extension'; import { useRequestEnv } from '@privateaim/server-http-kit'; +import { isAmqpClientUsable, useAmqpClient } from '@privateaim/server-kit'; import { RegistryCommand, buildRegistryPayload } from '../../../../../components'; -import { hasAmqpClient, useAmqpClient } from '../../../../../core'; import { NodeEntity, RegistryProjectEntity } from '../../../../../domains'; import { deleteNodeRobot } from '../utils'; @@ -38,7 +38,7 @@ export async function deleteNodeRouteHandler(req: Request, res: Response) : Prom throw new ForbiddenError('You are not permitted to delete this station.'); } - if (hasAmqpClient()) { + if (isAmqpClientUsable()) { if (entity.registry_project_id) { const registryProjectRepository = dataSource.getRepository(RegistryProjectEntity); diff --git a/packages/server-core/src/http/controllers/core/node/handlers/update.ts b/packages/server-core/src/http/controllers/core/node/handlers/update.ts index 98800739f..ae2baac7e 100644 --- a/packages/server-core/src/http/controllers/core/node/handlers/update.ts +++ b/packages/server-core/src/http/controllers/core/node/handlers/update.ts @@ -14,8 +14,8 @@ import type { Request, Response } from 'routup'; import { sendAccepted, useRequestParam } from 'routup'; import { useDataSource } from 'typeorm-extension'; import { useRequestEnv } from '@privateaim/server-http-kit'; +import { isAmqpClientUsable, useAmqpClient } from '@privateaim/server-kit'; import { RegistryCommand, buildRegistryPayload } from '../../../../../components'; -import { hasAmqpClient, useAmqpClient } from '../../../../../core'; import { createNodeRobot, runNodeValidation } from '../utils'; import { NodeEntity, RegistryProjectEntity } from '../../../../../domains'; @@ -87,7 +87,7 @@ export async function updateNodeRouteHandler(req: Request, res: Response) : Prom entity.registry_project_id = registryProject.id; entity.external_name = registryProjectExternalName; - if (hasAmqpClient()) { + if (isAmqpClientUsable()) { const client = useAmqpClient(); if (registryOperation === 'link') { await client.publish(buildRegistryPayload({ diff --git a/packages/server-core/src/http/controllers/core/node/utils/robot.ts b/packages/server-core/src/http/controllers/core/node/utils/robot.ts index 67108fd89..486056077 100644 --- a/packages/server-core/src/http/controllers/core/node/utils/robot.ts +++ b/packages/server-core/src/http/controllers/core/node/utils/robot.ts @@ -7,11 +7,11 @@ import type { Robot } from '@authup/core-kit'; import { isClientErrorWithStatusCode } from 'hapic'; -import { hasAuthupClient, useAuthupClient } from '../../../../../core'; +import { isAuthupClientUsable, useAuthupClient } from '@privateaim/server-kit'; import type { NodeEntity } from '../../../../../domains'; export async function createNodeRobot(entity: NodeEntity) : Promise { - if (!hasAuthupClient()) { + if (!isAuthupClientUsable()) { return; } @@ -39,7 +39,7 @@ export async function createNodeRobot(entity: NodeEntity) : Promise { } export async function deleteNodeRobot(entity: NodeEntity) : Promise { - if (!hasAuthupClient() || !entity.robot_id) { + if (!isAuthupClientUsable() || !entity.robot_id) { return; } diff --git a/packages/server-core/src/http/controllers/core/registry-project/handlers/create.ts b/packages/server-core/src/http/controllers/core/registry-project/handlers/create.ts index 18f3a13f8..80a078c61 100644 --- a/packages/server-core/src/http/controllers/core/registry-project/handlers/create.ts +++ b/packages/server-core/src/http/controllers/core/registry-project/handlers/create.ts @@ -11,8 +11,8 @@ import type { Request, Response } from 'routup'; import { sendCreated } from 'routup'; import { useDataSource } from 'typeorm-extension'; import { useRequestEnv } from '@privateaim/server-http-kit'; +import { isAmqpClientUsable, useAmqpClient } from '@privateaim/server-kit'; import { RegistryCommand, buildRegistryPayload } from '../../../../../components'; -import { hasAmqpClient, useAmqpClient } from '../../../../../core'; import { runRegistryProjectValidation } from '../utils'; import { RegistryProjectEntity } from '../../../../../domains'; @@ -30,7 +30,7 @@ export async function createRegistryProjectRouteHandler(req: Request, res: Respo await repository.save(entity); - if (hasAmqpClient()) { + if (isAmqpClientUsable()) { const client = useAmqpClient(); await client.publish(buildRegistryPayload({ command: RegistryCommand.PROJECT_LINK, diff --git a/packages/server-core/src/http/controllers/core/registry-project/handlers/delete.ts b/packages/server-core/src/http/controllers/core/registry-project/handlers/delete.ts index 7e05a440f..105ada5c1 100644 --- a/packages/server-core/src/http/controllers/core/registry-project/handlers/delete.ts +++ b/packages/server-core/src/http/controllers/core/registry-project/handlers/delete.ts @@ -12,8 +12,8 @@ import { sendAccepted, useRequestParam } from 'routup'; import { useDataSource } from 'typeorm-extension'; import { isRealmResourceWritable } from '@authup/core-kit'; import { useRequestEnv } from '@privateaim/server-http-kit'; +import { isAmqpClientUsable, useAmqpClient } from '@privateaim/server-kit'; import { RegistryCommand, buildRegistryPayload } from '../../../../../components'; -import { hasAmqpClient, useAmqpClient } from '../../../../../core'; import { RegistryProjectEntity } from '../../../../../domains'; export async function deleteRegistryProjectRouteHandler(req: Request, res: Response) : Promise { @@ -43,7 +43,7 @@ export async function deleteRegistryProjectRouteHandler(req: Request, res: Respo entity.id = entityId; - if (hasAmqpClient()) { + if (isAmqpClientUsable()) { const client = useAmqpClient(); await client.publish(buildRegistryPayload({ command: RegistryCommand.PROJECT_UNLINK, diff --git a/packages/server-core/src/http/controllers/core/registry-project/handlers/update.ts b/packages/server-core/src/http/controllers/core/registry-project/handlers/update.ts index e0febc289..94c1733c5 100644 --- a/packages/server-core/src/http/controllers/core/registry-project/handlers/update.ts +++ b/packages/server-core/src/http/controllers/core/registry-project/handlers/update.ts @@ -12,8 +12,8 @@ import { sendAccepted, useRequestParam } from 'routup'; import { useDataSource } from 'typeorm-extension'; import { isRealmResourceWritable } from '@authup/core-kit'; import { useRequestEnv } from '@privateaim/server-http-kit'; +import { isAmqpClientUsable, useAmqpClient } from '@privateaim/server-kit'; import { RegistryCommand, buildRegistryPayload } from '../../../../../components'; -import { hasAmqpClient, useAmqpClient } from '../../../../../core'; import { runRegistryProjectValidation } from '../utils'; import { RegistryProjectEntity } from '../../../../../domains'; @@ -46,13 +46,14 @@ export async function updateRegistryProjectRouteHandler(req: Request, res: Respo await repository.save(entity); - if (hasAmqpClient()) { + if (isAmqpClientUsable()) { + const client = useAmqpClient(); + if ( entity.external_name && result.data.external_name && entity.external_name !== result.data.external_name ) { - const client = useAmqpClient(); await client.publish(buildRegistryPayload({ command: RegistryCommand.PROJECT_UNLINK, data: { @@ -63,10 +64,7 @@ export async function updateRegistryProjectRouteHandler(req: Request, res: Respo }, })); } - } - if (hasAmqpClient()) { - const client = useAmqpClient(); await client.publish(buildRegistryPayload({ command: RegistryCommand.PROJECT_LINK, data: { diff --git a/packages/server-core/src/http/controllers/special/service/handlers/registry/command.ts b/packages/server-core/src/http/controllers/special/service/handlers/registry/command.ts index 35092ae55..1ac00cb99 100644 --- a/packages/server-core/src/http/controllers/special/service/handlers/registry/command.ts +++ b/packages/server-core/src/http/controllers/special/service/handlers/registry/command.ts @@ -15,12 +15,11 @@ import type { Request, Response } from 'routup'; import { sendAccepted } from 'routup'; import { useDataSource } from 'typeorm-extension'; import { useRequestEnv } from '@privateaim/server-http-kit'; -import { useLogger } from '@privateaim/server-kit'; +import { isAmqpClientUsable, useAmqpClient, useLogger } from '@privateaim/server-kit'; import { RegistryCommand, buildRegistryPayload, } from '../../../../../../components'; -import { hasAmqpClient, useAmqpClient } from '../../../../../../core'; import { RegistryEntity, RegistryProjectEntity } from '../../../../../../domains'; import { runServiceRegistryValidation } from '../../utils/validation'; @@ -33,7 +32,7 @@ export async function handleRegistryCommandRouteHandler(req: Request, res: Respo const { data: result } = await runServiceRegistryValidation(req); - if (!hasAmqpClient()) { + if (!isAmqpClientUsable()) { return sendAccepted(res); } diff --git a/packages/server-core/src/http/controllers/special/service/handlers/registry/hook.ts b/packages/server-core/src/http/controllers/special/service/handlers/registry/hook.ts index b94b463bd..4514bc443 100644 --- a/packages/server-core/src/http/controllers/special/service/handlers/registry/hook.ts +++ b/packages/server-core/src/http/controllers/special/service/handlers/registry/hook.ts @@ -8,9 +8,8 @@ import { useRequestBody } from '@routup/basic/body'; import type { Request, Response } from 'routup'; import { sendAccepted } from 'routup'; -import { useLogger } from '@privateaim/server-kit'; +import { isAmqpClientUsable, useAmqpClient, useLogger } from '@privateaim/server-kit'; import { RegistryCommand, RegistryHookSchema, buildRegistryPayload } from '../../../../../../components'; -import { hasAmqpClient, useAmqpClient } from '../../../../../../core'; export async function postHarborHookRouteHandler(req: Request, res: Response) : Promise { const body = useRequestBody(req); @@ -18,7 +17,7 @@ export async function postHarborHookRouteHandler(req: Request, res: Response) : const validation = await RegistryHookSchema.safeParseAsync(body); if (validation.success === false) { useLogger().warn('The registry hook has a malformed shape.', { error: validation.error }); - } else if (hasAmqpClient()) { + } else if (isAmqpClientUsable()) { const client = useAmqpClient(); await client.publish(buildRegistryPayload({ command: RegistryCommand.EVENT_HANDLE, diff --git a/packages/server-core/src/http/router.ts b/packages/server-core/src/http/router.ts index a6dc647a8..9dc0b2c51 100644 --- a/packages/server-core/src/http/router.ts +++ b/packages/server-core/src/http/router.ts @@ -5,18 +5,19 @@ * view the LICENSE file that was distributed with this source code. */ -import { isRedisClientUsable, useRedisClient } from '@privateaim/server-kit'; +import { + isAuthupClientUsable, + isRedisClientUsable, + isVaultClientUsable, + useAuthupClient, + useRedisClient, + useVaultClient, +} from '@privateaim/server-kit'; import { mountErrorMiddleware, mountMiddlewares } from '@privateaim/server-http-kit'; import type { MiddlewareSwaggerOptions } from '@privateaim/server-http-kit'; import { Router } from 'routup'; import { EnvironmentName, useEnv } from '../config'; -import { - hasAuthupClient, - hasVaultClient, - useAuthupClient, - useVaultClient, -} from '../core'; import { AnalysisController } from './controllers/core/analysis'; import { AnalysisFileController } from './controllers/core/analysis-file'; import { AnalysisLogController } from './controllers/core/analysis-log'; @@ -51,10 +52,10 @@ export function createRouter() : Router { prometheus: !isTestEnvironment, rateLimit: !isTestEnvironment, authup: { - client: hasAuthupClient() ? + client: isAuthupClientUsable() ? useAuthupClient() : undefined, - vaultClient: hasVaultClient() ? + vaultClient: isVaultClientUsable() ? useVaultClient() : undefined, redisClient: isRedisClientUsable() ? diff --git a/packages/server-kit/package.json b/packages/server-kit/package.json index 02bbbd939..28b33e150 100644 --- a/packages/server-kit/package.json +++ b/packages/server-kit/package.json @@ -35,12 +35,16 @@ "winston": "^3.13.0" }, "devDependencies": { + "@authup/core-http-kit": "^1.0.0-beta.13", "@hapic/vault": "^2.3.3", + "amqp-extension": "^3.3.0", "redis-extension": "^1.5.0", "typeorm-extension": "^3.5.1" }, "peerDependencies": { + "@authup/core-http-kit": "^1.0.0-beta.13", "@hapic/vault": "^2.3.3", + "amqp-extension": "^3.3.0", "redis-extension": "^1.5.0", "typeorm-extension": "^3.5.1" }, diff --git a/packages/server-analysis-manager/src/core/amqp/index.ts b/packages/server-kit/src/services/amqp/index.ts similarity index 100% rename from packages/server-analysis-manager/src/core/amqp/index.ts rename to packages/server-kit/src/services/amqp/index.ts diff --git a/packages/server-analysis-manager/src/core/amqp/module.ts b/packages/server-kit/src/services/amqp/module.ts similarity index 82% rename from packages/server-analysis-manager/src/core/amqp/module.ts rename to packages/server-kit/src/services/amqp/module.ts index 2833b5bbb..20691c12c 100644 --- a/packages/server-analysis-manager/src/core/amqp/module.ts +++ b/packages/server-kit/src/services/amqp/module.ts @@ -12,11 +12,12 @@ import { singa } from 'singa'; const instance = singa({ name: 'amqp', }); -export function setAmqpFactory(factory: Factory) { + +export function setAmqpClientFactory(factory: Factory) { instance.setFactory(factory); } -export function hasAmqpClient() { +export function isAmqpClientUsable() { return instance.has() || instance.hasFactory(); } diff --git a/packages/server-kit/src/services/authup/helpers.ts b/packages/server-kit/src/services/authup/helpers.ts new file mode 100644 index 000000000..0d4d0d08a --- /dev/null +++ b/packages/server-kit/src/services/authup/helpers.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024. + * Author Peter Placzek (tada5hi) + * For the full copyright and license information, + * view the LICENSE file that was distributed with this source code. + */ + +import type { TokenCreatorOptions } from '@authup/core-http-kit'; +import { isVaultClientUsable, useVaultClient } from '../vault'; + +export function guessAuthupTokenCreatorOptions() : TokenCreatorOptions { + let options : TokenCreatorOptions; + if (isVaultClientUsable()) { + options = { + type: 'robotInVault', + name: 'system', + vault: useVaultClient(), + }; + } else { + options = { + type: 'user', + name: 'admin', + password: 'start123', + }; + } + + return options; +} diff --git a/packages/server-core/src/core/amqp/index.ts b/packages/server-kit/src/services/authup/index.ts similarity index 79% rename from packages/server-core/src/core/amqp/index.ts rename to packages/server-kit/src/services/authup/index.ts index 7424f1649..aebefcf8a 100644 --- a/packages/server-core/src/core/amqp/index.ts +++ b/packages/server-kit/src/services/authup/index.ts @@ -5,4 +5,6 @@ * view the LICENSE file that was distributed with this source code. */ +export * from './helpers'; export * from './module'; +export * from './singleton'; diff --git a/packages/server-kit/src/services/authup/module.ts b/packages/server-kit/src/services/authup/module.ts new file mode 100644 index 000000000..a6d2b902a --- /dev/null +++ b/packages/server-kit/src/services/authup/module.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024. + * Author Peter Placzek (tada5hi) + * For the full copyright and license information, + * view the LICENSE file that was distributed with this source code. + */ + +import type { ClientOptions } from '@authup/core-http-kit'; +import { + Client, mountClientResponseErrorTokenHook as mountAuthupClientResponseErrorTokenHook, + mountClientResponseErrorTokenHook, +} from '@authup/core-http-kit'; +import { guessAuthupTokenCreatorOptions } from './helpers'; + +export { + mountAuthupClientResponseErrorTokenHook, +}; + +export class AuthupClient extends Client { + constructor(options: ClientOptions = {}) { + super(options); + + mountClientResponseErrorTokenHook(this, { + baseURL: options.baseURL, + tokenCreator: guessAuthupTokenCreatorOptions(), + }); + } +} diff --git a/packages/server-core/src/core/authup/module.ts b/packages/server-kit/src/services/authup/singleton.ts similarity index 83% rename from packages/server-core/src/core/authup/module.ts rename to packages/server-kit/src/services/authup/singleton.ts index 2ffae167a..bfac6f006 100644 --- a/packages/server-core/src/core/authup/module.ts +++ b/packages/server-kit/src/services/authup/singleton.ts @@ -17,10 +17,10 @@ export function useAuthupClient() { return instance.use(); } -export function hasAuthupClient() { +export function isAuthupClientUsable() { return instance.has() || instance.hasFactory(); } -export function setAuthupFactory(factory: Factory) { +export function setAuthupClientFactory(factory: Factory) { instance.setFactory(factory); } diff --git a/packages/server-kit/src/services/index.ts b/packages/server-kit/src/services/index.ts index 864d9711c..f1f1f01f3 100644 --- a/packages/server-kit/src/services/index.ts +++ b/packages/server-kit/src/services/index.ts @@ -5,6 +5,8 @@ * view the LICENSE file that was distributed with this source code. */ +export * from './amqp'; +export * from './authup'; export * from './logger'; export * from './redis'; export * from './vault'; diff --git a/packages/server-realtime/src/config/constants.ts b/packages/server-realtime/src/config/constants.ts new file mode 100644 index 000000000..5ede1bb99 --- /dev/null +++ b/packages/server-realtime/src/config/constants.ts @@ -0,0 +1,11 @@ +/* + * Copyright (c) 2021-2024. + * Author Peter Placzek (tada5hi) + * For the full copyright and license information, + * view the LICENSE file that was distributed with this source code. + */ + +import path from 'node:path'; +import process from 'node:process'; + +export const WRITABLE_DIR_PATH = path.join(process.cwd(), 'writable'); diff --git a/packages/server-realtime/src/config/index.ts b/packages/server-realtime/src/config/index.ts index 0e9f82392..f330e9162 100644 --- a/packages/server-realtime/src/config/index.ts +++ b/packages/server-realtime/src/config/index.ts @@ -7,5 +7,5 @@ export * from './env'; export * from './module'; -export * from './paths'; +export * from './constants'; export * from './type'; diff --git a/packages/server-realtime/src/config/module.ts b/packages/server-realtime/src/config/module.ts index 7e1e3f8da..c53655430 100644 --- a/packages/server-realtime/src/config/module.ts +++ b/packages/server-realtime/src/config/module.ts @@ -5,20 +5,21 @@ * view the LICENSE file that was distributed with this source code. */ -import { setConfig as setRedisConfig, useClient as useRedisClient } from 'redis-extension'; -import { useEnv } from './env'; +import { useRedisClient, useRedisPublishClient, useRedisSubscribeClient } from '@privateaim/server-kit'; +import { WRITABLE_DIR_PATH } from './constants'; +import { setupLogger, setupRedis } from './services'; import type { Config } from './type'; export function createConfig() : Config { - setRedisConfig({ connectionString: useEnv('redisConnectionString') }); + setupLogger({ + directory: WRITABLE_DIR_PATH, + }); - const redisDatabase = useRedisClient(); - const redisPub = redisDatabase.duplicate(); - const redisSub = redisDatabase.duplicate(); + setupRedis(); return { - redisDatabase, - redisPub, - redisSub, + redisDatabase: useRedisClient(), + redisPub: useRedisPublishClient(), + redisSub: useRedisSubscribeClient(), }; } diff --git a/packages/server-realtime/src/config/paths.ts b/packages/server-realtime/src/config/paths.ts deleted file mode 100644 index 6252f4dbd..000000000 --- a/packages/server-realtime/src/config/paths.ts +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (c) 2021-2024. - * Author Peter Placzek (tada5hi) - * For the full copyright and license information, - * view the LICENSE file that was distributed with this source code. - */ - -import path from 'node:path'; - -let writableDirPath : string | undefined; - -export function getWritableDirPath() { - if (typeof writableDirPath !== 'undefined') { - return writableDirPath; - } - - writableDirPath = path.resolve(`${__dirname}../../../writable`); - return writableDirPath; -} diff --git a/packages/server-realtime/src/core/index.ts b/packages/server-realtime/src/config/services/index.ts similarity index 79% rename from packages/server-realtime/src/core/index.ts rename to packages/server-realtime/src/config/services/index.ts index 0768c5588..7a50e6bce 100644 --- a/packages/server-realtime/src/core/index.ts +++ b/packages/server-realtime/src/config/services/index.ts @@ -1,8 +1,9 @@ /* - * Copyright (c) 2023-2024. + * Copyright (c) 2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ export * from './logger'; +export * from './redis'; diff --git a/packages/server-realtime/src/config/services/logger.ts b/packages/server-realtime/src/config/services/logger.ts new file mode 100644 index 000000000..9dcb2c816 --- /dev/null +++ b/packages/server-realtime/src/config/services/logger.ts @@ -0,0 +1,13 @@ +/* + * Copyright (c) 2024. + * Author Peter Placzek (tada5hi) + * For the full copyright and license information, + * view the LICENSE file that was distributed with this source code. + */ + +import type { LoggerCreateContext } from '@privateaim/server-kit'; +import { createLogger, setLoggerFactory } from '@privateaim/server-kit'; + +export function setupLogger(ctx: LoggerCreateContext): void { + setLoggerFactory(() => createLogger(ctx)); +} diff --git a/packages/server-realtime/src/config/services/redis.ts b/packages/server-realtime/src/config/services/redis.ts new file mode 100644 index 000000000..afe388193 --- /dev/null +++ b/packages/server-realtime/src/config/services/redis.ts @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2024. + * Author Peter Placzek (tada5hi) + * For the full copyright and license information, + * view the LICENSE file that was distributed with this source code. + */ + +import { createRedisClient, setRedisFactory } from '@privateaim/server-kit'; +import { useEnv } from '../env'; + +export function setupRedis() { + const connectionString = useEnv('redisConnectionString'); + if (typeof connectionString !== 'undefined') { + setRedisFactory(() => createRedisClient({ + connectionString, + })); + } +} diff --git a/packages/server-realtime/src/core/logger/index.ts b/packages/server-realtime/src/core/logger/index.ts deleted file mode 100644 index 5c76199d9..000000000 --- a/packages/server-realtime/src/core/logger/index.ts +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2021-2024. - * Author Peter Placzek (tada5hi) - * For the full copyright and license information, - * view the LICENSE file that was distributed with this source code. - */ - -import type { Logger } from '@privateaim/server-kit'; -import { createLogger } from '@privateaim/server-kit'; -import { getWritableDirPath } from '../../config'; - -let logger : undefined | any; - -/* -Levels -{ - error: 0, - warn: 1, - info: 2, - http: 3, - verbose: 4, - debug: 5, - silly: 6 -} - */ - -export function useLogger() : Logger { - if (typeof logger !== 'undefined') { - return logger; - } - - logger = createLogger({ - directory: getWritableDirPath(), - }); - - return logger; -} diff --git a/packages/server-realtime/src/http/server.ts b/packages/server-realtime/src/http/server.ts index 3f4c2d573..15f944887 100644 --- a/packages/server-realtime/src/http/server.ts +++ b/packages/server-realtime/src/http/server.ts @@ -11,6 +11,7 @@ import { Router, coreHandler, createNodeDispatcher } from 'routup'; export function createHttpServer() : Server { const router = new Router(); + router.get('/', coreHandler(() => ({ timestamp: Date.now(), }))); diff --git a/packages/server-realtime/src/index.ts b/packages/server-realtime/src/index.ts index 7975a1488..2a7cb5cc9 100644 --- a/packages/server-realtime/src/index.ts +++ b/packages/server-realtime/src/index.ts @@ -5,7 +5,7 @@ * view the LICENSE file that was distributed with this source code. */ -import { useLogger } from './core'; +import { useLogger } from '@privateaim/server-kit'; import { createConfig, useEnv } from './config'; import { createHttpServer } from './http'; import { createSocketServer } from './socket'; diff --git a/packages/server-realtime/src/socket/namespaces/messages/module.ts b/packages/server-realtime/src/socket/namespaces/messages/module.ts index 53f933889..7ca931d29 100644 --- a/packages/server-realtime/src/socket/namespaces/messages/module.ts +++ b/packages/server-realtime/src/socket/namespaces/messages/module.ts @@ -6,7 +6,7 @@ */ import { UnauthorizedError } from '@ebec/http'; -import { useLogger } from '../../../core'; +import { useLogger } from '@privateaim/server-kit'; import type { SocketNamespaceContext } from '../../types'; import { registerMessagesNamespaceControllers } from './register'; import type { MessagesNamespace } from './types'; diff --git a/packages/server-realtime/src/socket/namespaces/resources/module.ts b/packages/server-realtime/src/socket/namespaces/resources/module.ts index 65d5de45d..881fb6074 100644 --- a/packages/server-realtime/src/socket/namespaces/resources/module.ts +++ b/packages/server-realtime/src/socket/namespaces/resources/module.ts @@ -8,7 +8,7 @@ import { REALM_MASTER_NAME } from '@authup/core-kit'; import { ForbiddenError, UnauthorizedError } from '@ebec/http'; import type { Socket } from '@privateaim/server-realtime-kit'; -import { useLogger } from '../../../core'; +import { useLogger } from '@privateaim/server-kit'; import type { SocketNamespaceContext } from '../../types'; import { registerResourcesNamespaceControllers } from './register'; diff --git a/packages/server-storage/src/config/index.ts b/packages/server-storage/src/config/index.ts index 6b75d3464..40bf7d6ec 100644 --- a/packages/server-storage/src/config/index.ts +++ b/packages/server-storage/src/config/index.ts @@ -7,4 +7,3 @@ export * from './env'; export * from './module'; -export * from './types'; diff --git a/packages/server-storage/src/config/services/authup.ts b/packages/server-storage/src/config/services/authup.ts index 701e67838..7b0cabb5d 100644 --- a/packages/server-storage/src/config/services/authup.ts +++ b/packages/server-storage/src/config/services/authup.ts @@ -5,8 +5,7 @@ * view the LICENSE file that was distributed with this source code. */ -import { Client, mountClientResponseErrorTokenHook } from '@authup/core-http-kit'; -import { setAuthupClientFactory } from '../../core'; +import { AuthupClient, setAuthupClientFactory } from '@privateaim/server-kit'; import { useEnv } from '../env'; export function configureAuthup() { @@ -15,22 +14,7 @@ export function configureAuthup() { return; } - setAuthupClientFactory(() => { - const authupClient = new Client({ - baseURL, - }); - - // todo use vault for token creation - - mountClientResponseErrorTokenHook(authupClient, { - baseURL, - tokenCreator: { - type: 'user', - name: 'admin', - password: 'start123', - }, - }); - - return authupClient; - }); + setAuthupClientFactory(() => new AuthupClient({ + baseURL, + })); } diff --git a/packages/server-storage/src/config/services/redis.ts b/packages/server-storage/src/config/services/redis.ts index 0f11a4e13..20c2d3659 100644 --- a/packages/server-storage/src/config/services/redis.ts +++ b/packages/server-storage/src/config/services/redis.ts @@ -5,8 +5,7 @@ * view the LICENSE file that was distributed with this source code. */ -import { createClient } from 'redis-extension'; -import { setRedisFactory } from '../../core'; +import { createRedisClient, setRedisFactory } from '@privateaim/server-kit'; import { useEnv } from '../env'; export function configureRedis() { @@ -16,7 +15,7 @@ export function configureRedis() { return; } - setRedisFactory(() => createClient({ + setRedisFactory(() => createRedisClient({ connectionString, })); } diff --git a/packages/server-storage/src/config/services/vault.ts b/packages/server-storage/src/config/services/vault.ts index cd8e27259..eec3d6ce0 100644 --- a/packages/server-storage/src/config/services/vault.ts +++ b/packages/server-storage/src/config/services/vault.ts @@ -5,8 +5,7 @@ * view the LICENSE file that was distributed with this source code. */ -import { VaultClient } from '@hapic/vault'; -import { setVaultFactory } from '../../core'; +import { VaultClient, setVaultFactory } from '@privateaim/server-kit'; import { useEnv } from '../env'; export function configureVault() { diff --git a/packages/server-storage/src/core/authup/index.ts b/packages/server-storage/src/core/authup/index.ts deleted file mode 100644 index 7424f1649..000000000 --- a/packages/server-storage/src/core/authup/index.ts +++ /dev/null @@ -1,8 +0,0 @@ -/* - * Copyright (c) 2024. - * Author Peter Placzek (tada5hi) - * For the full copyright and license information, - * view the LICENSE file that was distributed with this source code. - */ - -export * from './module'; diff --git a/packages/server-storage/src/core/authup/module.ts b/packages/server-storage/src/core/authup/module.ts deleted file mode 100644 index 15cffbed7..000000000 --- a/packages/server-storage/src/core/authup/module.ts +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (c) 2024. - * Author Peter Placzek (tada5hi) - * For the full copyright and license information, - * view the LICENSE file that was distributed with this source code. - */ - -import type { Client } from '@authup/core-http-kit'; -import type { Factory } from 'singa'; -import { singa } from 'singa'; - -const singleton = singa({ - name: 'authup', -}); - -export function useAuthupClient() { - return singleton.use(); -} - -export function hasAuthupClient() { - return singleton.has() || singleton.hasFactory(); -} - -export function setAuthupClientFactory(factory: Factory) { - return singleton.setFactory(factory); -} diff --git a/packages/server-storage/src/core/index.ts b/packages/server-storage/src/core/index.ts index 9aca82d88..49bb64d56 100644 --- a/packages/server-storage/src/core/index.ts +++ b/packages/server-storage/src/core/index.ts @@ -5,8 +5,5 @@ * view the LICENSE file that was distributed with this source code. */ -export * from './authup'; export * from './minio'; -export * from './redis'; export * from './utils'; -export * from './vault'; diff --git a/packages/server-storage/src/core/redis/index.ts b/packages/server-storage/src/core/redis/index.ts deleted file mode 100644 index 7424f1649..000000000 --- a/packages/server-storage/src/core/redis/index.ts +++ /dev/null @@ -1,8 +0,0 @@ -/* - * Copyright (c) 2024. - * Author Peter Placzek (tada5hi) - * For the full copyright and license information, - * view the LICENSE file that was distributed with this source code. - */ - -export * from './module'; diff --git a/packages/server-storage/src/core/redis/module.ts b/packages/server-storage/src/core/redis/module.ts deleted file mode 100644 index 2aa34832f..000000000 --- a/packages/server-storage/src/core/redis/module.ts +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (c) 2024. - * Author Peter Placzek (tada5hi) - * For the full copyright and license information, - * view the LICENSE file that was distributed with this source code. - */ - -import type { Client } from 'redis-extension'; -import type { Factory } from 'singa'; -import { singa } from 'singa'; - -const singleton = singa({ - name: 'redis', -}); - -export function useRedis() { - return singleton.use(); -} - -export function hasRedis() { - return singleton.has() || singleton.hasFactory(); -} - -export function setRedisFactory(factory: Factory) { - return singleton.setFactory(factory); -} diff --git a/packages/server-storage/src/core/vault/index.ts b/packages/server-storage/src/core/vault/index.ts deleted file mode 100644 index 7424f1649..000000000 --- a/packages/server-storage/src/core/vault/index.ts +++ /dev/null @@ -1,8 +0,0 @@ -/* - * Copyright (c) 2024. - * Author Peter Placzek (tada5hi) - * For the full copyright and license information, - * view the LICENSE file that was distributed with this source code. - */ - -export * from './module'; diff --git a/packages/server-storage/src/core/vault/module.ts b/packages/server-storage/src/core/vault/module.ts deleted file mode 100644 index f926a7b8d..000000000 --- a/packages/server-storage/src/core/vault/module.ts +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (c) 2024. - * Author Peter Placzek (tada5hi) - * For the full copyright and license information, - * view the LICENSE file that was distributed with this source code. - */ - -import type { Factory } from 'singa'; -import { singa } from 'singa'; -import type { VaultClient } from '@hapic/vault'; - -const instance = singa({ - name: 'vault', -}); - -export function setVaultFactory(factory: Factory) { - instance.setFactory(factory); -} - -export function hasVaultClient() { - return instance.has() || instance.hasFactory(); -} - -export function useVaultClient() { - return instance.use(); -} diff --git a/packages/server-storage/src/http/router.ts b/packages/server-storage/src/http/router.ts index 2ad313e36..1a6cc2444 100644 --- a/packages/server-storage/src/http/router.ts +++ b/packages/server-storage/src/http/router.ts @@ -10,11 +10,16 @@ import { mountErrorMiddleware, mountMiddlewares, } from '@privateaim/server-http-kit'; +import { + isAuthupClientUsable, + isRedisClientUsable, + isVaultClientUsable, + useAuthupClient, + useRedisClient, + useVaultClient, +} from '@privateaim/server-kit'; import { Router, coreHandler } from 'routup'; import { EnvironmentName, useEnv } from '../config'; -import { - hasAuthupClient, hasRedis, hasVaultClient, useAuthupClient, useRedis, useVaultClient, -} from '../core'; import { BucketController, BucketFileController } from './controllers'; export function createHTTPRouter() : Router { @@ -33,14 +38,14 @@ export function createHTTPRouter() : Router { basic: true, cors: true, authup: { - client: hasAuthupClient() ? + client: isAuthupClientUsable() ? useAuthupClient() : undefined, - vaultClient: hasVaultClient() ? + vaultClient: isVaultClientUsable() ? useVaultClient() : undefined, - redisClient: hasRedis() ? - useRedis() : + redisClient: isRedisClientUsable() ? + useRedisClient() : undefined, fakeAbilities: useEnv('env') === EnvironmentName.TEST, },