Skip to content

Commit

Permalink
fix: cors
Browse files Browse the repository at this point in the history
  • Loading branch information
temadev committed Dec 13, 2022
1 parent d935fd0 commit 687618e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ AMQP_TOPICS="amq.topic"
TWITTER_CLIENT_ID="twitter-client-id"
TWITTER_CLIENT_SECRET="twitter-client-secret"
TWITTER_CALLBACK_URL="http://localhost:3001/auth/twitter"

CORS_ORIGINS="http://localhost:3001"
12 changes: 12 additions & 0 deletions apps/app/src/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export type TwitterConfig = {
callbackUrl: string;
};

export type CorsConfig = {
origin: string | string[];
allowedHeaders: string;
methods: string;
};

export class AppConfiguration implements BotConfig, DatabaseConfig, AccessConfig, AmpqConfig {
defaultTtl: number;
telegramToken: string;
Expand All @@ -39,6 +45,7 @@ export class AppConfiguration implements BotConfig, DatabaseConfig, AccessConfig
db: DatabaseConnection;
amqp: AmpqConnection;
healthTimeout: number;
cors: CorsConfig;
}

export function configuration(): AppConfiguration {
Expand Down Expand Up @@ -75,5 +82,10 @@ export function configuration(): AppConfiguration {
topics: process.env.AMQP_TOPICS ? process.env.AMQP_TOPICS.split(',') : [],
},
healthTimeout: (process.env.HEALTH_TIMEOUT ? parseInt(process.env.HEALTH_TIMEOUT, 10) : 120) * 1_000,
cors: {
origin: process.env.CORS_ORIGINS ? process.env.CORS_ORIGINS.split(',') : '*',
allowedHeaders: process.env.CORS_ALLOWED_HEADERS ? process.env.CORS_ALLOWED_HEADERS : '*',
methods: process.env.CORS_METHODS ? process.env.CORS_METHODS : '*',
},
};
}
12 changes: 6 additions & 6 deletions apps/app/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import { Logger } from 'nestjs-pino';
import { ValidationPipe } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';

import { AppConfiguration } from './app.config';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule, { bufferLogs: true });
const app = await NestFactory.create<NestExpressApplication>(AppModule, { bufferLogs: true });
const config = app.get<ConfigService<AppConfiguration>>(ConfigService);

app.useLogger(app.get(Logger));
app.enableCors();
app.enableCors({ ...config.get('cors', { infer: true }), credentials: true });
app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true }));

const config = app.get<ConfigService<AppConfiguration>>(ConfigService);
const port = config.get('port', { infer: true });

const swaggerConfig = new DocumentBuilder()
.setTitle('Tookey API')
.addApiKey({ type: 'apiKey', in: 'header', name: 'X-SIGNIN-KEY' }, 'signin')
Expand All @@ -27,7 +27,7 @@ async function bootstrap() {
const document = SwaggerModule.createDocument(app, swaggerConfig.build());
SwaggerModule.setup('swagger', app, document);

await app.listen(port);
await app.listen(config.get('port', { infer: true }));
}

bootstrap();

0 comments on commit 687618e

Please sign in to comment.