From ff2df4a7e7bbadcd4c16f85437977412540c923d Mon Sep 17 00:00:00 2001 From: SevenWaysDP Date: Thu, 28 Nov 2024 11:17:41 +0100 Subject: [PATCH 1/6] refactor: update configuration properties for consistency and clarity --- .env.default | 22 ++++++++--------- .env.test | 24 +++++++++---------- .../roles/tldraw-server/defaults/main.yaml | 5 ++++ .../tldraw-server/templates/configmap.yml.j2 | 19 +++++++-------- .../strategy/x-api-key.strategy.spec.ts | 4 ++-- .../auth-guard/strategy/x-api-key.strategy.ts | 2 +- src/infra/auth-guard/x-api-key.config.ts | 2 +- .../authorization/authorization.config.ts | 2 +- .../authorization/authorization.module.ts | 2 +- src/infra/logger/logger.config.ts | 4 ++-- src/infra/logger/logger.module.ts | 4 ++-- src/infra/redis/redis.config.ts | 2 +- src/infra/redis/redis.service.spec.ts | 4 ++-- src/infra/redis/redis.service.ts | 2 +- src/infra/storage/storage.config.ts | 2 +- src/infra/testing/test-api-client.spec.ts | 2 +- .../server/api/dto/tldraw-config.response.ts | 21 ++++++++-------- .../server/api/test/tldraw-config.api.spec.ts | 8 +++---- .../server/api/websocket.gateway.spec.ts | 4 ++-- src/modules/server/api/websocket.gateway.ts | 4 ++-- src/modules/server/server.config.ts | 14 +++++------ 21 files changed, 77 insertions(+), 76 deletions(-) create mode 100644 ansible/roles/tldraw-server/defaults/main.yaml diff --git a/.env.default b/.env.default index 5a6b43aa..b945d1d7 100644 --- a/.env.default +++ b/.env.default @@ -1,17 +1,17 @@ -REDIS=redis://localhost:6379 -API_HOST=http://localhost:3030 -ADMIN_API__ALLOWED_API_KEYS=randomString +AUTHORIZATION_API_HOST=http://localhost:3030 +LOGGER_LOG_LEVEL=debug + +REDIS_URL=redis://localhost:6379 + +S3_ACCESS_KEY=miniouser +S3_BUCKET=ydocs S3_ENDPOINT=localhost S3_PORT=9000 -S3_SSL=false -S3_ACCESS_KEY=miniouser S3_SECRET_KEY=miniouser +S3_SSL=false -WS_PORT=3345 - -TLDRAW__WEBSOCKET_URL=ws://localhost:3345 -FEATURE_TLDRAW_ENABLED=true +SERVER_FEATURE_TLDRAW_ENABLED=true +SERVER_WEBSOCKET_URL=ws://localhost:3345 -NEST_LOG_LEVEL=debug -EXIT_ON_ERROR=true \ No newline at end of file +X_API_ALLOWED_KEYS=randomString \ No newline at end of file diff --git a/.env.test b/.env.test index 0fa52b85..4c094c37 100644 --- a/.env.test +++ b/.env.test @@ -1,19 +1,17 @@ -API_HOST=http://localhost:3030 -ADMIN_API__ALLOWED_API_KEYS=randomString +AUTHORIZATION_API_HOST=http://localhost:3030 +LOGGER_LOG_LEVEL=error + +REDIS_URL=redis://localhost:6379 + +S3_ACCESS_KEY=miniouser +S3_BUCKET=ydocs S3_ENDPOINT=localhost S3_PORT=9000 -S3_SSL=false -S3_ACCESS_KEY=miniouser S3_SECRET_KEY=miniouser +S3_SSL=false -WS_PORT=3345 - -TLDRAW__WEBSOCKET_URL=ws://localhost:3345 -TLDRAW__ASSETS_ENABLED=true -TLDRAW__ASSETS_MAX_SIZE_BYTES=10485760 -TLDRAW__ASSETS_ALLOWED_MIME_TYPES_LIST="image/png,image/jpeg,image/gif,image/svg+xml" -FEATURE_TLDRAW_ENABLED=true +SERVER_FEATURE_TLDRAW_ENABLED=true +SERVER_WEBSOCKET_URL=ws://localhost:3345 -NEST_LOG_LEVEL=debug -EXIT_ON_ERROR=true +X_API_ALLOWED_KEYS=randomString \ No newline at end of file diff --git a/ansible/roles/tldraw-server/defaults/main.yaml b/ansible/roles/tldraw-server/defaults/main.yaml new file mode 100644 index 00000000..7ad16b13 --- /dev/null +++ b/ansible/roles/tldraw-server/defaults/main.yaml @@ -0,0 +1,5 @@ +TLDRAW_ASSETS_ALLOWED_MIME_TYPES_LIST : "image/png,image/jpeg,image/gif,image/svg+xml" +TLDRAW_LOG_LEVEL: "error" +TLDRAW_METRICS_COLLECT_DEFAULT: "true" +TLDRAW_SERVER_ASSETS_ENABLED: "true" +TLDRAW_WEBSOCKET_PATH: "/tldraw-server" \ No newline at end of file diff --git a/ansible/roles/tldraw-server/templates/configmap.yml.j2 b/ansible/roles/tldraw-server/templates/configmap.yml.j2 index 856f6bf6..5fe7ce81 100644 --- a/ansible/roles/tldraw-server/templates/configmap.yml.j2 +++ b/ansible/roles/tldraw-server/templates/configmap.yml.j2 @@ -6,16 +6,13 @@ metadata: labels: app: tldraw-server data: - API_HOST: "http://api-svc:3030" - WS_PATH_PREFIX: "/tldraw-server" - WS_PORT: "3345" - FEATURE_PROMETHEUS_METRICS_ENABLED: "true" + AUTHORIZATION_API_HOST: {{ API_HOST }} + LOGGER_LOG_LEVEL: {{ TLDRAW_LOG_LEVEL }} + METRICS_COLLECT_DEFAULT: {{ TLDRAW_METRICS_COLLECT_DEFAULT }} REDIS_CLUSTER_ENABLED: "true" REDIS_SENTINEL_SERVICE_NAME: "valkey-headless.{{ NAMESPACE }}.svc.cluster.local" - TLDRAW__WEBSOCKET_URL: "wss://{{ DOMAIN }}/tldraw-server" - TLDRAW__ASSETS_ENABLED: "true" - TLDRAW__ASSETS_MAX_SIZE_BYTES: "10485760" - TLDRAW__ASSETS_ALLOWED_MIME_TYPES_LIST: "image/png,image/jpeg,image/gif,image/svg+xml" - FEATURE_TLDRAW_ENABLED: "true" - NEST_LOG_LEVEL: "info" - EXIT_ON_ERROR: "true" \ No newline at end of file + SERVER_ASSETS_ALLOWED_MIME_TYPES_LIST: "{{ TLDRAW_ASSETS_ALLOWED_MIME_TYPES_LIST }}" + SERVER_ASSETS_ENABLED: {{ TLDRAW_SERVER_ASSETS_ENABLED }} + SERVER_FEATURE_TLDRAW_ENABLED: {{ FEATURE_TLDRAW_ENABLED }} + SERVER_WEBSOCKET_PATH: "{{ TLDRAW_WEBSOCKET_PATH }}" + SERVER_WEBSOCKET_URL: "wss://{{ DOMAIN }}{{ TLDRAW_WEBSOCKET_PATH }}" diff --git a/src/infra/auth-guard/strategy/x-api-key.strategy.spec.ts b/src/infra/auth-guard/strategy/x-api-key.strategy.spec.ts index 7258545e..44047561 100644 --- a/src/infra/auth-guard/strategy/x-api-key.strategy.spec.ts +++ b/src/infra/auth-guard/strategy/x-api-key.strategy.spec.ts @@ -38,7 +38,7 @@ describe('XApiKeyStrategy', () => { describe('when a valid api key is provided', () => { const setup = () => { const CORRECT_API_KEY = '7ccd4e11-c6f6-48b0-81eb-cccf7922e7a4'; - config.ADMIN_API__ALLOWED_API_KEYS = [CORRECT_API_KEY]; + config.X_API_ALLOWED_KEYS = [CORRECT_API_KEY]; return { CORRECT_API_KEY, done }; }; @@ -52,7 +52,7 @@ describe('XApiKeyStrategy', () => { describe('when a invalid api key is provided', () => { const setup = () => { const INVALID_API_KEY = '7ccd4e11-c6f6-48b0-81eb-cccf7922e7a4BAD'; - config.ADMIN_API__ALLOWED_API_KEYS = [INVALID_API_KEY]; + config.X_API_ALLOWED_KEYS = [INVALID_API_KEY]; return { INVALID_API_KEY, done }; }; diff --git a/src/infra/auth-guard/strategy/x-api-key.strategy.ts b/src/infra/auth-guard/strategy/x-api-key.strategy.ts index 1fb5f85c..b984709c 100644 --- a/src/infra/auth-guard/strategy/x-api-key.strategy.ts +++ b/src/infra/auth-guard/strategy/x-api-key.strategy.ts @@ -10,7 +10,7 @@ export class XApiKeyStrategy extends PassportStrategy(Strategy, StrategyType.API public constructor(private readonly config: XApiKeyConfig) { super({ header: 'X-API-KEY' }, false); - this.allowedApiKeys = this.config.ADMIN_API__ALLOWED_API_KEYS; + this.allowedApiKeys = this.config.X_API_ALLOWED_KEYS; } public validate(apiKey: string, done: (error: Error | null, data: boolean | null) => void): void { diff --git a/src/infra/auth-guard/x-api-key.config.ts b/src/infra/auth-guard/x-api-key.config.ts index c8628096..2b2fc113 100644 --- a/src/infra/auth-guard/x-api-key.config.ts +++ b/src/infra/auth-guard/x-api-key.config.ts @@ -4,5 +4,5 @@ import { IsArray } from 'class-validator'; export class XApiKeyConfig { @Transform(({ value }) => value.split(',').map((part: string) => (part.split(':').pop() ?? '').trim())) @IsArray() - public ADMIN_API__ALLOWED_API_KEYS!: string[]; + public X_API_ALLOWED_KEYS!: string[]; } diff --git a/src/infra/authorization/authorization.config.ts b/src/infra/authorization/authorization.config.ts index 0e8cf560..d7cd9607 100644 --- a/src/infra/authorization/authorization.config.ts +++ b/src/infra/authorization/authorization.config.ts @@ -2,5 +2,5 @@ import { IsUrl } from 'class-validator'; export class AuthorizationConfig { @IsUrl({ require_tld: false }) - public API_HOST!: string; + public AUTHORIZATION_API_HOST!: string; } diff --git a/src/infra/authorization/authorization.module.ts b/src/infra/authorization/authorization.module.ts index 14b3ab0d..cb36be02 100644 --- a/src/infra/authorization/authorization.module.ts +++ b/src/infra/authorization/authorization.module.ts @@ -11,7 +11,7 @@ import { AuthorizationService } from './authorization.service.js'; { provide: AuthorizationApi, useFactory: (config: AuthorizationConfig): AuthorizationApi => { - const apiHost = config.API_HOST; + const apiHost = config.AUTHORIZATION_API_HOST; const configuration = new Configuration({ basePath: `${apiHost}/api/v3` }); const authorizationApi = new AuthorizationApi(configuration); diff --git a/src/infra/logger/logger.config.ts b/src/infra/logger/logger.config.ts index 162af355..1ce1e7ac 100644 --- a/src/infra/logger/logger.config.ts +++ b/src/infra/logger/logger.config.ts @@ -14,9 +14,9 @@ export enum LoggerLogLevel { export class LoggerConfig { @IsEnum(LoggerLogLevel) - public NEST_LOG_LEVEL!: LoggerLogLevel; + public LOGGER_LOG_LEVEL!: LoggerLogLevel; @IsBoolean() @Transform(({ value }) => value === 'true') - public EXIT_ON_ERROR!: boolean; + public LOGGER_EXIT_ON_ERROR = true; } diff --git a/src/infra/logger/logger.module.ts b/src/infra/logger/logger.module.ts index 26aa0531..fbf853de 100644 --- a/src/infra/logger/logger.module.ts +++ b/src/infra/logger/logger.module.ts @@ -12,8 +12,8 @@ import { Logger } from './logger.js'; useFactory: (config: LoggerConfig) => { return { levels: winston.config.syslog.levels, - level: config.NEST_LOG_LEVEL, - exitOnError: config.EXIT_ON_ERROR, + level: config.LOGGER_LOG_LEVEL, + exitOnError: config.LOGGER_EXIT_ON_ERROR, transports: [ new winston.transports.Console({ handleExceptions: true, diff --git a/src/infra/redis/redis.config.ts b/src/infra/redis/redis.config.ts index f42995a7..4fee9d5d 100644 --- a/src/infra/redis/redis.config.ts +++ b/src/infra/redis/redis.config.ts @@ -9,7 +9,7 @@ export class RedisConfig { @IsUrl({ protocols: ['redis'], require_tld: false }) @ValidateIf((o: RedisConfig) => !o.REDIS_CLUSTER_ENABLED) - public REDIS!: string; + public REDIS_URL!: string; @IsString() @ValidateIf((o: RedisConfig) => o.REDIS_CLUSTER_ENABLED) diff --git a/src/infra/redis/redis.service.spec.ts b/src/infra/redis/redis.service.spec.ts index baa8d9b4..0c7a1da1 100644 --- a/src/infra/redis/redis.service.spec.ts +++ b/src/infra/redis/redis.service.spec.ts @@ -29,7 +29,7 @@ describe('Redis Service', () => { const config = new RedisConfig(); - config.REDIS = sentinelServiceName; + config.REDIS_URL = sentinelServiceName; config.REDIS_CLUSTER_ENABLED = true; config.REDIS_SENTINEL_SERVICE_NAME = sentinelServiceName; config.REDIS_PREFIX = redisPrefix; @@ -95,7 +95,7 @@ describe('Redis Service', () => { const config = new RedisConfig(); const redisUrl = 'redis://localhost:6379'; - config.REDIS = redisUrl; + config.REDIS_URL = redisUrl; const resolveSrv = jest.fn(); jest.spyOn(util, 'promisify').mockReturnValueOnce(resolveSrv); diff --git a/src/infra/redis/redis.service.ts b/src/infra/redis/redis.service.ts index 25a89afb..095ceba8 100644 --- a/src/infra/redis/redis.service.ts +++ b/src/infra/redis/redis.service.ts @@ -29,7 +29,7 @@ export class RedisService { } private createNewRedisInstance(): Redis { - const redisUrl = this.config.REDIS; + const redisUrl = this.config.REDIS_URL; const redisInstance = new Redis(redisUrl); return redisInstance; diff --git a/src/infra/storage/storage.config.ts b/src/infra/storage/storage.config.ts index b1005cff..68c9bef3 100644 --- a/src/infra/storage/storage.config.ts +++ b/src/infra/storage/storage.config.ts @@ -6,7 +6,7 @@ export class StorageConfig { public S3_ENDPOINT!: string; @IsString() - public S3_BUCKET = 'ydocs'; + public S3_BUCKET!: string; @IsNumber() @Type(() => Number) diff --git a/src/infra/testing/test-api-client.spec.ts b/src/infra/testing/test-api-client.spec.ts index 0b39a72a..966fbd9b 100644 --- a/src/infra/testing/test-api-client.spec.ts +++ b/src/infra/testing/test-api-client.spec.ts @@ -52,7 +52,7 @@ describe(TestApiClient.name, () => { const setup = () => { const id = '60f1b9b3b3b3b3b3b3b3b3b3'; const useAsApiKey = true; - const validApiKey: XApiKeyConfig['ADMIN_API__ALLOWED_API_KEYS'][0] = 'randomString'; + const validApiKey: XApiKeyConfig['X_API_ALLOWED_KEYS'][0] = 'randomString'; const testApiClient = new TestApiClient(app, baseRoute, validApiKey, useAsApiKey); return { testApiClient, id }; diff --git a/src/modules/server/api/dto/tldraw-config.response.ts b/src/modules/server/api/dto/tldraw-config.response.ts index 29d233c7..c85f57ab 100644 --- a/src/modules/server/api/dto/tldraw-config.response.ts +++ b/src/modules/server/api/dto/tldraw-config.response.ts @@ -1,25 +1,26 @@ import { ApiProperty } from '@nestjs/swagger'; +import { ServerConfig } from '../../server.config.js'; export class TldrawPublicConfigResponse { - public constructor(config: TldrawPublicConfigResponse) { - this.TLDRAW__WEBSOCKET_URL = config.TLDRAW__WEBSOCKET_URL; - this.TLDRAW__ASSETS_ENABLED = config.TLDRAW__ASSETS_ENABLED; - this.TLDRAW__ASSETS_MAX_SIZE_BYTES = config.TLDRAW__ASSETS_MAX_SIZE_BYTES; - this.TLDRAW__ASSETS_ALLOWED_MIME_TYPES_LIST = config.TLDRAW__ASSETS_ALLOWED_MIME_TYPES_LIST; - this.FEATURE_TLDRAW_ENABLED = config.FEATURE_TLDRAW_ENABLED; + public constructor(config: ServerConfig) { + this.TLDRAW_WEBSOCKET_URL = config.SERVER_WEBSOCKET_URL; + this.TLDRAW_ASSETS_ENABLED = config.SERVER_ASSETS_ENABLED; + this.TLDRAW_ASSETS_MAX_SIZE_BYTES = config.SERVER_ASSETS_MAX_SIZE_BYTES; + this.TLDRAW_ASSETS_ALLOWED_MIME_TYPES_LIST = config.SERVER_ASSETS_ALLOWED_MIME_TYPES_LIST; + this.FEATURE_TLDRAW_ENABLED = config.SERVER_FEATURE_TLDRAW_ENABLED; } @ApiProperty() - public TLDRAW__WEBSOCKET_URL: string; + public TLDRAW_WEBSOCKET_URL: string; @ApiProperty() - public TLDRAW__ASSETS_ENABLED: boolean; + public TLDRAW_ASSETS_ENABLED: boolean; @ApiProperty() - public TLDRAW__ASSETS_MAX_SIZE_BYTES: number; + public TLDRAW_ASSETS_MAX_SIZE_BYTES: number; @ApiProperty() - public TLDRAW__ASSETS_ALLOWED_MIME_TYPES_LIST: string[]; + public TLDRAW_ASSETS_ALLOWED_MIME_TYPES_LIST: string[]; @ApiProperty() public FEATURE_TLDRAW_ENABLED!: boolean; diff --git a/src/modules/server/api/test/tldraw-config.api.spec.ts b/src/modules/server/api/test/tldraw-config.api.spec.ts index 3d93769b..7f294f0c 100644 --- a/src/modules/server/api/test/tldraw-config.api.spec.ts +++ b/src/modules/server/api/test/tldraw-config.api.spec.ts @@ -28,10 +28,10 @@ describe('Tldraw-Config Api Test', () => { expect(response.body).toEqual({ FEATURE_TLDRAW_ENABLED: true, - TLDRAW__ASSETS_ALLOWED_MIME_TYPES_LIST: ['image/png', 'image/jpeg', 'image/gif', 'image/svg+xml'], - TLDRAW__ASSETS_ENABLED: true, - TLDRAW__ASSETS_MAX_SIZE_BYTES: 10485760, - TLDRAW__WEBSOCKET_URL: 'ws://localhost:3345', + TLDRAW_ASSETS_ALLOWED_MIME_TYPES_LIST: ['image/png', 'image/jpeg', 'image/gif', 'image/svg+xml'], + TLDRAW_ASSETS_ENABLED: true, + TLDRAW_ASSETS_MAX_SIZE_BYTES: 10485760, + TLDRAW_WEBSOCKET_URL: 'ws://localhost:3345', }); }); }); diff --git a/src/modules/server/api/websocket.gateway.spec.ts b/src/modules/server/api/websocket.gateway.spec.ts index 3d16f4ad..84fc238b 100644 --- a/src/modules/server/api/websocket.gateway.spec.ts +++ b/src/modules/server/api/websocket.gateway.spec.ts @@ -46,8 +46,8 @@ describe(WebsocketGateway.name, () => { { provide: ServerConfig, useValue: { - WS_PATH_PREFIX: 'tests', - WS_PORT: 3345, + SERVER_WEBSOCKET_PATH: 'tests', + SERVER_WEBSOCKET_PORT: 3345, }, }, ], diff --git a/src/modules/server/api/websocket.gateway.ts b/src/modules/server/api/websocket.gateway.ts index bd1f807a..ba67e17f 100644 --- a/src/modules/server/api/websocket.gateway.ts +++ b/src/modules/server/api/websocket.gateway.ts @@ -28,8 +28,8 @@ export class WebsocketGateway implements OnModuleInit, OnModuleDestroy { } public async onModuleInit(): Promise { - const wsPathPrefix = this.config.WS_PATH_PREFIX; - const wsPort = this.config.WS_PORT; + const wsPathPrefix = this.config.SERVER_WEBSOCKET_PATH; + const wsPort = this.config.SERVER_WEBSOCKET_PORT; await registerYWebsocketServer( this.webSocketServer, diff --git a/src/modules/server/server.config.ts b/src/modules/server/server.config.ts index 38fbe486..e11af3ad 100644 --- a/src/modules/server/server.config.ts +++ b/src/modules/server/server.config.ts @@ -3,28 +3,28 @@ import { IsArray, IsBoolean, IsNumber, IsString, IsUrl } from 'class-validator'; export class ServerConfig { @IsString() - public WS_PATH_PREFIX = ''; + public SERVER_WEBSOCKET_PATH = ''; @IsNumber() @Transform(({ value }) => parseInt(value)) - public WS_PORT = 3345; + public SERVER_WEBSOCKET_PORT = 3345; @IsUrl({ protocols: ['wss', 'ws'], require_tld: false }) - public TLDRAW__WEBSOCKET_URL!: string; + public SERVER_WEBSOCKET_URL!: string; @Transform(({ value }) => value === 'true') @IsBoolean() - public TLDRAW__ASSETS_ENABLED = true; + public SERVER_ASSETS_ENABLED = true; @Transform(({ value }) => parseInt(value)) @IsNumber() - public TLDRAW__ASSETS_MAX_SIZE_BYTES = 10485760; + public SERVER_ASSETS_MAX_SIZE_BYTES = 10485760; @Transform(({ value }) => value.split(',')) @IsArray() - public TLDRAW__ASSETS_ALLOWED_MIME_TYPES_LIST = ['image/png', 'image/jpeg', 'image/gif', 'image/svg+xml']; + public SERVER_ASSETS_ALLOWED_MIME_TYPES_LIST = ['image/png', 'image/jpeg', 'image/gif', 'image/svg+xml']; @Transform(({ value }) => value === 'true') @IsBoolean() - public FEATURE_TLDRAW_ENABLED!: boolean; + public SERVER_FEATURE_TLDRAW_ENABLED!: boolean; } From c457e1a92368c2e84adb1b6f83144bb7a34de949 Mon Sep 17 00:00:00 2001 From: SevenWaysDP Date: Thu, 28 Nov 2024 11:17:57 +0100 Subject: [PATCH 2/6] chore: update tsconfig to exclude 'scripts' directory from build --- tsconfig.build.json | 2 +- tsconfig.json | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tsconfig.build.json b/tsconfig.build.json index 64f86c6b..052134cd 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -1,4 +1,4 @@ { "extends": "./tsconfig.json", - "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] + "exclude": ["node_modules", "test", "dist", "**/*spec.ts", "scripts"] } diff --git a/tsconfig.json b/tsconfig.json index 7826f7e0..394e9927 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -21,5 +21,6 @@ "strictFunctionTypes": true, "strictPropertyInitialization": true, "skipLibCheck": true, - } + }, + "exclude": ["scripts"], } From 3366d6c215ba9ac973ce538d2dbde51007975595 Mon Sep 17 00:00:00 2001 From: SevenWaysDP Date: Thu, 28 Nov 2024 11:49:23 +0100 Subject: [PATCH 3/6] fix: wrap configuration values in quotes for YAML compatibility --- ansible/roles/tldraw-server/templates/configmap.yml.j2 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ansible/roles/tldraw-server/templates/configmap.yml.j2 b/ansible/roles/tldraw-server/templates/configmap.yml.j2 index 5fe7ce81..5ea9fe89 100644 --- a/ansible/roles/tldraw-server/templates/configmap.yml.j2 +++ b/ansible/roles/tldraw-server/templates/configmap.yml.j2 @@ -6,13 +6,13 @@ metadata: labels: app: tldraw-server data: - AUTHORIZATION_API_HOST: {{ API_HOST }} - LOGGER_LOG_LEVEL: {{ TLDRAW_LOG_LEVEL }} - METRICS_COLLECT_DEFAULT: {{ TLDRAW_METRICS_COLLECT_DEFAULT }} + AUTHORIZATION_API_HOST: "{{ API_HOST }}" + LOGGER_LOG_LEVEL: "{{ TLDRAW_LOG_LEVEL }}" + METRICS_COLLECT_DEFAULT: "{{ TLDRAW_METRICS_COLLECT_DEFAULT }}" REDIS_CLUSTER_ENABLED: "true" REDIS_SENTINEL_SERVICE_NAME: "valkey-headless.{{ NAMESPACE }}.svc.cluster.local" SERVER_ASSETS_ALLOWED_MIME_TYPES_LIST: "{{ TLDRAW_ASSETS_ALLOWED_MIME_TYPES_LIST }}" - SERVER_ASSETS_ENABLED: {{ TLDRAW_SERVER_ASSETS_ENABLED }} - SERVER_FEATURE_TLDRAW_ENABLED: {{ FEATURE_TLDRAW_ENABLED }} + SERVER_ASSETS_ENABLED: "{{ TLDRAW_SERVER_ASSETS_ENABLED }}" + SERVER_FEATURE_TLDRAW_ENABLED: "{{ FEATURE_TLDRAW_ENABLED }}" SERVER_WEBSOCKET_PATH: "{{ TLDRAW_WEBSOCKET_PATH }}" SERVER_WEBSOCKET_URL: "wss://{{ DOMAIN }}{{ TLDRAW_WEBSOCKET_PATH }}" From bfa9856ceac73b929a9b1a525507251f4e043b50 Mon Sep 17 00:00:00 2001 From: SevenWaysDP Date: Thu, 28 Nov 2024 12:18:22 +0100 Subject: [PATCH 4/6] fix: update AUTHORIZATION_API_HOST variable to use API_HOST_URI --- ansible/roles/tldraw-server/templates/configmap.yml.j2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ansible/roles/tldraw-server/templates/configmap.yml.j2 b/ansible/roles/tldraw-server/templates/configmap.yml.j2 index 5ea9fe89..d9cdd3b9 100644 --- a/ansible/roles/tldraw-server/templates/configmap.yml.j2 +++ b/ansible/roles/tldraw-server/templates/configmap.yml.j2 @@ -6,7 +6,7 @@ metadata: labels: app: tldraw-server data: - AUTHORIZATION_API_HOST: "{{ API_HOST }}" + AUTHORIZATION_API_HOST: "{{ API_HOST_URI }}" LOGGER_LOG_LEVEL: "{{ TLDRAW_LOG_LEVEL }}" METRICS_COLLECT_DEFAULT: "{{ TLDRAW_METRICS_COLLECT_DEFAULT }}" REDIS_CLUSTER_ENABLED: "true" From 7dfd2e87795f7954a40ef0dce040ec9474ab94f9 Mon Sep 17 00:00:00 2001 From: SevenWaysDP Date: Thu, 28 Nov 2024 16:11:56 +0100 Subject: [PATCH 5/6] fix tests --- .env.test | 2 -- scripts/testing/globalSetup.ts | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/.env.test b/.env.test index 4c094c37..7bb57d34 100644 --- a/.env.test +++ b/.env.test @@ -2,8 +2,6 @@ AUTHORIZATION_API_HOST=http://localhost:3030 LOGGER_LOG_LEVEL=error -REDIS_URL=redis://localhost:6379 - S3_ACCESS_KEY=miniouser S3_BUCKET=ydocs S3_ENDPOINT=localhost diff --git a/scripts/testing/globalSetup.ts b/scripts/testing/globalSetup.ts index c72cf24b..7ec6ffbb 100644 --- a/scripts/testing/globalSetup.ts +++ b/scripts/testing/globalSetup.ts @@ -5,7 +5,7 @@ export default async function globalSetup(): Promise { const host = await redisMemoryServer.getHost(); const port = await redisMemoryServer.getPort(); - process.env.REDIS = `redis://${host}:${port}`; + process.env.REDIS_URL = `redis://${host}:${port}`; // @ts-ignore global.__REDISINSTANCE = redisMemoryServer; } From 39b8d8d604a1b487ea8c9ca6283486054cef30b6 Mon Sep 17 00:00:00 2001 From: SevenWaysDP Date: Fri, 29 Nov 2024 08:55:01 +0100 Subject: [PATCH 6/6] refactor: rename server configuration variables for clarity and consistency --- .env.default | 4 ++-- .env.test | 4 ++-- ansible/roles/tldraw-server/defaults/main.yaml | 2 +- .../tldraw-server/templates/configmap.yml.j2 | 10 +++++----- .../server/api/dto/tldraw-config.response.ts | 14 +++++++------- .../server/api/tldraw-confg.controller.ts | 4 ++-- src/modules/server/api/websocket.gateway.spec.ts | 8 ++++---- src/modules/server/api/websocket.gateway.ts | 8 ++++---- src/modules/server/server.module.ts | 4 ++-- ...{server.config.ts => tldraw-server.config.ts} | 16 ++++++++-------- 10 files changed, 37 insertions(+), 37 deletions(-) rename src/modules/server/{server.config.ts => tldraw-server.config.ts} (61%) diff --git a/.env.default b/.env.default index b945d1d7..91fa736d 100644 --- a/.env.default +++ b/.env.default @@ -11,7 +11,7 @@ S3_PORT=9000 S3_SECRET_KEY=miniouser S3_SSL=false -SERVER_FEATURE_TLDRAW_ENABLED=true -SERVER_WEBSOCKET_URL=ws://localhost:3345 +FEATURE_TLDRAW_ENABLED=true +TLDRAW_WEBSOCKET_URL=ws://localhost:3345 X_API_ALLOWED_KEYS=randomString \ No newline at end of file diff --git a/.env.test b/.env.test index 7bb57d34..aad73219 100644 --- a/.env.test +++ b/.env.test @@ -9,7 +9,7 @@ S3_PORT=9000 S3_SECRET_KEY=miniouser S3_SSL=false -SERVER_FEATURE_TLDRAW_ENABLED=true -SERVER_WEBSOCKET_URL=ws://localhost:3345 +FEATURE_TLDRAW_ENABLED=true +TLDRAW_WEBSOCKET_URL=ws://localhost:3345 X_API_ALLOWED_KEYS=randomString \ No newline at end of file diff --git a/ansible/roles/tldraw-server/defaults/main.yaml b/ansible/roles/tldraw-server/defaults/main.yaml index 7ad16b13..dc3d6f59 100644 --- a/ansible/roles/tldraw-server/defaults/main.yaml +++ b/ansible/roles/tldraw-server/defaults/main.yaml @@ -1,5 +1,5 @@ TLDRAW_ASSETS_ALLOWED_MIME_TYPES_LIST : "image/png,image/jpeg,image/gif,image/svg+xml" TLDRAW_LOG_LEVEL: "error" TLDRAW_METRICS_COLLECT_DEFAULT: "true" -TLDRAW_SERVER_ASSETS_ENABLED: "true" +TLDRAW_ASSETS_ENABLED: "true" TLDRAW_WEBSOCKET_PATH: "/tldraw-server" \ No newline at end of file diff --git a/ansible/roles/tldraw-server/templates/configmap.yml.j2 b/ansible/roles/tldraw-server/templates/configmap.yml.j2 index d9cdd3b9..f438ae06 100644 --- a/ansible/roles/tldraw-server/templates/configmap.yml.j2 +++ b/ansible/roles/tldraw-server/templates/configmap.yml.j2 @@ -11,8 +11,8 @@ data: METRICS_COLLECT_DEFAULT: "{{ TLDRAW_METRICS_COLLECT_DEFAULT }}" REDIS_CLUSTER_ENABLED: "true" REDIS_SENTINEL_SERVICE_NAME: "valkey-headless.{{ NAMESPACE }}.svc.cluster.local" - SERVER_ASSETS_ALLOWED_MIME_TYPES_LIST: "{{ TLDRAW_ASSETS_ALLOWED_MIME_TYPES_LIST }}" - SERVER_ASSETS_ENABLED: "{{ TLDRAW_SERVER_ASSETS_ENABLED }}" - SERVER_FEATURE_TLDRAW_ENABLED: "{{ FEATURE_TLDRAW_ENABLED }}" - SERVER_WEBSOCKET_PATH: "{{ TLDRAW_WEBSOCKET_PATH }}" - SERVER_WEBSOCKET_URL: "wss://{{ DOMAIN }}{{ TLDRAW_WEBSOCKET_PATH }}" + TLDRAW_ASSETS_ALLOWED_MIME_TYPES_LIST: "{{ TLDRAW_ASSETS_ALLOWED_MIME_TYPES_LIST }}" + TLDRAW_ASSETS_ENABLED: "{{ TLDRAW_ASSETS_ENABLED }}" + FEATURE_TLDRAW_ENABLED: "{{ FEATURE_TLDRAW_ENABLED }}" + TLDRAW_WEBSOCKET_PATH: "{{ TLDRAW_WEBSOCKET_PATH }}" + TLDRAW_WEBSOCKET_URL: "wss://{{ DOMAIN }}{{ TLDRAW_WEBSOCKET_PATH }}" diff --git a/src/modules/server/api/dto/tldraw-config.response.ts b/src/modules/server/api/dto/tldraw-config.response.ts index c85f57ab..d1ec6ddc 100644 --- a/src/modules/server/api/dto/tldraw-config.response.ts +++ b/src/modules/server/api/dto/tldraw-config.response.ts @@ -1,13 +1,13 @@ import { ApiProperty } from '@nestjs/swagger'; -import { ServerConfig } from '../../server.config.js'; +import { TldrawServerConfig } from '../../tldraw-server.config.js'; export class TldrawPublicConfigResponse { - public constructor(config: ServerConfig) { - this.TLDRAW_WEBSOCKET_URL = config.SERVER_WEBSOCKET_URL; - this.TLDRAW_ASSETS_ENABLED = config.SERVER_ASSETS_ENABLED; - this.TLDRAW_ASSETS_MAX_SIZE_BYTES = config.SERVER_ASSETS_MAX_SIZE_BYTES; - this.TLDRAW_ASSETS_ALLOWED_MIME_TYPES_LIST = config.SERVER_ASSETS_ALLOWED_MIME_TYPES_LIST; - this.FEATURE_TLDRAW_ENABLED = config.SERVER_FEATURE_TLDRAW_ENABLED; + public constructor(config: TldrawServerConfig) { + this.TLDRAW_WEBSOCKET_URL = config.TLDRAW_WEBSOCKET_URL; + this.TLDRAW_ASSETS_ENABLED = config.TLDRAW_ASSETS_ENABLED; + this.TLDRAW_ASSETS_MAX_SIZE_BYTES = config.TLDRAW_ASSETS_MAX_SIZE_BYTES; + this.TLDRAW_ASSETS_ALLOWED_MIME_TYPES_LIST = config.TLDRAW_ASSETS_ALLOWED_MIME_TYPES_LIST; + this.FEATURE_TLDRAW_ENABLED = config.FEATURE_TLDRAW_ENABLED; } @ApiProperty() diff --git a/src/modules/server/api/tldraw-confg.controller.ts b/src/modules/server/api/tldraw-confg.controller.ts index ee90e5d5..2650a6a4 100644 --- a/src/modules/server/api/tldraw-confg.controller.ts +++ b/src/modules/server/api/tldraw-confg.controller.ts @@ -1,12 +1,12 @@ import { Controller, Get } from '@nestjs/common'; import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; -import { ServerConfig } from '../server.config.js'; +import { TldrawServerConfig } from '../tldraw-server.config.js'; import { TldrawPublicConfigResponse } from './dto/tldraw-config.response.js'; @ApiTags('tldraw/config') @Controller('tldraw/config') export class TldrawConfigController { - public constructor(private readonly config: ServerConfig) {} + public constructor(private readonly config: TldrawServerConfig) {} @ApiOperation({ summary: 'Useable configuration for clients' }) @ApiResponse({ status: 200, type: TldrawPublicConfigResponse }) diff --git a/src/modules/server/api/websocket.gateway.spec.ts b/src/modules/server/api/websocket.gateway.spec.ts index 84fc238b..6845d86f 100644 --- a/src/modules/server/api/websocket.gateway.spec.ts +++ b/src/modules/server/api/websocket.gateway.spec.ts @@ -9,7 +9,7 @@ import { RedisService } from '../../../infra/redis/redis.service.js'; import { StorageService } from '../../../infra/storage/storage.service.js'; import * as WsService from '../../../infra/y-redis/ws.service.js'; import { registerYWebsocketServer } from '../../../infra/y-redis/ws.service.js'; -import { ServerConfig } from '../server.config.js'; +import { TldrawServerConfig } from '../tldraw-server.config.js'; import { WebsocketGateway } from './websocket.gateway.js'; describe(WebsocketGateway.name, () => { @@ -44,10 +44,10 @@ describe(WebsocketGateway.name, () => { useValue: createMock(), }, { - provide: ServerConfig, + provide: TldrawServerConfig, useValue: { - SERVER_WEBSOCKET_PATH: 'tests', - SERVER_WEBSOCKET_PORT: 3345, + TLDRAW_WEBSOCKET_PATH: 'tests', + TLDRAW_WEBSOCKET_PORT: 3345, }, }, ], diff --git a/src/modules/server/api/websocket.gateway.ts b/src/modules/server/api/websocket.gateway.ts index ba67e17f..cfc76cc2 100644 --- a/src/modules/server/api/websocket.gateway.ts +++ b/src/modules/server/api/websocket.gateway.ts @@ -6,7 +6,7 @@ import { MetricsService } from '../../../infra/metrics/metrics.service.js'; import { RedisService } from '../../../infra/redis/redis.service.js'; import { StorageService } from '../../../infra/storage/storage.service.js'; import { registerYWebsocketServer } from '../../../infra/y-redis/ws.service.js'; -import { ServerConfig } from '../server.config.js'; +import { TldrawServerConfig } from '../tldraw-server.config.js'; export const UWS = 'UWS'; @@ -17,7 +17,7 @@ export class WebsocketGateway implements OnModuleInit, OnModuleDestroy { private readonly storageService: StorageService, private readonly authorizationService: AuthorizationService, private readonly redisService: RedisService, - private readonly config: ServerConfig, + private readonly config: TldrawServerConfig, private readonly logger: Logger, ) { this.logger.setContext(WebsocketGateway.name); @@ -28,8 +28,8 @@ export class WebsocketGateway implements OnModuleInit, OnModuleDestroy { } public async onModuleInit(): Promise { - const wsPathPrefix = this.config.SERVER_WEBSOCKET_PATH; - const wsPort = this.config.SERVER_WEBSOCKET_PORT; + const wsPathPrefix = this.config.TLDRAW_WEBSOCKET_PATH; + const wsPort = this.config.TLDRAW_WEBSOCKET_PORT; await registerYWebsocketServer( this.webSocketServer, diff --git a/src/modules/server/server.module.ts b/src/modules/server/server.module.ts index 7f928a1d..d14b6698 100644 --- a/src/modules/server/server.module.ts +++ b/src/modules/server/server.module.ts @@ -10,12 +10,12 @@ import { StorageModule } from '../../infra/storage/storage.module.js'; import { TldrawConfigController } from './api/tldraw-confg.controller.js'; import { TldrawDocumentController } from './api/tldraw-document.controller.js'; import { UWS, WebsocketGateway } from './api/websocket.gateway.js'; -import { ServerConfig } from './server.config.js'; import { TldrawDocumentService } from './service/tldraw-document.service.js'; +import { TldrawServerConfig } from './tldraw-server.config.js'; @Module({ imports: [ - ConfigurationModule.register(ServerConfig), + ConfigurationModule.register(TldrawServerConfig), RedisModule, StorageModule, AuthorizationModule, diff --git a/src/modules/server/server.config.ts b/src/modules/server/tldraw-server.config.ts similarity index 61% rename from src/modules/server/server.config.ts rename to src/modules/server/tldraw-server.config.ts index e11af3ad..b21a7e0f 100644 --- a/src/modules/server/server.config.ts +++ b/src/modules/server/tldraw-server.config.ts @@ -1,30 +1,30 @@ import { Transform } from 'class-transformer'; import { IsArray, IsBoolean, IsNumber, IsString, IsUrl } from 'class-validator'; -export class ServerConfig { +export class TldrawServerConfig { @IsString() - public SERVER_WEBSOCKET_PATH = ''; + public TLDRAW_WEBSOCKET_PATH = ''; @IsNumber() @Transform(({ value }) => parseInt(value)) - public SERVER_WEBSOCKET_PORT = 3345; + public TLDRAW_WEBSOCKET_PORT = 3345; @IsUrl({ protocols: ['wss', 'ws'], require_tld: false }) - public SERVER_WEBSOCKET_URL!: string; + public TLDRAW_WEBSOCKET_URL!: string; @Transform(({ value }) => value === 'true') @IsBoolean() - public SERVER_ASSETS_ENABLED = true; + public TLDRAW_ASSETS_ENABLED = true; @Transform(({ value }) => parseInt(value)) @IsNumber() - public SERVER_ASSETS_MAX_SIZE_BYTES = 10485760; + public TLDRAW_ASSETS_MAX_SIZE_BYTES = 10485760; @Transform(({ value }) => value.split(',')) @IsArray() - public SERVER_ASSETS_ALLOWED_MIME_TYPES_LIST = ['image/png', 'image/jpeg', 'image/gif', 'image/svg+xml']; + public TLDRAW_ASSETS_ALLOWED_MIME_TYPES_LIST = ['image/png', 'image/jpeg', 'image/gif', 'image/svg+xml']; @Transform(({ value }) => value === 'true') @IsBoolean() - public SERVER_FEATURE_TLDRAW_ENABLED!: boolean; + public FEATURE_TLDRAW_ENABLED!: boolean; }