functions-framework-dart icon indicating copy to clipboard operation
functions-framework-dart copied to clipboard

Added customMiddleware to allow for easy request intercepting

Open jpeiffer opened this issue 2 years ago • 6 comments

  • 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

jpeiffer avatar Mar 27 '22 18:03 jpeiffer

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.

jimmyff avatar Oct 20 '22 11:10 jimmyff

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

kevmoo avatar Oct 20 '22 17:10 kevmoo

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 avatar Oct 27 '22 09:10 jimmyff

I have taken the idea from this PR, but moved the middleware from server.dart to the functions file, details here: https://github.com/GoogleCloudPlatform/functions-framework-dart/issues/222#issuecomment-1293372664

jimmyff avatar Oct 27 '22 10:10 jimmyff

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

kevmoo avatar Oct 27 '22 16:10 kevmoo

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"?

kevmoo avatar Nov 11 '22 22:11 kevmoo