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

Added customMiddleware to allow for easy request intercepting #317

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

jpeiffer
Copy link

@jpeiffer jpeiffer commented Mar 27, 2022

  • feat: Exposes autoCompress via serve so it can be passed to the created server
  • feat: Exposes a customMiddleware attribute via serve for CORS support
  • fix: Fixed fullstack/frontend when running on Flutter Web as it was previously failing due to lack of CORS support

@jimmyff
Copy link

jimmyff commented Oct 20, 2022

I would really like to see the PR go through. I can't find another elegant solution for a flutter web project to communicate with a cloud run backend.

@kevmoo
Copy link
Collaborator

kevmoo commented Oct 20, 2022

I'd love to look at this. A bit slammed this week. I'll dig in next week.

@jimmyff
Copy link

jimmyff commented Oct 27, 2022

I've just been attempting to use this PR (as I really need to find a solution for the CORS issue) but I'm not having any luck -unless I'm misunderstanding I think the implementation could be flawed as it requires editing of the generated file: bin/server.dart? I've attempted to make the changes as demonstrated in @jpeiffer example however after being deployed to cloud run via GCP Cloud Build, I believe my changes are lost when the functions_framework_builder rebuilds the file. There is a very good chance I've missed a critical details though. Here is my bin/server.dart (complete with generated code warning!):

bin/server.dart
// GENERATED CODE - DO NOT MODIFY BY HAND
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import 'dart:io';

import 'package:cloud_functions_core/core.dart';
import 'package:functions_framework/serve.dart';
import 'package:shelf/shelf.dart';
import 'package:update_claims/functions.dart' as function_library;

Future<void> main(List<String> args) async {
  await serve(
    args,
    _nameToFunctionTarget,
    customMiddleware: corsHeaders(originChecker: (origin) {
      return Cors.domains.contains(origin);
    }),
  );
}

FunctionTarget? _nameToFunctionTarget(String name) {
  switch (name) {
    case 'function':
      return FunctionTarget.http(
        function_library.function,
      );
    default:
      return null;
  }
}

If my understanding is correct, then I think this solution would need implementing in the builder. The function method is exposed to the server.dart file via the @CloudFunction annotation and function_library.function -perhaps custom middleware would need an annotation @CustomMiddleware and the builder could pick that up as function_library.custom_middleware or similar. All of a sudden this solution would be significantly more complex. Hopefully i've missed a detail though, and this PR is fine as it is!

If this PR doesn't go through, @kevmoo do you have any suggestions for resolving the CORS issue in a functions_framework + flutter web combo?

@jimmyff
Copy link

jimmyff commented Oct 27, 2022

I have taken the idea from this PR, but moved the middleware from server.dart to the functions file, details here: #222 (comment)

@kevmoo
Copy link
Collaborator

kevmoo commented Oct 27, 2022

I've been super slammed. Thanks for your patience waiting for me here!

@kevmoo
Copy link
Collaborator

kevmoo commented Nov 11, 2022

Have you seen pkg:gcp?

If you just want to use cloud run and get some of those features, you can use pkg:gcp

Or are you actually handling "functions"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants