Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BC-7960 - Move config api to tldraw server #23

Merged
merged 17 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .env.default
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ S3_SSL=false
S3_ACCESS_KEY=miniouser
S3_SECRET_KEY=miniouser

WS_PORT="3345"
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
5 changes: 5 additions & 0 deletions ansible/roles/tldraw-server/templates/configmap.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ data:
LOG: "@y/redis"
FEATURE_PROMETHEUS_METRICS_ENABLED: "true"
REDIS_SENTINEL_SERVICE_NAME: valkey-headless.{{ NAMESPACE }}.svc.cluster.local
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
71 changes: 68 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@nestjs/core": "^10.4.1",
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.4.1",
"@nestjs/swagger": "^7.4.2",
"@y/redis": "github:hpi-schul-cloud/y-redis#7d48e08d18ec78c9ab90063a7d867ec7f191319c",
"ioredis": "^5.4.1",
"passport": "^0.7.0",
Expand Down
13 changes: 13 additions & 0 deletions src/apps/tldraw-server.app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerDocumentOptions, SwaggerModule } from '@nestjs/swagger';
import { Logger } from '../infra/logging/logger.js';
import { MetricsModule } from '../infra/metrics/metrics.module.js';
import { ServerModule } from '../modules/server/server.module.js';
Expand All @@ -8,6 +9,18 @@ async function bootstrap(): Promise<void> {
const nestApp = await NestFactory.create(ServerModule);
nestApp.setGlobalPrefix('api');
nestApp.enableCors();

const options: SwaggerDocumentOptions = {
operationIdFactory: (_controllerKey: string, methodKey: string) => methodKey,
};

const config = new DocumentBuilder()
.setTitle('Tldraw API')
.setDescription('The Tldraw API to persist and share drawings')
.build();
const document = SwaggerModule.createDocument(nestApp, config, options);
SwaggerModule.setup('docs', nestApp, document);

await nestApp.init();

const metricsPort = 9090;
Expand Down
26 changes: 26 additions & 0 deletions src/modules/server/api/dto/tldraw-config.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ApiProperty } from '@nestjs/swagger';

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;
}

@ApiProperty()
public TLDRAW__WEBSOCKET_URL: string;

@ApiProperty()
public TLDRAW__ASSETS_ENABLED: boolean;

@ApiProperty()
public TLDRAW__ASSETS_MAX_SIZE_BYTES: number;

@ApiProperty()
public TLDRAW__ASSETS_ALLOWED_MIME_TYPES_LIST: string[];

@ApiProperty()
public FEATURE_TLDRAW_ENABLED!: boolean;
}
29 changes: 29 additions & 0 deletions src/modules/server/api/tldraw-confg.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Controller, Get } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { TldrawPublicConfigResponse } from './dto/tldraw-config.response.js';

@ApiTags('tldraw/config')
@Controller('tldraw/config')
export class TldrawConfigController {
public constructor(private readonly configService: ConfigService) {}

@ApiOperation({ summary: 'Useable configuration for clients' })
@ApiResponse({ status: 200, type: TldrawPublicConfigResponse })
@Get('/public')
public publicConfig(): TldrawPublicConfigResponse {
const mappedConfig = {
TLDRAW__WEBSOCKET_URL: this.configService.getOrThrow('TLDRAW__WEBSOCKET_URL'),
TLDRAW__ASSETS_ENABLED: this.configService.getOrThrow('TLDRAW__ASSETS_ENABLED') === 'true',
TLDRAW__ASSETS_MAX_SIZE_BYTES: parseInt(this.configService.getOrThrow('TLDRAW__ASSETS_MAX_SIZE_BYTES')),
TLDRAW__ASSETS_ALLOWED_MIME_TYPES_LIST: this.configService
.getOrThrow('TLDRAW__ASSETS_ALLOWED_MIME_TYPES_LIST')
.split(','),
FEATURE_TLDRAW_ENABLED: this.configService.getOrThrow('FEATURE_TLDRAW_ENABLED') === 'true',
};

const response = new TldrawPublicConfigResponse(mappedConfig);

return response;
}
}
7 changes: 4 additions & 3 deletions src/modules/server/server.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { App } from 'uws';
import { AuthGuardModule } from '../../infra/auth-guard/auth-guard.module.js';
import { AuthorizationModule } from '../../infra/authorization/authorization.module.js';
import { LoggerModule } from '../../infra/logging/logger.module.js';
import { RedisModule } from '../../infra/redis/index.js';
import { StorageModule } from '../../infra/storage/storage.module.js';
import { UWS, WebsocketGateway } from './api/websocket.gateway.js';
import { AuthGuardModule } from '../../infra/auth-guard/auth-guard.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 { TldrawDocumentService } from './service/tldraw-document.service.js';

@Module({
Expand All @@ -27,6 +28,6 @@ import { TldrawDocumentService } from './service/tldraw-document.service.js';
useValue: App({}),
},
],
controllers: [TldrawDocumentController],
controllers: [TldrawDocumentController, TldrawConfigController],
})
export class ServerModule {}
Loading