Skip to content

Commit

Permalink
feat: add origin support
Browse files Browse the repository at this point in the history
  • Loading branch information
homj committed Aug 12, 2024
1 parent e702d5b commit 922d9ca
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/favicon/favicon.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Controller, Get, Param, Redirect, Res } from '@nestjs/common';
import { EnvironmentService } from '../packages/environment/environment.service';
import { FaviconService } from './favicon.service';
import { Response } from 'express';
import { DEFAULT_SIZE, SUPPORTED_SIZES } from './favicon.constants';
Expand All @@ -7,12 +8,15 @@ import { Domain } from './utils/domain';

@Controller()
export class FaviconController {
constructor(private readonly faviconService: FaviconService) {}
constructor(private readonly faviconService: FaviconService,
private readonly environmentService: EnvironmentService) {
}

@Get()
@Redirect('https://github.com/twentyhq/favicon/blob/main/README.md', 301)
// eslint-disable-next-line @typescript-eslint/no-empty-function
redirectToReadme() {}
redirectToReadme() {
}

@Get('/health')
checkHealth(@Res() res: Response) {
Expand All @@ -34,6 +38,8 @@ export class FaviconController {
@Param() params: { domainName: string; size: string },
@Res() res: Response,
) {
res.set('Access-Control-Allow-Origin', this.environmentService.getAllowedOrigins());

const domainName = params.domainName;
const size = params.size ?? DEFAULT_SIZE;
if (!Domain.checkDomainIsValid(domainName)) {
Expand Down
6 changes: 6 additions & 0 deletions src/packages/environment/environment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ export class EnvironmentService {
this.configService.get<string>('STORAGE_LOCAL_PATH') ?? '.local-storage'
);
}

getAllowedOrigins(): string {
return (
this.configService.get<string>('ALLOWED_ORIGINS') ?? '*'
);
}
}
4 changes: 4 additions & 0 deletions src/packages/environment/environment.validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export class EnvironmentVariables {
@IsString()
@ValidateIf((env) => env.STORAGE_TYPE === StorageType.Local)
STORAGE_LOCAL_PATH?: string;

@IsString()
@IsOptional()
ALLOWED_ORIGINS: string;
}

export function validate(config: Record<string, unknown>) {
Expand Down

0 comments on commit 922d9ca

Please sign in to comment.