Skip to content

Commit

Permalink
Backend: Fix CORS policy issue and make logger dev-only
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamsinghshubham777 committed Sep 30, 2024
1 parent e3dc072 commit 846de7f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
20 changes: 15 additions & 5 deletions .github/workflows/publish_backend.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Deploy Dart Frog Backend
name: Test & Deploy Dart Frog Backend

on:
push:
Expand All @@ -21,10 +21,20 @@ jobs:
uses: dart-lang/setup-dart@v1

- name: Install Dependencies
run: |
cd backend
dart pub get
cd ..
working-directory: ./backend
run: dart pub get

- name: Get dependencies (core)
working-directory: ./core
run: flutter pub get

- name: Run build_runner (core)
working-directory: ./core
run: flutter pub run build_runner build --delete-conflicting-outputs

- name: Run Tests
working-directory: ./backend
run: dart test

- name: Install Globe CLI
run: dart pub global activate globe_cli
Expand Down
2 changes: 2 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ export MONGO_CONNECTION_STRING="mongodb://<username>:<password>@<host>:<port>/<d
export JWT_AUDIENCE="http://0.0.0.0:8080/"
export JWT_ISSUER="http://0.0.0.0:8080/"
export JWT_SECRET="dev_secret"
export CORS_URL="http://0.0.0.0:8080/"

# Print confirmation
echo "Environment variables have been set:"
echo "MONGO_CONNECTION_STRING=$MONGO_CONNECTION_STRING"
echo "JWT_AUDIENCE=$JWT_AUDIENCE"
echo "JWT_ISSUER=$JWT_ISSUER"
echo "JWT_SECRET=$JWT_SECRET"
echo "CORS_URL=$CORS_URL"

# Start the Dart Frog backend server
dart_frog dev
Expand Down
1 change: 1 addition & 0 deletions backend/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies:
dart_frog: ^1.1.0
logger: ^2.4.0
mongo_dart: ^0.10.3
shelf_cors_headers: ^0.1.5

dev_dependencies:
mocktail: ^1.0.3
Expand Down
10 changes: 10 additions & 0 deletions backend/routes/_middleware.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:io';

import 'package:dart_frog/dart_frog.dart';
import 'package:mongo_dart/mongo_dart.dart';
import 'package:shelf_cors_headers/shelf_cors_headers.dart';

import '../src/models/token_config.dart';
import '../src/repositories/credential_manager.dart';
Expand All @@ -11,6 +12,15 @@ import '../src/utils/extensions.dart';
Handler middleware(Handler handler) {
return handler
.use(requestLogger())
.use(
fromShelfMiddleware(
corsHeaders(
headers: {
ACCESS_CONTROL_ALLOW_ORIGIN: Platform.environment['CORS_URL']!,
},
),
),
)
.use(Middlewares.userRepository)
.use(Middlewares.db)
.use(Middlewares.credentialManager);
Expand Down
8 changes: 7 additions & 1 deletion backend/src/utils/global_instances.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import 'dart:io';

import 'package:logger/logger.dart';

final logger = Logger();
final logger = Logger(
filter: Platform.environment['CORS_URL']?.contains('0.0.0.0') ?? false
? DevelopmentFilter()
: ProductionFilter(),
);

0 comments on commit 846de7f

Please sign in to comment.