Running a Dart server on an App Engine Managed VM

Dart applications can be hosted on the Google Cloud Platform—they run in a custom runtime on App Engine Managed VM. In contrast to the secure, sandboxed environments in which Python, Java, Go, and PHP are run, Dart runs in a VM-based hosting environment, which is more flexible, providing more CPU and memory options.

In the previous chapters, we learned how to write HTTP clients and servers in Dart, which is exactly what we need for a Dart app running in a managed VM.

This environment uses the Docker cloud service to distribute applications. Docker is a tool, written in the Go programming language, to package an application and its dependencies in a virtual container that can run on any Linux server. Docker can be used with various infrastructure tools besides Google Cloud Platform, a few being Amazon Web Services, Chef, Jelastic, Jenkins, Microsoft Azure, OpenStack Nova, OpenSVC, Puppet, and Salt. If you do not develop on Linux, you will need a VM running Linux; boot2docker is a pre-packaged Linux VM image for VirtualBox.

Go through the following steps to deploy Dart on App Engine:

  1. To install and configure Docker and related tools, visit http://docs.docker.com/installation/windows/.
  2. To install and set up a Google Cloud SDK project, visit https://www.dartlang.org/server/google-cloud-platform/app-engine/setup.html.

Use the following steps to create your Dart project:

  1. You need a dependency on the appengine package, so add this to your pubspec.yaml file.
  2. You need an app.yaml file with the following as minimal content:
    version: app_name
    runtime: custom
    vm: true
    api_version: 1
  3. Your server code must reside in a binserver.dart file and contain the following start up code:
    import 'dart:io';
    import 'package:appengine/appengine.dart';              (1)
    main() {
      runAppEngine((HttpRequest request) {                  (2)
        request.response.write(My first Dart App Engine App!')
                        .close();
      });
    }

    In line (1), the appengine package is imported. This includes the top-level runAppEngine() method, which starts Dart's runtime and connects it to App Engine. This takes an HTTP request handler as callback, which creates the response for the browser.

  4. To start the app using App Engine, run on a command line:
    gcloud preview app run app.yaml
    
  5. Navigate to http://localhost:8080 using your browser to see the result and start developing and testing it locally.
  6. When you are satisfied, you can deploy your app to App Engine using the following command:
    gcloud preview app deploy app.yaml
    

    After deployment is completed your app can be reached on the World Wide Web (WWW) via http://app_name.appspot.com/.

Before embarking on your own project, study and experiment with a worked out example of a client-server app available at https://www.dartlang.org/server/google-cloud-platform/app-engine/client-server/. Other examples can be found at https://github.com/dart-lang/appengine_samples.

Note

You can find the latest news on Dart in App Engine on https://www.dartlang.org/server/google-cloud-platform/app-engine/.

For more information on App Engine, consult at https://cloud.google.com/appengine/docs/managed-vms/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.152.58