appengine 0.7.1

Dart support for Google AppEngine #

This package provides support for running server applications written in Dart on Google App Engine using Custom Runtimes with Flex Environment.

Prerequisites #

Install Dart and Cloud SDKs #

This page assumes the Dart SDK (see dartlang.org/install) as well as the Google Cloud SDK (see cloud.google.com/sdk) were installed and their bin folders have been added to PATH.

Setup gcloud #

To ensure gcloud was authorized to access the cloud project and we have the app component installed, we assume the following has been run:

$ gcloud auth login
$ gcloud config set project <project-name>
$ gcloud components update app

Creation service account #

Furthermore in order to operate on the data of the cloud project, a service account needs to be created which allows downloading a private key in JSON format. Such a key can be obtained via the Cloud Console under IAM & Admin > Service Accounts > Create Service Account.

Creating a hello world application #

To setup a hello world application we need 4 different things:

An app/pubspec.yaml file describing the Dart package:

name: hello_world
version: 0.1.0
environment:
  sdk: '>=2.0.0 <3.0.0'

dependencies:
  appengine: ^0.6.0

An app/app.yaml file describing the AppEngine app:

runtime: custom
env: flex
service: default

An app/Dockerfile describing how to build/bundle the app:

FROM google/dart-runtime

### NOTE: Uncomment the following lines for local testing:
#ADD key.json /project/key.json
#ENV GCLOUD_KEY /project/key.json
#ENV GCLOUD_PROJECT dartlang-pub

An app/bin/server.dart containing the app code

import 'dart:io';
import 'package:appengine/appengine.dart';

requestHandler(HttpRequest request) {
  request.response
      ..write('Hello, world!')
      ..close();
}

main() async {
  await runAppEngine(requestHandler);
}

Running the app locally #

There are two ways to run the application locally - with or without docker. Both of which require a service account key.

Running without Docker #

The simplest way to run the application is on the command line like this:

$ export GCLOUD_KEY=<path-to-service-account-key.json>
$ export GCLOUD_PROJECT=<project-name>
$ dart bin/server.dart

This will serve the application at localhost:8080!

Running with Docker #

