GithubHelp home page GithubHelp logo

googlecloudplatform / functions-framework-dart Goto Github PK

View Code? Open in Web Editor NEW
532.0 49.0 53.0 1.15 MB

FaaS (Function as a service) framework for writing portable Dart functions

Home Page: https://pub.dev/packages/functions_framework

License: Apache License 2.0

Dockerfile 1.87% Dart 95.96% Shell 1.67% Makefile 0.50%
dart functions-as-a-service serverless dart-frameworks faas-framework dartlang flutter-backend flutter-full-stack dart-build-system

functions-framework-dart's Introduction

Functions Framework for Dart

This is a community-supported project, meaning there is no official level of support. The code is not covered by any SLA or deprecation policy.

Feel free to start a discussion to share thoughts or open issues for bugs and feature requests.

Functions Framework Unit Tests Lint Test Conformance Tests
Dart

An open source FaaS (Function as a Service) framework for writing portable Dart functions, brought to you by the Google Dart and Cloud Functions teams.

The Functions Framework lets you write lightweight functions that run in many different environments, including:

Google Cloud Functions does not currently provide an officially supported Dart language runtime, but we're working to make running on Google Cloud Run as seamless and symmetric an experience as possible for your Dart Functions Framework projects.

The framework allows you to go from:

examples/hello/lib/functions.dart

import 'package:functions_framework/functions_framework.dart';
import 'package:shelf/shelf.dart';

@CloudFunction()
Response function(Request request) => Response.ok('Hello, World!');

To:

curl https://<your-app-url>
# Output: Hello, World!

All without needing to worry about writing an HTTP server or request handling logic.

See more demos under the examples directory.

Features

  • Invoke a function in response to a request
  • Automatically unmarshal events conforming to the CloudEvents spec
  • Portable between serverless platforms

Quickstart

From the Dart quickstart on your local machine:

$ cd examples/hello
$ docker build -t app .
...

$ docker run -it -p 8080:8080 --name demo --rm app
Listening on :8080

In another terminal:

$ curl localhost:8080
Hello, World!

See more quickstarts.

Contributing changes

See CONTRIBUTING.md for details on how to contribute to this project, including how to build and test your changes as well as how to properly format your code.

Licensing

Apache 2.0; see LICENSE for details.

functions-framework-dart's People

Contributors

craiglabenz avatar dependabot[bot] avatar felangel avatar grant avatar kevmoo avatar mit-mit avatar nickmeinhold avatar steren avatar subfuzion avatar tahatesser avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

functions-framework-dart's Issues

Add tool for managing projects

