Code Monkey home page Code Monkey logo

appengine's Introduction

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

appengine's People

Contributors

anders-sandholm avatar domesticmouse avatar enyo avatar isoos avatar jakobr-google avatar jcollins-g avatar jonasfj avatar kevmoo avatar kwalrath avatar leonsenft avatar mkustermann avatar scarygami avatar sgjesse avatar tvolkert avatar wibling avatar

Watchers

 avatar  avatar

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.