Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shelf middleware example #22

Merged
merged 4 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ name: Dart

on:
push:
branches: [ "main" ]
branches: ["main"]
pull_request:
branches: [ "main" ]
branches: ["main"]

jobs:
build:
Expand All @@ -27,6 +27,9 @@ jobs:
- name: Install dependencies
run: dart pub get

- name: Install dependencies for pharaoh examples
run: cd pharaoh_examples && dart pub get

# Uncomment this step to verify the use of 'dart format' on each commit.
# - name: Verify formatting
# run: dart format --output=none --set-exit-if-changed .
Expand Down
12 changes: 9 additions & 3 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ API service with two route groups `/guest` and `/admin`.
- Group: `/admin`
- Group: `/guest`

[Jump to Source](https://github.com/codekeyz/pharaoh/tree/main/pharaoh_examples/lib/route_groups)
[Jump to Source](https://github.com/codekeyz/pharaoh/tree/main/pharaoh_examples/lib/route_groups/index.dart)

### Middleware

API service with Logger Middleware that logs every request that hits our server.

[Jump to Source](https://github.com/codekeyz/pharaoh/tree/main/pharaoh_examples/lib/middleware)
[Jump to Source](https://github.com/codekeyz/pharaoh/tree/main/pharaoh_examples/lib/middleware/index.dart)

### Shelf Middleware with Pharaoh

Add CORS to our Pharaoah server using [shelf_cors_headers](https://pub.dev/packages/shelf_cors_headers)

[Jump to Source](https://github.com/codekeyz/pharaoh/tree/main/pharaoh_examples/lib/shelf_middleware/index.dart)

### Serve Webpages and Files

[Jump to Source](https://github.com/codekeyz/pharaoh/tree/main/pharaoh_examples/lib/serve_files)
[Jump to Source](https://github.com/codekeyz/pharaoh/tree/main/pharaoh_examples/lib/serve_files/index.dart)
13 changes: 13 additions & 0 deletions pharaoh_examples/lib/shelf_middleware/index.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:pharaoh/pharaoh.dart';
import 'package:shelf_cors_headers/shelf_cors_headers.dart';

final app = Pharaoh();

void main() async {
/// Using shelf_cors_header with Pharaoh
app.use(useShelfMiddleware(corsHeaders()));

app.get('/', (req, res) => res.json(req.headers));

await app.listen();
}