To be closer to the production environment one can run the application inside a docker container. In order to do so, docker needs to be installed first (see the official instructions.

In order to run the application locally we uncomment the 3 lines in the Dockerfile and place the service account key in under app/key.json:

ADD key.json /project/key.json
ENV GCLOUD_KEY /project/key.json
ENV GCLOUD_PROJECT dartlang-pub

We can then run the application via:

$ docker build .
...
Sucessfully built <docker-imgage-hash>
$ docker run -it <docker-imgage-hash>
...

In order to find out at which IP the Docker container is available we inspect the running container via:

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
<container-id>       ...
app % docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container-id>
172.17.0.2

Then the application will be available at 172.17.0.2:8080.

Deployment #

Before deploying the app, be sure to remove the environment variables in the Dockerfile which we used for local testing!

To deploy the application to the cloud we run the following command (optionally passing the --no-promote flag to avoid replacing the production version)

$ gcloud app deploy --no-promote app.yaml
...
Updating service [default]...done.
Deployed service [default] to [https://<version-id>-dot-<project-id>.appspot.com]
...

This will perform a remote docker build in the cloud and deploy a new version. You can find the URL to the version that got deployed in the output of gcloud app deploy (as well as via the Cloud Console under AppEngine > Versions).

Using the datastore emulator #

The gcloud sdk provides an easy-to-use datastore emulator. The emulator can be launched via

$ gcloud beta emulators datastore start
...
[datastore] If you are using a library that supports the DATASTORE_EMULATOR_HOST
[datastore] environment variable, run:
[datastore] 
[datastore]   export DATASTORE_EMULATOR_HOST=localhost:8268
[datastore] 
[datastore] Dev App Server is now running.
...

To make the application use the emulator, the DATASTORE_EMULATOR_HOST environment variable needs to be set (in addition to the other variables):

$ export DATASTORE_EMULATOR_HOST=localhost:8268
$ export GCLOUD_KEY=<path-to-service-account-key.json>
$ export GCLOUD_PROJECT=<project-name>
$ dart bin/server.dart

0.7.1 #

  • Update to use package:grpc version 2.0

0.7.0 #

  • Refactored to use package:grpc to talk to Logging and Datastore backends.

0.6.1 #

  • Added isCronJonRequest as a helper method for determining if a request originates from the AppEngine cronjob scheduler.

0.6.0 #

Breaking changes:

  • Removed poorly documented assets support with broken tests.
  • Removed memcache as the service was never made it past alpha.

Users of memcache should consider using Cloud Memorystore instead. This comes with a redis interface for which there are multiple packages available on pub. Serving assets is just a matter of sending a file from disk. This is easy to do without the logic that this package used to contain.

0.5.1+1 #

  • Support latest pkg:http and pkg:http2.

0.5.1 #

  • Correct root path for serving assets.

0.5.0 #

  • Support for Dart 2.0 constants and updated gcloud.

0.4.4+2 #

  • Fix race condition in gRPC client between http2Connection.isOpen and http2Connection.makeRequest.

0.4.4+1 #

  • Delay http/2 connection dialer by 20 ms to give client enough time to receive server settings.

0.4.4 #

  • Improve output logging when memcache connections fail.
  • Fix Dart 2 runtime issues.

0.4.3+1 #

  • When logging requests, the appengine.googleapis.com/trace_id label is populated.

0.4.3 #

  • When logging, the following protoPayload values are now populated:

    • instanceId
    • referrer
    • traceId via the X-Cloud-Trace-Context request header.
  • The appengine.googleapis.com/instance_name label is also populated for all log entries.

  • traceId was also added to the ClientContext class.

0.4.2 #

  • Add support for connecting to memcache instance defined by environment variables GAE_MEMCACHE_HOST and GAE_MEMCACHE_PORT.

0.4.1 #

  • Add shared option to runAppEngine to enable multi-threaded operation with isolates.

0.4.0+3 #

  • Fix an issue where models with un-indexed list properties could not be committed.

0.4.0+2 #

  • Be less verbose in request logs printed during local development

0.4.0 #

Switch from Managed VMs to Flexible environment:

  • Removed UsersService api.
  • Removed ModulesService api.
  • Removed RemoteApi api (was already deprecated).
  • Introduce new GCLOUD_PROJECT and GCLOUD_KEY environment variables.
  • Remove STORAGE_SERVICE_ACCOUNT_FILE environment variable.
  • To prevent duplicate logging of errors, runAppEngine will no longer log request-specific errors on stdout if they got already logged via the request-specific log.

0.3.3+1 #

  • Support gcloud package version 0.3.0.

0.3.3 #

  • Work around dev_appserver.py issue (it doesn't drain stdout we therefore avoid printing anything).

0.3.2 #

  • Require protobuf package ^0.5.0
  • Support the lastest release of fixnum package.

0.3.1+1 #

  • Improved output of Logger.root with useLoggingPackageAdaptor.

0.3.1 #

  • Added optional port argument to runAppEngine.
  • Removed the call to /bin/hostname.

0.3.0+1 #

  • Widen dependency constraint on package:logging.

0.3.0 #

  • Pass the memcache expiration time to the memcache service. Before the expiration argument to Memcache.set and Memcache.setAll was ignored.

  • Removed the expiration argument to Memcache.clear. It is not supported by the App Engine memcache API.

0.2.6+3 #

  • Update dependencies to allow gcloud 0.2.0 with Cloud Pub/Sub support.

0.2.6+2 #

  • Do not close authClient, since registerStorageService does it automatically.

0.2.6+1 #

  • Correctly handling x-appengine-https header.
  • Turn logging of for Level.OFF

0.2.6 #

  • Added adaptor for package:logging via useLoggingPackageAdaptor().
  • Added workaround for incorrect requestedUri comming from 'dart:io'.

0.2.5 #

  • Added withAppEngineServices() function which allows running arbitrary code using AppEngine services via a service scope.

0.2.4+1 #

  • Change the service scope keys keys to non-private symbols.

0.2.4 #

  • Run request handlers inside a service scope (see package:gcloud/service_scope.dart).
  • Insert an authenticated HTTP client into the service scope.
  • Insert a memcacheService into the service scope.
  • Added isDevelopmentServer and isProductionEnvironment getter to client context.
  • Make hostnames returned from modules service use -dot- naming to support HTTPS.
  • Mark package:appengine/remote_api.dart as deprecated.

0.2.3 #

  • Small bugfix in the lowlevel memcache API implementation.

0.2.2 #

  • Updated README.md
  • Widen googleapis_auth constraint to include version 0.2.0

0.2.1 #

  • Small bugfix in storage API implementation
  • respect DART_PUB_SERVE url only in developer mode
  • sync db/datastore tests to the ones used in gcloud

0.2.0 #

  • Use datastore/db APIs from package:gcloud
    • simplified annotation system
    • paging-based query API
  • Added module service
  • Some bugfixes

0.1.0 #

  • Alpha release

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  appengine: ^0.7.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:appengine/appengine.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
71
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
83
Learn more about scoring.

We analyzed this package on Jul 15, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: other

Primary library: package:appengine/appengine.dart with components: io, mirrors.

Health suggestions

Format lib/appengine.dart.

Run dartfmt to format lib/appengine.dart.

Maintenance suggestions

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and appengine.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.0 <3.0.0
fixnum ^0.10.0 0.10.9
gcloud ^0.6.0 0.6.0+4
googleapis_auth ^0.2.8 0.2.10
grpc ^2.0.0 2.0.0+1
http ^0.12.0 0.12.0+2
http2 ^1.0.0 1.0.0
http_server ^0.9.2 0.9.8+3
logging ^0.11.0 0.11.3+2
path ^1.6.0 1.6.2
protobuf ^0.13.11 0.13.15
stack_trace ^1.9.0 1.9.3
Transitive dependencies
_discoveryapis_commons 0.1.8+1
async 2.2.0
charcode 1.1.2
collection 1.14.11
convert 2.1.1
crypto 2.0.6
googleapis 0.54.0
http_parser 3.1.3
meta 1.1.7
mime 0.9.6+3
source_span 1.5.5
string_scanner 1.0.4
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
pedantic ^1.7.0 1.8.0+1
test ^1.6.0

Admin