Skip to content
Closed
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
15 changes: 2 additions & 13 deletions dartfn/templates/cloudevent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,7 @@ producer. They generally perform some work and print output for logging.
The basic shape of the function handler looks like this:

```dart
@CloudFunction()
void function(CloudEvent event, RequestContext context) {
}
```

Or like this if it needs to perform work that will complete sometime in the
future:

```dart
@CloudFunction()
FutureOr<void> function(CloudEvent event, RequestContext context) async {
Future<void> function(CloudEvent event, RequestContext context) async {
}
```

Expand All @@ -34,8 +24,7 @@ import 'package:functions_framework/functions_framework.dart';

const _encoder = JsonEncoder.withIndent(' ');

@CloudFunction()
void function(CloudEvent event, RequestContext context) {
Future<void> function(CloudEvent event, RequestContext context) async {
context.logger.info('event subject: ${event.subject}');
stderr.writeln(_encoder.convert(event));
}
Expand Down
9 changes: 3 additions & 6 deletions dartfn/templates/cloudevent/bin/server.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -21,8 +20,6 @@ Future<void> main(List<String> args) async {
}

FunctionTarget? _nameToFunctionTarget(String name) => switch (name) {
'function' => FunctionTarget.cloudEventWithContext(
function_library.function,
),
_ => null
};
'function' => FunctionTarget.cloudEventWithContext(function_library.function),
_ => null,
};
15 changes: 5 additions & 10 deletions dartfn/templates/cloudevent/lib/functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@ import 'package:functions_framework/functions_framework.dart';

const _encoder = JsonEncoder();

