Skip to content

Commit

Permalink
feat(cors): enable cors for specific origin based on env (#855)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinezanardi committed Feb 7, 2024
1 parent 5bf00c5 commit f4ceb12
Show file tree
Hide file tree
Showing 9 changed files with 8,840 additions and 8,622 deletions.
1 change: 1 addition & 0 deletions .github/workflows/deploy-to-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ jobs:
echo DATABASE_NAME="${{ secrets.PRODUCTION_DATABASE_NAME }}" >> env/.env.production
echo DATABASE_USERNAME="${{ secrets.PRODUCTION_DATABASE_USERNAME }}" >> env/.env.production
echo DATABASE_PASSWORD="${{ secrets.PRODUCTION_DATABASE_PASSWORD }}" >> env/.env.production
echo CORS_ORIGIN="${{ secrets.PRODUCTION_CORS_ORIGIN }}" >> env/.env.production
cat env/.env.production
- name: Build app ✨
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ Environment variables are :
| `DATABASE_NAME` | MongoDB database name ||| Can't be empty string |
| `DATABASE_USERNAME` | MongoDB database user ||| Can't be empty string |
| `DATABASE_PASSWORD` | MongoDB database password ||| Can't be empty string |
| `CORS_ORIGIN` | CORS allowed origin || `*` | If set, can't be empty string |


## <a name="code-analysis-and-consistency">☑️ Code analysis and consistency</a>
Expand Down
3 changes: 2 additions & 1 deletion env/.env.development
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ DATABASE_HOST=127.0.0.1
DATABASE_PORT=27017
DATABASE_NAME=werewolves-assistant
DATABASE_USERNAME=dev
DATABASE_PASSWORD=speed_burger_is_the_best
DATABASE_PASSWORD=speed_burger_is_the_best
CORS_ORIGIN="http://localhost:3000"
3 changes: 2 additions & 1 deletion env/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ DATABASE_HOST=locahost
DATABASE_PORT=port
DATABASE_NAME=name
DATABASE_USERNAME=username
DATABASE_PASSWORD=password
DATABASE_PASSWORD=password
CORS_ORIGIN="*"
5 changes: 5 additions & 0 deletions src/modules/config/env/types/env.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class EnvironmentVariables {
@IsString()
@IsNotEmpty()
public DATABASE_PASSWORD: string;

@IsOptional()
@IsString()
@IsNotEmpty()
public CORS_ORIGIN: string = "*";
}

export { EnvironmentVariables };
1 change: 1 addition & 0 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { AppModule } from "@/app.module";

async function bootstrap(): Promise<NestFastifyApplication> {
const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter(FASTIFY_SERVER_DEFAULT_OPTIONS));
app.enableCors({ origin: process.env.CORS_ORIGIN ?? "*" });
app.useGlobalPipes(new ValidationPipe(DEFAULT_VALIDATION_PIPE_OPTIONS));
const documentationPath = "docs";
createSwaggerDocument(documentationPath, app);
Expand Down

0 comments on commit f4ceb12

Please sign in to comment.