Initial concept: tool should support package API that can be used by both a CLI and Cloud Code for enhancing developer experience working with Functions Framework for Dart projects, making it easy to create, deploy, and monitor function apps.

  • Including project scaffolding support; generate example and starter projects (relates to #19 - one or more starter project templates can also be exposed through stagehand)
  • Minimize need for installing other tools (gcloud, stagehand, etc) to work with cloud projects
  • Associate current project with a Google Cloud project, deploy, get URL, open logs/traces directly
  • Potentially be able to add new handlers (and tests) for different HTTP methods and Cloud Event types to an existing project.

Dockerfile can be a lot more simple

RUN dart pub global activate mono_repo 3.2.0
COPY mono_repo.yaml /app/mono_repo.yaml
COPY functions_framework/mono_pkg.yaml /app/functions_framework/mono_pkg.yaml
COPY functions_framework/pubspec.yaml /app/functions_framework/pubspec.yaml
COPY functions_framework_builder/mono_pkg.yaml /app/functions_framework_builder/mono_pkg.yaml
COPY functions_framework_builder/pubspec.yaml /app/functions_framework_builder/pubspec.yaml
COPY test/hello/mono_pkg.yaml /app/test/hello/mono_pkg.yaml
COPY test/hello/pubspec.yaml /app/test/hello/pubspec.yaml
RUN mono_repo pub get
COPY . .
RUN mono_repo pub get --offline

We don't need mono_repo. We can just run pub get on the example app directory. Everything else is not needed.

running example docker locally finishes with error

I first cloned this repo and then following instructions in Readme did: docker build -t app . which worked thru to step 15 but then errored out:

Removing intermediate container 7aba22d733dd
 ---> 4cc0441c32a2
Step 12/20 : COPY . .
 ---> a146a4bcafc4
Step 13/20 : RUN mono_repo pub get --offline
 ---> Running in 63eda24cfdd8
Running `pub get --offline` across 3 package(s).

Starting `/usr/lib/dart/bin/pub get --offline` in `functions_framework`...
Resolving dependencies...
Got dependencies!
`functions_framework`: success!

Starting `/usr/lib/dart/bin/pub get --offline` in `functions_framework_builder`...
Resolving dependencies...
Warning: You are using these overridden dependencies:
! functions_framework 0.1.0 from path ../functions_framework
Got dependencies!
`functions_framework_builder`: success!

Starting `/usr/lib/dart/bin/pub get --offline` in `test/hello`...
Resolving dependencies...
Warning: You are using these overridden dependencies:
! functions_framework 0.1.0 from path ../../functions_framework
! functions_framework_builder 0.1.0 from path ../../functions_framework_builder
Got dependencies!
`test/hello`: success!
Removing intermediate container 63eda24cfdd8
 ---> 6788c1f4ee66
Step 14/20 : WORKDIR /app/test/hello
 ---> Running in 540820f58151
Removing intermediate container 540820f58151
 ---> 55fe9b2fedf1
Step 15/20 : RUN dart pub run build_runner build
 ---> Running in 89576204ccf5
[INFO] Generating build script...
[INFO] Generating build script completed, took 393ms

[INFO] Creating build script snapshot......
[INFO] Creating build script snapshot... completed, took 12.9s

[INFO] Initializing inputs
[INFO] Building new asset graph...
[INFO] Building new asset graph completed, took 936ms

[INFO] Checking for unexpected pre-existing outputs....
[INFO] Found 1 declared outputs which already exist on disk. This is likely because the`.dart_tool/build` folder was deleted, or you are submitting generated files to your source repository.

Delete these files?
1 - Delete
2 - Cancel build
3 - List conflicts
Invalid argument(s): The source must not be null
The command '/bin/sh -c dart pub run build_runner build' returned a non-zero code: 64

This is on Debian 10 (running in a Crostini container on ChromeOS) and

Docker version 19.03.13, build 4484c46d9d

Update docs to use the generator

Docs currently require cloning the repo to run the examples. It's easier to have the user generate new examples to test and experiment with.

  • Update docs to mention using the generate function to scaffold starter projects (won't work until 0.3.0).

Update example

Checklist after #52 is merged:

  1. Bump packages on pub.dev to 0.2.0
  2. Update the example (app.dart -> functions.dart)
  3. Update the Dockerfile (main.dart -> server.dart)
  4. Tag the repo:
functions_framework-v0.2.0-dev
functions_framework-builder-v0.2.0-dev

Export CloudMetadata?

I'm trying to access other Google Cloud Services in Cloud Run, so I need to know my project id at runtime. For now I'm just getting it via an environment variable, but since this package can already look it up from the metadata server I wonder if CloudMetadata.projectId() could be exported?

Expose more in `RequestContext`

We can't (er, shouldn't?) expose the entire Request because we read the body.

But that leaves some things not accessible to custom and CloudEvent handlers

Including:

  • requestedUri
  • HTTP method
  • protocol version

Follow-up to #108

Validate that functions are the right "shape"

Something like

Response handleGet(Request request) => Response.ok('Hello, World!');
Future<Response> handleGet2(Request request) async =>
    Response.ok('Hello, World!');
FutureOr<Response> handleGet3(Request request) => Response.ok('Hello, World!');

Example not working on Windows 😁

This is what I did, also tried running build_runner command but no luck.

I use Flutter so does that make some problem for the environment?

PS C:\Users\yazee\Desktop\hello> dart run .\bin\server.dart
Unhandled exception:
SignalException: Failed to listen for SIGTERM, osError: OS Error: The request is not supported.
, errno = 50

Dart Version

PS C:\Users\yazee> dart --version
Dart SDK version: 2.12.0-141.0.dev (dev) (Thu Dec 10 06:04:48 2020 -0800) on "windows_x64"

Refactor tests

Use a convention similar to the one commonly used for Go tests: keep data separate from test logic. This makes the tests more readable and easier to add new test cases.

Fix JSON example and template

  • Improve wording

The wording of this title and description threw me off a bit. We're talking about a HTTP request with JSON (POST body). A JSON request is not a thing.

Suggestions:

  • Change the title?
  • Don't say "JSON request"?

Originally posted by @grant in #87 (comment)

Fix for both examples/json/README.md and the template.

Reconsider STDOUT logging behavior

When hosted on cloud, the environment provides rich HTTP logging.

The extra STDOUT prints seem to be noise.

Should figure out what the best path here is.

build_runner fails due to missing `--delete-conflicting-outputs` when building docker image

Steps to reproduce

Following the steps
Execute docker build -t app .

Logs

PS C:\Users\Taha\AndroidStudioProjects\functions-framework-dart\examples\hello> docker build -t app .
[+] Building 75.6s (13/15)
 => [internal] load build definition from Dockerfile                                                               0.0s
 => => transferring dockerfile: 419B                                                                               0.0s
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 2B                                                                                    0.0s
 => [internal] load metadata for docker.io/subfuzion/dart:slim                                                     5.0s
 => [internal] load metadata for docker.io/google/dart:latest                                                      3.3s
 => [internal] load build context                                                                                  0.0s
 => => transferring context: 4.03kB                                                                                0.0s
 => [stage-1 1/2] FROM docker.io/subfuzion/dart:slim@sha256:1a60fa1089065c4dd86d0735becafab9d14dca828ba0ee19ab6b5  9.2s
 => => resolve docker.io/subfuzion/dart:slim@sha256:1a60fa1089065c4dd86d0735becafab9d14dca828ba0ee19ab6b547c9e81e  0.0s
 => => sha256:96038e2c2edc52d526490905a0a8a29009121e6b818611cbeb0bf680ef4ff0fc 3.36kB / 3.36kB                     0.0s
 => => sha256:d2479f507fd5864befeaa2e34e66c0fcd3a4569391414055045504b6d6264439 5.25kB / 5.25kB                     1.2s
 => => sha256:1a60fa1089065c4dd86d0735becafab9d14dca828ba0ee19ab6b547c9e81e479 3.03kB / 3.03kB                     0.0s
 => => sha256:b61110f8811e25de718932280def2f6e2a76db1d1efa9ec5839e8ae5ea0a3592 85.33kB / 85.33kB                   1.8s
 => => sha256:bb427ce0421e3ec656217773e46edc510071334101188c5958fa89faa9dadbbb 806.48kB / 806.48kB                 2.5s
 => => sha256:47b4e080f3c21e53a31376bdb4e7da17e2840c3e884b2f9ec2b251c514e472de 1.07MB / 1.07MB                     3.5s
 => => extracting sha256:b61110f8811e25de718932280def2f6e2a76db1d1efa9ec5839e8ae5ea0a3592                          0.0s
 => => extracting sha256:d2479f507fd5864befeaa2e34e66c0fcd3a4569391414055045504b6d6264439                          0.0s
 => => sha256:1a7f627f3770b2bfc01ae558b9ef3c97db9b7aee8614b7948515a95b9f4cd1bb 14.61kB / 14.61kB                   3.4s
 => => extracting sha256:bb427ce0421e3ec656217773e46edc510071334101188c5958fa89faa9dadbbb                          0.0s
 => => sha256:cbec143a02d30d0ca4174074f8919cac8652f325a3710b4c983e699dba00de59 57.98kB / 57.98kB                   4.1s
 => => sha256:e8f92b5f341209361f2168ad7b6e51d93c2e7457fa601aa7c9a92249a5588000 217B / 217B                         5.1s
 => => extracting sha256:47b4e080f3c21e53a31376bdb4e7da17e2840c3e884b2f9ec2b251c514e472de                          0.0s
 => => sha256:74f9d6fb6203c0de9ebdc3214249151bedd52f457493b6732a314187fcd033c1 375B / 375B                         4.7s
 => => extracting sha256:1a7f627f3770b2bfc01ae558b9ef3c97db9b7aee8614b7948515a95b9f4cd1bb                          0.0s
 => => extracting sha256:cbec143a02d30d0ca4174074f8919cac8652f325a3710b4c983e699dba00de59                          0.0s
 => => sha256:0399d7ff3b284bf28985db4586b3f4c4b20c20dce0c7d02d856a39562efe622d 158B / 158B                         5.2s
 => => sha256:5d74d374fa1b997fc7e3319a10a40ee3c8b8435cfe44920e627d75c8e0336650 11.48kB / 11.48kB                   6.4s
 => => extracting sha256:e8f92b5f341209361f2168ad7b6e51d93c2e7457fa601aa7c9a92249a5588000                          0.0s
 => => extracting sha256:74f9d6fb6203c0de9ebdc3214249151bedd52f457493b6732a314187fcd033c1                          0.0s
 => => sha256:8346061d681ce7ab61469d917ddb90aa594d2036785a3a75e5dfef1e787c4a0d 41.94kB / 41.94kB                   6.5s
 => => extracting sha256:0399d7ff3b284bf28985db4586b3f4c4b20c20dce0c7d02d856a39562efe622d                          0.0s
 => => sha256:4289624774d737fdd22c04278f684fe55bda50b98befaed6fa61cc8c5a9ce35f 120.54kB / 120.54kB                 7.0s
 => => extracting sha256:5d74d374fa1b997fc7e3319a10a40ee3c8b8435cfe44920e627d75c8e0336650                          0.0s
 => => sha256:1057b7629ca333d5344117084946b32a44424faed73d2f24072e648060947184 122.43kB / 122.43kB                 8.8s
 => => extracting sha256:8346061d681ce7ab61469d917ddb90aa594d2036785a3a75e5dfef1e787c4a0d                          0.0s
 => => extracting sha256:4289624774d737fdd22c04278f684fe55bda50b98befaed6fa61cc8c5a9ce35f                          0.0s
 => => extracting sha256:1057b7629ca333d5344117084946b32a44424faed73d2f24072e648060947184                          0.0s
 => [stage-0 1/8] FROM docker.io/google/dart@sha256:ee2eace7c54696a43ba0f7db7ebeacf0b851d199cecd3bf4d7e3863a01dc  35.4s
 => => resolve docker.io/google/dart@sha256:ee2eace7c54696a43ba0f7db7ebeacf0b851d199cecd3bf4d7e3863a01dcbbf0       0.0s
 => => sha256:ee2eace7c54696a43ba0f7db7ebeacf0b851d199cecd3bf4d7e3863a01dcbbf0 742B / 742B                         0.0s
 => => sha256:5de967ecef0e9ad9ba8b3df26c8aec7d5b69bb496ac7bd05df85c36c3253ec2d 3.07kB / 3.07kB                     0.0s
 => => sha256:46c8e211f114a92f0bbcf260d117bc6a0836ae90c0c812d6936c363065dd8f85 52.59MB / 52.59MB                  15.5s
 => => sha256:65db95c42438444e9844fbd6e4f71b711b201526c36c08ab4543d808aa210c08 214.52MB / 214.52MB                30.7s
 => => extracting sha256:46c8e211f114a92f0bbcf260d117bc6a0836ae90c0c812d6936c363065dd8f85                          2.2s
 => => extracting sha256:65db95c42438444e9844fbd6e4f71b711b201526c36c08ab4543d808aa210c08                          4.2s
 => [stage-0 2/8] WORKDIR /app                                                                                     0.3s
 => [stage-0 3/8] COPY pubspec.yaml /app/pubspec.yaml                                                              0.0s
 => [stage-0 4/8] RUN dart pub get                                                                                 6.2s
 => [stage-0 5/8] COPY . .                                                                                         0.0s
 => [stage-0 6/8] RUN dart pub get --offline                                                                       1.0s
 => ERROR [stage-0 7/8] RUN dart pub run build_runner build                                                       27.6s
------
 > [stage-0 7/8] RUN dart pub run build_runner build:
#13 12.73 [INFO] Generating build script...
#13 13.05 [INFO] Generating build script completed, took 345ms
#13 13.05
#13 13.07 [INFO] Creating build script snapshot......
#13 25.62 [INFO] Creating build script snapshot... completed, took 12.6s
#13 25.62
#13 26.16 [INFO] Initializing inputs
#13 26.53 [INFO] Building new asset graph...
#13 27.57 [INFO] Building new asset graph completed, took 1.0s
#13 27.57
#13 27.57 [INFO] Checking for unexpected pre-existing outputs....
#13 27.57 [INFO] Found 1 declared outputs which already exist on disk. This is likely because the`.dart_tool/build` folder was deleted, or you are submitting generated files to your source repository.
#13 27.57 [SEVERE] Conflicting outputs were detected and the build is unable to prompt for permission to remove them. These outputs must be removed manually or the build can be run with `--delete-conflicting-outputs`. The outputs are: bin/main.dart
------
failed to solve with frontend dockerfile.v0: failed to build LLB: executor failed running [/bin/sh -c dart pub run build_runner build]: runc did not terminate sucessfully
PS C:\Users\Taha\AndroidStudioProjects\functions-framework-dart\examples\hello>

Solution
Add --delete-conflicting-outputs in DockerFile
At line 10 updated following line
RUN dart pub run build_runner build --delete-conflicting-outputs

After making changes, images builds successfully

PS C:\Users\Taha\AndroidStudioProjects\functions-framework-dart\examples\hello> docker build -t app .
[+] Building 40.7s (16/16) FINISHED
 => [internal] load build definition from Dockerfile                                                               0.0s
 => => transferring dockerfile: 448B                                                                               0.0s
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 2B                                                                                    0.0s
 => [internal] load metadata for docker.io/subfuzion/dart:slim                                                     2.3s
 => [internal] load metadata for docker.io/google/dart:latest                                                      2.2s
 => [stage-0 1/8] FROM docker.io/google/dart@sha256:ee2eace7c54696a43ba0f7db7ebeacf0b851d199cecd3bf4d7e3863a01dcb  0.0s
 => CACHED [stage-1 1/2] FROM docker.io/subfuzion/dart:slim@sha256:1a60fa1089065c4dd86d0735becafab9d14dca828ba0ee  0.0s
 => [internal] load build context                                                                                  0.0s
 => => transferring context: 687B                                                                                  0.0s
 => CACHED [stage-0 2/8] WORKDIR /app                                                                              0.0s
 => CACHED [stage-0 3/8] COPY pubspec.yaml /app/pubspec.yaml                                                       0.0s
 => CACHED [stage-0 4/8] RUN dart pub get                                                                          0.0s
 => [stage-0 5/8] COPY . .                                                                                         0.0s
 => [stage-0 6/8] RUN dart pub get --offline                                                                       0.8s
 => [stage-0 7/8] RUN dart pub run build_runner build --delete-conflicting-outputs                                31.8s
 => [stage-0 8/8] RUN dart compile exe bin/main.dart -o bin/server                                                 5.6s
 => [stage-1 2/2] COPY --from=0 /app/bin/server /app/bin/server                                                    0.0s
 => exporting to image                                                                                             0.1s
 => => exporting layers                                                                                            0.0s
 => => writing image sha256:a82797f3df1b4b7ea3ead64fd8e56fd3c5cdb15d233514adfaf744aa2681ecf6                       0.0s
 => => naming to docker.io/library/app

Expose a simple wrapper for one handler

serve currently does the work to find the right function.

We could expose just the run bits that handle one function.

Would be very useful for folks doing Cloud Run w/ Dart that just want the right behavior w/ logging, etc.

Consider outputting log information for Cloud Error Reporting

Might be nice

            // https://cloud.google.com/error-reporting/docs/formatting-error-messages#json_representation
            '@type':
                'type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent',
            'serviceContext': {
              'service': Platform.environment['K_SERVICE'],
              'version': Platform.environment['K_REVISION'],
            },
            if (stackFrame != null)
              'context': {
                'reportLocation': {
                  'filePath': stackFrame.library,
                  if (stackFrame.line != null) 'lineNumber': stackFrame.line,
                  'functionName': stackFrame.member,
                }
              },

FUNCTION_TARGET / --target doesn't override default function (`function`) as expected

Description

Should be able to override the default function (function) by setting the environment variable FUNCTION_TARGET or supplying a command-line parameter for the --target option.

This needs to work to pass conformance tests (#31)

Steps to reproduce

Change directory to examples/hello and run the function specifying the target function.

dart run bin/main.dart --target handleGet

or

$ FUNCTION_TARGET=handleGet dart run bin/main.dart

Result

There is no handler configured for FUNCTION_TARGET `handleGet`.

Expected:

To run the function handleGet and start listening for requests.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.