@CloudFunction()
void function(CloudEvent event, RequestContext context) {
context.logger
.info('[CloudEvent] source: ${event.source}, subject: ${event.subject}');
Future<void> function(CloudEvent event, RequestContext context) async {
context.logger.info(
'[CloudEvent] source: ${event.source}, subject: ${event.subject}',
);
stderr.writeln(
_encoder.convert(
{
'message': event,
'severity': LogSeverity.info,
},
),
_encoder.convert({'message': event, 'severity': LogSeverity.info}),
);
}
1 change: 0 additions & 1 deletion dartfn/templates/cloudevent/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ dependencies:

dev_dependencies:
build_runner: ^2.0.0
functions_framework_builder: ^0.4.1
http: ^1.0.0
dart_flutter_team_lints: ^3.0.0
test: ^1.16.6
Expand Down
7 changes: 2 additions & 5 deletions dartfn/templates/helloworld/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ This example handles HTTP GET requests by responding with 'Hello, World!'.

```dart
// lib/functions.dart
import 'package:functions_framework/functions_framework.dart';
import 'package:shelf/shelf.dart';

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

## Simulate a hosted environment on your own machine
Expand Down Expand Up @@ -48,8 +46,7 @@ variable is set to the new function name.
For example:

```dart
@CloudFunction()
Response handleGet(Request request) => Response.ok('Hello, World!');
Future<Response> handleGet(Request request) async => Response.ok('Hello, World!');
```

Run the `build_runner` to regenerate `bin/server.dart` from `lib/functions.dart`
Expand Down
9 changes: 3 additions & 6 deletions dartfn/templates/helloworld/bin/server.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -21,8 +20,6 @@ Future<void> main(List<String> args) async {
}

FunctionTarget? _nameToFunctionTarget(String name) => switch (name) {
'function' => FunctionTarget.http(
function_library.function,
),
_ => null
};
'function' => FunctionTarget.http(function_library.function),
_ => null,
};
5 changes: 2 additions & 3 deletions dartfn/templates/helloworld/lib/functions.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:functions_framework/functions_framework.dart';
import 'package:shelf/shelf.dart';

@CloudFunction()
Response function(Request request) => Response.ok('Hello, World!');
Future<Response> function(Request request) async =>
Response.ok('Hello, World!');
1 change: 0 additions & 1 deletion dartfn/templates/helloworld/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ dependencies:

dev_dependencies:
build_runner: ^2.0.0
functions_framework_builder: ^0.4.1
http: ^1.0.0
dart_flutter_team_lints: ^3.0.0
test: ^1.16.6
Expand Down
14 changes: 5 additions & 9 deletions dartfn/templates/json/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ This example demonstrates writing a function that accepts and returns JSON.
The basic shape of the function handler looks like this:

```dart
@CloudFunction()
GreetingResponse function(GreetingRequest request) {
Future<GreetingResponse> function(GreetingRequest request) async {
}
```

Expand Down Expand Up @@ -34,21 +33,19 @@ The Functions Framework parses the request data to fit the shape of a
the request, the function implementation provides a default name: `World`.

```dart
@CloudFunction()
GreetingResponse function(GreetingRequest request) {
Future<GreetingResponse> function(GreetingRequest request) async {
final name = request.name ?? 'World';
}
```

Finally, the function creates an object returns it. The Functions Framework will
take this and attempt "do the right" thing. In this case, the function is typed
to return a `FutureOr<GreetingResponse>`, which has a `toJson()` method, so the
to return a `Future<GreetingResponse>`, which has a `toJson()` method, so the
framework will invoke this, then set the response body to the stringified result
and set the response header (`content-type`) to `application/json`).

```dart
@CloudFunction()
GreetingResponse function(Map<String, dynamic> request) {
Future<GreetingResponse> function(Map<String, dynamic> request) async {
final name = request['name'] as String ?? 'World';
final json = GreetingResponse(salutation: 'Hello', name: name);
return json;
Expand Down Expand Up @@ -94,8 +91,7 @@ class GreetingResponse {
// operator, and therefore also `hashCode`.
}

@CloudFunction()
GreetingResponse function(GreetingRequest request) {
Future<GreetingResponse> function(GreetingRequest request) async {
final name = request.name ?? 'World';
final json = GreetingResponse(salutation: 'Hello', name: name);
return json;
Expand Down
46 changes: 21 additions & 25 deletions dartfn/templates/json/bin/server.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -21,27 +20,24 @@ Future<void> main(List<String> args) async {
}

FunctionTarget? _nameToFunctionTarget(String name) => switch (name) {
'function' => JsonFunctionTarget(
function_library.function,
(json) {
if (json is Map<String, dynamic>) {
try {
return function_library.GreetingRequest.fromJson(json);
} catch (e, stack) {
throw BadRequestException(
400,
'There was an error parsing the provided JSON data.',
innerError: e,
innerStack: stack,
);
}
}
throw BadRequestException(
400,
'The provided JSON is not the expected type '
'`Map<String, dynamic>`.',
);
},
),
_ => null
};
'function' => JsonFunctionTarget(function_library.function, (json) {
if (json is Map<String, dynamic>) {
try {
return function_library.GreetingRequest.fromJson(json);
} catch (e, stack) {
throw BadRequestException(
400,
'There was an error parsing the provided JSON data.',
innerError: e,
innerStack: stack,
);
}
}
throw BadRequestException(
400,
'The provided JSON is not the expected type '
'`Map<String, dynamic>`.',
);
}),
_ => null,
};
4 changes: 1 addition & 3 deletions dartfn/templates/json/lib/functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

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

part 'functions.g.dart';
Expand Down Expand Up @@ -58,8 +57,7 @@ class GreetingResponse {
int get hashCode => salutation.hashCode ^ name.hashCode;
}

@CloudFunction()
GreetingResponse function(GreetingRequest request) {
Future<GreetingResponse> function(GreetingRequest request) async {
final name = request.name ?? 'World';
final json = GreetingResponse(salutation: 'Hello', name: name);
return json;
Expand Down
1 change: 0 additions & 1 deletion dartfn/templates/json/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ dependencies:

dev_dependencies:
build_runner: ^2.2.1
functions_framework_builder: ^0.4.7
http: ^1.0.0
json_serializable: ^6.6.0
dart_flutter_team_lints: ^3.0.0
Expand Down
4 changes: 1 addition & 3 deletions docs/01-introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ Here is a simple example of how to write a function that responds to HTTP
request events with a "Hello, World!" greeting:

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

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

If you deploy this simple example to [Cloud Run], you can easily choose to make
Expand Down
3 changes: 1 addition & 2 deletions docs/quickstarts/01-quickstart-dart.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ the `FUNCTION_TARGET` environment variable is set to the new function name.
For example:

```dart
@CloudFunction()
Response handleGet(Request request) => Response.ok('Hello, World!');
Future<Response> handleGet(Request request) async => Response.ok('Hello, World!');
```

Run `build_runner` to regenerate `bin/server.dart` from `lib/functions.dart`
Expand Down
3 changes: 1 addition & 2 deletions examples/fullstack/backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ The backend demonstrates writing a function that accepts and returns JSON.
The function handler looks like this:

```dart
@CloudFunction()
GreetingResponse function(GreetingRequest request) {
Future<GreetingResponse> function(GreetingRequest request) async {
final name = request.name ?? 'World';
final json = GreetingResponse(salutation: getSalutation(), name: name);
return json;
Expand Down
34 changes: 6 additions & 28 deletions examples/fullstack/backend/bin/server.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -17,31 +16,10 @@ import 'package:backend/functions.dart' as function_library;
import 'package:functions_framework/serve.dart';

Future<void> main(List<String> args) async {
await serve(args, _nameToFunctionTarget);
await serve(args, {
'function': FunctionTarget.jsonable(
function_library.greeting,
function_library.GreetingRequest.fromJson,
),
});
}

FunctionTarget? _nameToFunctionTarget(String name) => switch (name) {
'function' => JsonWithContextFunctionTarget(
function_library.function,
(json) {
if (json is Map<String, dynamic>) {
try {
return function_library.GreetingRequest.fromJson(json);
} catch (e, stack) {
throw BadRequestException(
400,
'There was an error parsing the provided JSON data.',
innerError: e,
innerStack: stack,
);
}
}
throw BadRequestException(
400,
'The provided JSON is not the expected type '
'`Map<String, dynamic>`.',
);
},
),
_ => null
};
12 changes: 8 additions & 4 deletions examples/fullstack/backend/lib/functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ import 'api_types.dart';
// Export api_types so builder can use them when generating `bin/server.dart`.
export 'api_types.dart';

@CloudFunction()
GreetingResponse function(GreetingRequest request, RequestContext context) {
Future<GreetingResponse> greeting(
GreetingRequest request,
RequestContext context,
) async {
final name = request.name ?? 'World';
final response =
GreetingResponse(salutation: _randomSalutation(), name: name);
final response = GreetingResponse(
salutation: _randomSalutation(),
name: name,
);
context.logger.info('greetingResponse: ${response.toJson()}');
return response;
}
Expand Down
1 change: 0 additions & 1 deletion examples/fullstack/backend/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ dependencies:
dev_dependencies:
build_runner: ^2.2.1
dart_flutter_team_lints: ^3.0.0
functions_framework_builder: ^0.4.7
http: ^1.0.0
json_serializable: ^6.6.0
test: ^1.21.6
Expand Down
14 changes: 2 additions & 12 deletions examples/hello/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,8 @@
FUNCTION_TARGET = function
PORT = 8080

# bin/server.dart is the generated target for lib/functions.dart
bin/server.dart:
dart run build_runner build --delete-conflicting-outputs

build: bin/server.dart

test: clean build
test:
dart test

clean:
dart run build_runner clean
rm -rf bin/server.dart

run: build
run:
dart run bin/server.dart --port=$(PORT) --target=$(FUNCTION_TARGET)
Loading
Loading