Skip to content

Commit

Permalink
BC-7960 - Move config api to tldraw server (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
SevenWaysDP authored Oct 22, 2024
1 parent 1afb19c commit 4c657aa
Show file tree
Hide file tree
Showing 18 changed files with 255 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .env.default
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ S3_PORT=9000
S3_SSL=false
S3_ACCESS_KEY=miniouser
S3_SECRET_KEY=miniouser

WS_PORT=3345

TLDRAW__WEBSOCKET_URL=ws://localhost:3345
FEATURE_TLDRAW_ENABLED=true
8 changes: 8 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ S3_PORT=9000
S3_SSL=false
S3_ACCESS_KEY=miniouser
S3_SECRET_KEY=miniouser

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
2 changes: 1 addition & 1 deletion .github/workflows/dependency-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ jobs:
- name: 'Dependency Review'
uses: actions/dependency-review-action@v4
with:
allow-licenses: AGPL-3.0-only, LGPL-3.0, MIT, Apache-2.0, BSD-2-Clause, BSD-3-Clause, ISC, X11, 0BSD, GPL-3.0, Unlicense, CC0-1.0
allow-licenses: AGPL-3.0-only, LGPL-3.0, MIT, Apache-2.0, BSD-2-Clause, BSD-3-Clause, ISC, X11, 0BSD, GPL-3.0, Unlicense, CC0-1.0, Python-2.0
allow-dependencies-licenses: pkg:npm/%40y/redis
9 changes: 7 additions & 2 deletions ansible/roles/tldraw-server/templates/configmap.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v1
kind: ConfigMap
metadata:
name: tldraw-server-configmap
namespace: {{ NAMESPACE }}
namespace: "{{ NAMESPACE }}"
labels:
app: tldraw-server
data:
Expand All @@ -12,4 +12,9 @@ data:
LOG: "@y/redis"
FEATURE_PROMETHEUS_METRICS_ENABLED: "true"
REDIS_CLUSTER_ENABLED: "true"
REDIS_SENTINEL_SERVICE_NAME: valkey-headless.{{ NAMESPACE }}.svc.cluster.local
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"
3 changes: 3 additions & 0 deletions ansible/roles/tldraw-server/templates/deployment.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ spec:
- containerPort: 3345
name: tldraw-ws
protocol: TCP
- containerPort: 3349
name: tldraw-http
protocol: TCP
- containerPort: 9090
name: metrics
protocol: TCP
Expand Down
7 changes: 7 additions & 0 deletions ansible/roles/tldraw-server/templates/ingress.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,10 @@ spec:
port:
number: 3345
pathType: Prefix
- path: /api/tldraw
backend:
service:
name: tldraw-server-svc
port:
number: 3349
pathType: Prefix
4 changes: 4 additions & 0 deletions ansible/roles/tldraw-server/templates/server-svc.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ spec:
targetPort: 3345
protocol: TCP
name: tldraw-ws
- port: 3349
targetPort: 3349
protocol: TCP
name: tldraw-http
selector:
app: tldraw-server
84 changes: 67 additions & 17 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 @@ -35,6 +35,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",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
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
8 changes: 1 addition & 7 deletions src/infra/redis/redis.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ import { IsBoolean, IsOptional, IsString, IsUrl, ValidateIf } from 'class-valida
export class RedisConfig {
@IsBoolean()
@IsOptional()
@Transform(({ value }: { value: string }) => {
if (value.toLowerCase() === 'true') {
return true;
} else {
return false;
}
})
@Transform(({ value }) => value === 'true')
public REDIS_CLUSTER_ENABLED!: boolean;

@IsUrl({ protocols: ['redis'], require_tld: false })
Expand Down
8 changes: 1 addition & 7 deletions src/infra/storage/storage.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ export class StorageConfig {
public S3_PORT!: number;

@IsBoolean()
@Transform(({ value }: { value: string }) => {
if (value.toLowerCase() === 'true') {
return true;
} else {
return false;
}
})
@Transform(({ value }) => value === 'true')
public S3_SSL!: boolean;

@IsString()
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;
}
8 changes: 7 additions & 1 deletion src/modules/server/api/test/test-api-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { INestApplication } from '@nestjs/common';
import supertest from 'supertest';
import * as supertest from 'supertest';

const headerConst = {
accept: 'accept',
Expand Down Expand Up @@ -34,6 +34,7 @@ export class TestApiClient {

public get(subPath?: string): supertest.Test {
const path = this.getPath(subPath);
//@ts-ignore
const testRequestInstance = supertest(this.app.getHttpServer())
.get(path)
.set(this.kindOfAuth, this.authHeader)
Expand All @@ -44,6 +45,7 @@ export class TestApiClient {

public delete(subPath?: string): supertest.Test {
const path = this.getPath(subPath);
//@ts-ignore
const testRequestInstance = supertest(this.app.getHttpServer())
.delete(path)
.set(this.kindOfAuth, this.authHeader)
Expand All @@ -54,6 +56,7 @@ export class TestApiClient {

public put<T extends object | string>(subPath?: string, data?: T): supertest.Test {
const path = this.getPath(subPath);
//@ts-ignore
const testRequestInstance = supertest(this.app.getHttpServer())
.put(path)
.set(this.kindOfAuth, this.authHeader)
Expand All @@ -64,6 +67,7 @@ export class TestApiClient {

public patch<T extends object | string>(subPath?: string, data?: T): supertest.Test {
const path = this.getPath(subPath);
//@ts-ignore
const testRequestInstance = supertest(this.app.getHttpServer())
.patch(path)
.set(this.kindOfAuth, this.authHeader)
Expand All @@ -74,6 +78,7 @@ export class TestApiClient {

public post<T extends object | string>(subPath?: string, data?: T): supertest.Test {
const path = this.getPath(subPath);
//@ts-ignore
const testRequestInstance = supertest(this.app.getHttpServer())
.post(path)
.set(this.kindOfAuth, this.authHeader)
Expand All @@ -90,6 +95,7 @@ export class TestApiClient {
fileName: string,
): supertest.Test {
const path = this.getPath(subPath);
//@ts-ignore
const testRequestInstance = supertest(this.app.getHttpServer())
.post(path)
.set(this.kindOfAuth, this.authHeader)
Expand Down
Loading

0 comments on commit 4c657aa

Please sign in to comment.