Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 --- | |
| 2 layout: tutorial | |
| 3 title: "Code Lab: Deploy the Pirates" | |
| 4 description: "Deploy your Dart app to the internet." | |
| 5 has-permalinks: true | |
| 6 tutorial: | |
| 7 id: buildanddeploy | |
| 8 js: | |
| 9 - url: /js/os-switcher.js | |
| 10 defer: true | |
| 11 - url: /js/editor-downloads-analytics.js | |
| 12 defer: true | |
| 13 - url: /js/editor-version.js | |
| 14 defer: true | |
| 15 header: | |
| 16 css: ["/codelabs/darrrt/darrrt.css"] | |
| 17 --- | |
| 18 | |
| 19 # {{ page.title }} | |
| 20 | |
| 21 After you finish developing a web app, | |
| 22 you need to put it on the internet so that people can use it. | |
| 23 | |
| 24 The following diagram summarizes the process | |
| 25 for deploying a Dart web app. | |
| 26 | |
| 27  | |
| 28 | |
| 29 In this code lab, | |
| 30 you learn to launch an app to the internet using | |
| 31 <a href="https://devcenter.heroku.com/">Heroku</a>, | |
| 32 which is a cloud application platform | |
| 33 for building and deploying web apps. | |
| 34 Heroku is just one of many options | |
| 35 you could choose for deploying an app. | |
| 36 Other options include | |
| 37 Google App Engine and Amazon Web Services. | |
| 38 | |
| 39 In this code lab, you deploy the Pirate Badge app from the | |
| 40 [Code Lab: Pirate Convention](/codelabs/darrrt/). | |
| 41 By doing so, you learn how to build the app for deployment, | |
| 42 how to write a basic static file server, | |
| 43 which Heroku requires to serve your app, | |
| 44 and finally, how to deploy to Heroku. | |
| 45 | |
| 46 This code lab assumes that you have written and run | |
| 47 a Dart web app before. | |
| 48 If you have not, consider doing the | |
| 49 [Code Lab: Pirate Convention](/codelabs/darrrt/). | |
| 50 | |
| 51 <hr> | |
| 52 | |
| 53 <div markdown="1"> | |
| 54 | |
| 55 ## Table of Contents | |
| 56 | |
| 57 * [Step 0: Set up](#step-zero) | |
| 58 * [Step 1: Run the app in Dart Editor](#step-one) | |
| 59 * [Step 2: Build the app and run as JavaScript](#step-two) | |
| 60 * [Step 3: Code walkthrough of a static server](#step-three) | |
|
Kathy Walrath
2014/01/16 00:01:29
It's weird to have a non-action-oriented name. May
mem
2014/01/16 22:49:00
Done.
| |
| 61 * [Step 4: Deploy to Heroku](#step-four) | |
| 62 * [Step 5: Run the app online and mobile](#step-five) | |
| 63 * [Resources](#resources) | |
| 64 * [What next?](#what-next) | |
| 65 | |
| 66 {% comment %} | |
| 67 consider writing section showing how to use | |
| 68 dev tools to run web page on phone | |
| 69 {% endcomment %} | |
| 70 | |
| 71 </div> | |
| 72 | |
| 73 <hr> | |
| 74 | |
| 75 ##Step 0: Set up {#step-zero} | |
| 76 | |
| 77 In this step, you download Dart and get the code for the app that you will be de ploying. | |
| 78 This code lab uses Dart Editor. | |
| 79 | |
| 80 ### <i class="fa fa-anchor"> </i> Get Dart. | |
| 81 | |
| 82 <div class="trydart-step-details" markdown="1"> | |
| 83 If you haven't already done so, | |
| 84 get the Dart download. | |
| 85 Unzip the ZIP file, which creates a directory called `dart`. | |
| 86 | |
| 87 <!--style here is a hack to remove the arrow, which was only partially showing. --> | |
| 88 <div style="padding-left: 10px"> | |
| 89 {% include downloads/_dart-editor.html buttonclass="btn btn-primary btn-lg" %} | |
| 90 </div> | |
| 91 | |
| 92 <p class="os-choices" markdown="1"> | |
| 93 The Dart tools | |
| 94 work in recent versions of | |
| 95 {% include os-choices.html %} | |
| 96 </p> | |
| 97 </div> | |
| 98 | |
| 99 ### <i class="fa fa-anchor"> </i> Start Dart Editor. | |
| 100 | |
| 101 <div class="trydart-step-details" markdown="1"> | |
| 102 Go to the `dart` directory and double-click **DartEditor**. | |
| 103 | |
| 104 **Got questions? Having trouble?** Go to the | |
| 105 [Troubleshooting Dart Editor](/tools/editor/troubleshoot.html) page. | |
| 106 | |
| 107 </div> | |
| 108 | |
| 109 ### <i class="fa fa-anchor"> </i> Get the code. | |
| 110 | |
| 111 <div class="trydart-step-details" markdown="1"> | |
| 112 <a href="https://github.com/marycampione/deploy-codelab/archive/master.zip">Down load</a> | |
| 113 the code for the app that you will deploy. | |
| 114 Unzip the ZIP file, | |
| 115 which creates a directory called `deploy-codelab-master`. | |
| 116 </div> | |
| 117 | |
| 118 ### <i class="fa fa-anchor"> </i> Open deploy-codelab-master. | |
| 119 | |
| 120 <div class="trydart-step-details" markdown="1"> | |
| 121 In Dart Editor, | |
| 122 use **File > Open Existing Folder...** | |
| 123 to open the `deploy-codelab-master` directory. | |
| 124 </div> | |
| 125 | |
| 126 <div class="row"> <div class="col-md-7" markdown="1"> | |
| 127 | |
| 128  | |
| 129 | |
| 130 </div> <div class="col-md-5" markdown="1"> | |
| 131 | |
| 132 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong> | |
| 133 | |
| 134 * The `packages` directory, as well as the `pubspec.yaml` and `pubspec.lock` fil es are | |
| 135 related to package dependencies. | |
| 136 This project has all the dependencies set up for you. | |
| 137 Dart Editor automatically installs the necessary packages. | |
| 138 | |
| 139 * The `web` directory contains the Pirate Badge app to deploy. | |
| 140 | |
| 141 * The `bin` directory contains the code for a static file server. | |
| 142 Heroku requires a server with your project. | |
| 143 | |
| 144 {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %} | |
| 145 | |
| 146 </div> </div> | |
| 147 | |
| 148 <hr> | |
| 149 | |
| 150 ##Step 1: Run the app in Dart Editor {#step-one} | |
| 151 | |
| 152 Open the code for the Pirate Badge web app and run it locally. | |
| 153 | |
| 154 ### <i class="fa fa-anchor"> </i> Expand the `web` directory. | |
| 155 | |
| 156 <div class="trydart-step-details" markdown="1"> | |
| 157 In Dart Editor, expand the `web` directory | |
| 158 by clicking the little arrow | |
| 159  to the left of its name. | |
| 160 The directory contains four files: | |
| 161 | |
| 162  | |
| 163 | |
| 164 </div> | |
| 165 | |
| 166 ### <i class="fa fa-anchor"> </i> Two notes about `piratebadge.html` | |
|
Kathy Walrath
2014/01/16 00:01:29
Um... What are the two notes?
Even if you're refe
mem
2014/01/16 22:49:00
Done.
mem
2014/01/16 22:49:00
Done.
| |
| 167 | |
| 168 <div class="row"> <div class="col-md-7"> | |
| 169 | |
| 170 <div class="trydart-step-details" markdown="1"> | |
| 171 | |
| 172 Double click on `piratebadge.html` to open it. | |
|
Kathy Walrath
2014/01/16 00:01:29
Double click on -> Double-click
(no "on", and use
mem
2014/01/16 22:49:00
Done.
| |
| 173 | |
| 174 {% prettify html %} | |
| 175 ... | |
| 176 <head> | |
| 177 <meta charset="utf-8"> | |
| 178 <title>Pirate badge</title> | |
| 179 [[highlight]]<meta name="viewport" content="width=device-width, initial-scale= 1.0">[[/highlight]] | |
| 180 <link rel="stylesheet" href="piratebadge.css"> | |
| 181 </head> | |
| 182 <body> | |
| 183 ... | |
| 184 [[highlight]]<script src="packages/browser/dart.js"></script>[[/highlight]] | |
| 185 ... | |
| 186 </body> | |
| 187 ... | |
| 188 {% endprettify %} | |
| 189 | |
| 190 </div> | |
| 191 | |
| 192 <div class="trydart-filename">piratedbadge.dart</div> | |
| 193 | |
| 194 </div> <div class="col-md-5" markdown="1"> | |
| 195 | |
| 196 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong> | |
| 197 | |
| 198 * The <code><meta></code> tag sets attributes to | |
| 199 make the app scale correctly on mobile devices. | |
| 200 | |
| 201 * `dart.js` is a script that determines if the browser supports Dart, | |
| 202 and if it does, runs the app natively. | |
| 203 If it does not, the script loads the JavaScript version of the app. | |
| 204 | |
| 205 * `dart.js` is part of the `browser` pub package. | |
| 206 | |
| 207 </div></div> | |
| 208 | |
| 209 ### <i class="fa fa-anchor"> </i> Run the app. | |
| 210 | |
| 211 | |
| 212 <div class="row"> <div class="col-md-7"> | |
| 213 | |
| 214 <div class="trydart-step-details" markdown="1"> | |
| 215 | |
| 216 Click on `piratebadge.html` | |
|
Kathy Walrath
2014/01/16 00:01:29
Click on -> Select
mem
2014/01/16 22:49:00
Done.
mem
2014/01/16 22:49:00
Done.
| |
| 217 and click the Run button | |
| 218 <img src="images/run.png" width="16" height="16" | |
| 219 alt="Run button">. | |
| 220 | |
| 221  | |
| 222 | |
| 223 </div> | |
| 224 | |
| 225 </div> <div class="col-md-5" markdown="1"> | |
| 226 | |
| 227 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong> | |
| 228 | |
| 229 * Dart Editor launches *Dartium*, a special build of Chromium | |
| 230 that has the Dart Virtual Machine built in, and loads the app. | |
| 231 | |
| 232 </div></div> | |
| 233 | |
| 234 <div class="row"> <div class="col-md-7"> | |
| 235 | |
| 236 <div class="trydart-step-details" markdown="1"> | |
| 237 | |
| 238 You should see a text field and a button on the left, | |
|
Kathy Walrath
2014/01/16 00:01:29
Describing the image seems weird, plus "as below".
mem
2014/01/16 22:49:00
Done.
| |
| 239 and a red and white name badge on the right as below. | |
| 240 | |
| 241 <iframe class="running-app-frame" | |
| 242 style="height:220px;width:550px;" | |
| 243 src="/codelabs/darrrt/examples/6-piratebadge_json/piratebadge.html"> | |
| 244 </iframe> | |
| 245 | |
| 246 </div> | |
| 247 | |
| 248 </div> <div class="col-md-5" markdown="1"> | |
| 249 | |
| 250 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong> | |
| 251 | |
| 252 * At this time, Dartium is the only browser that has the Dart VM built in. | |
| 253 To run a Dart app in other browsers you need to convert it to JavaScript, | |
| 254 which you do in the next step. | |
| 255 | |
| 256 {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %} | |
| 257 | |
| 258 </div></div> | |
| 259 | |
| 260 <hr> | |
| 261 | |
| 262 ##Step 2: Build the app and run as JavaScript {#step-two} | |
| 263 | |
| 264 In this step, you use `pub build` to | |
| 265 generate the assets for the app | |
| 266 and put them into a new directory named `build`. | |
| 267 In addition to other tasks, | |
| 268 this process generates minimized JavaScript that | |
| 269 can be run by any modern browser. | |
| 270 | |
| 271 ### <i class="fa fa-anchor"> </i> Check out pubspec.yaml | |
| 272 | |
| 273 <div class="row"> <div class="col-md-7"> | |
| 274 | |
| 275 <div class="trydart-step-details" markdown="1"> | |
| 276 | |
| 277 Double click the `pubspec.yaml` file to open it. | |
|
Kathy Walrath
2014/01/16 00:01:29
Double click -> Double-click
mem
2014/01/16 22:49:00
Done.
| |
| 278 | |
| 279 {% prettify dart %} | |
| 280 name: deploy_codelab | |
| 281 description: A sample deployment | |
| 282 dependencies: | |
| 283 [[highlight]]browser: any[[/highlight]] | |
| 284 [[highlight]]http_server: any[[/highlight]] | |
| 285 [[highlight]]path: any[[/highlight]] | |
| 286 {% endprettify %} | |
| 287 | |
| 288 </div> | |
| 289 | |
| 290 <div class="trydart-filename">pubspec.yaml</div> | |
| 291 | |
| 292 </div> <div class="col-md-5" markdown="1"> | |
| 293 | |
| 294 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong> | |
| 295 | |
| 296 * A `pubspec.yaml` file in a directory identifies the directory | |
| 297 and its contents as an application. | |
| 298 | |
| 299 * `pubspec.yaml` configures the application | |
| 300 and lists the libraries on which the app depends. | |
| 301 The three libraries needed by this app are all hosted on | |
| 302 [pub.dartlang.org](https://pub.dartlang.org/)) | |
| 303 | |
| 304 * `any` selects the latest package that matches your SDK. | |
| 305 | |
| 306 </div></div> | |
| 307 | |
| 308 ### <i class="fa fa-anchor"> </i> Look at the packages directory | |
| 309 | |
| 310 <div class="row"> <div class="col-md-7"> | |
| 311 | |
| 312 <div class="trydart-step-details" markdown="1"> | |
| 313 | |
| 314 In Dart Editor, expand the `packages` directory. | |
| 315 | |
| 316  | |
| 317 | |
| 318 </div> | |
| 319 | |
| 320 </div> <div class="col-md-5" markdown="1"> | |
| 321 | |
| 322 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong> | |
| 323 | |
| 324 * The `packages` directory contains the code for all of the dependencies | |
| 325 listed in the `pubspec.yaml` file. | |
| 326 These are installed automatically by Dart Editor. | |
| 327 | |
| 328 * The `browser` package contains the `dart.js` script | |
| 329 that checks for native Dart support. | |
| 330 | |
| 331 * The `http_server` package implements high-level HTTP server | |
| 332 functionality making it easier to provide content through HTTP servers. | |
| 333 | |
| 334 * The `path` package provides common path manipulation operations, | |
| 335 such as joining, splitting, and normalizing. | |
| 336 | |
| 337 * The `mime` package is included because `http_server` has a dependency on it. | |
|
Kathy Walrath
2014/01/16 00:01:29
has a dependency --> depends
mem
2014/01/16 22:49:00
Done.
| |
| 338 | |
| 339 </div></div> | |
| 340 | |
| 341 ### <i class="fa fa-anchor"> </i> Run pub build | |
| 342 | |
| 343 <div class="row"> <div class="col-md-7"> | |
| 344 | |
| 345 <div class="trydart-step-details" markdown="1"> | |
| 346 | |
| 347 Bring up a terminal window, | |
|
Kathy Walrath
2014/01/16 00:01:29
I was surprised this was in a terminal window, ins
mem
2014/01/16 22:49:00
it wasn't working in Dart Editor.
now it does.
| |
| 348 change into the `deploy-codelab-master` directory, | |
| 349 and run `pub build`. | |
| 350 | |
| 351 {% prettify bash %} | |
| 352 $ pub build | |
| 353 Building deploy_codelab..... | |
| 354 [Info in Dart2JS]: | |
| 355 Generated deploy_codelab|web/piratebadge.dart.js (98287 characters) in 0:00:06.2 35360 | |
| 356 Built 8 files! | |
| 357 $ | |
| 358 {% endprettify %} | |
| 359 | |
| 360 </div> | |
| 361 | |
| 362 <div class="trydart-filename">terminal</div> | |
| 363 | |
| 364 </div> <div class="col-md-5" markdown="1"> | |
| 365 | |
| 366 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong> | |
| 367 | |
| 368 * The `pub build` command creates a `build` directory that contains | |
| 369 everything your app needs to be deployed, including a minimized | |
| 370 JavaScript file and the required packages. | |
| 371 | |
| 372 </div></div> | |
| 373 | |
| 374 ### <i class="fa fa-anchor"> </i> Look at the `build` directory | |
| 375 | |
| 376 <div class="row"> <div class="col-md-7"> | |
| 377 | |
| 378 <div class="trydart-step-details" markdown="1"> | |
| 379 | |
| 380 The `pub build` command creates a `build` directory | |
| 381 under `deploy-codelab-master`. | |
| 382 | |
| 383  | |
| 384 </div> | |
| 385 | |
| 386 </div> <div class="col-md-5" markdown="1"> | |
| 387 | |
| 388 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong> | |
| 389 | |
| 390 * The `piratebadge.dart.js` file is JavaScript file that has been minimized. | |
|
Kathy Walrath
2014/01/16 00:01:29
is -> is a
mem
2014/01/16 22:49:00
Done.
| |
| 391 When deployed, this file runs in the browser. | |
| 392 | |
| 393 * The `packages` directory contains the package dependencies. | |
| 394 | |
| 395 * Note that the `build` directory contains no `piratebadge.dart` file. | |
| 396 It is not needed to deploy the app to JavaScript. | |
| 397 | |
| 398 </div></div> | |
| 399 | |
| 400 | |
| 401 ### <i class="fa fa-anchor"> </i> Run the app as JavaScript | |
| 402 | |
| 403 <div class="row"> <div class="col-md-7"> | |
| 404 | |
| 405 <div class="trydart-step-details" markdown="1"> | |
| 406 | |
| 407 Open the app. | |
| 408 Select **File > Open File...** | |
| 409 in a browser such a Firefix or Safari | |
| 410 and select the | |
| 411 `deploy-codelab-master/build/piratebadge.html` file. | |
| 412 | |
| 413 </div> | |
| 414 | |
| 415 </div> <div class="col-md-5" markdown="1"> | |
| 416 | |
| 417 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong> | |
| 418 | |
| 419 * The app is running on the local machine using the `file` protocol. | |
| 420 To share your app with others, you need to deploy the app to a hosting service , | |
| 421 such as Heroku. | |
| 422 | |
| 423 </div></div> | |
| 424 | |
| 425 <hr> | |
| 426 | |
| 427 ##Step 3: Code walkthrough of a static server {#step-three} | |
| 428 | |
|
Kathy Walrath
2014/01/16 00:01:29
I'd like some wording here about how you don't nee
mem
2014/01/16 22:49:00
Done.
| |
| 429 Heroku requires that you provide a static file server with your project. | |
| 430 One is provided with the sample code for this code lab. | |
| 431 This step walks through the code for the server, | |
| 432 which uses some interesting Dart APIs and | |
| 433 two helpful pub packages. | |
| 434 | |
| 435 ### <i class="fa fa-anchor"> </i> Setting the path to the app. | |
| 436 | |
| 437 <div class="row"> <div class="col-md-7" markdown="1"> | |
| 438 | |
| 439 <div class="trydart-step-details" markdown="1"> | |
| 440 | |
| 441 {% prettify dart %} | |
| 442 import 'dart:io' show File, HttpServer, [[highlight]]Platform[[/highlight]]; | |
| 443 import 'dart:async' show runZoned; | |
| 444 import 'package:http_server/http_server.dart' show VirtualDirectory; | |
| 445 import 'package:path/path.dart' show [[highlight]]join, dirname;[[/highlight]] | |
| 446 | |
| 447 void main() { | |
| 448 [[highlight]]// Assumes the server lives in bin/ and that `pub build` ran[[/hi ghlight]] | |
| 449 [[highlight]]var pathToBuild = join(dirname(Platform.script.toFilePath()),[[/h ighlight]] | |
| 450 [[highlight]]'..', 'build');[[/highlight]] | |
| 451 | |
| 452 ... | |
| 453 } | |
| 454 {% endprettify %} | |
| 455 </div> | |
| 456 | |
| 457 <div class="trydart-filename">basic_http_server.dart</div> | |
| 458 | |
| 459 </div> <div class="col-md-5" markdown="1"> | |
| 460 | |
| 461 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong> | |
| 462 | |
| 463 * This code assumes that `pub build` has run. | |
| 464 | |
| 465 * In the next step, you will configure a Heroku buildpack | |
| 466 that runs `pub build` when you upload your app. | |
| 467 | |
| 468 * This code uses `join` along with `dirname` to | |
| 469 build a path to the `build` directory | |
| 470 based on the path of the running script. | |
| 471 | |
| 472 {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %} | |
| 473 | |
| 474 </div> </div> | |
| 475 | |
| 476 ### <i class="fa fa-anchor"> </i> Serving `piratebadge.html`. | |
| 477 | |
| 478 <div class="row"> <div class="col-md-7" markdown="1"> | |
| 479 | |
| 480 <div class="trydart-step-details" markdown="1"> | |
| 481 | |
| 482 {% prettify dart %} | |
| 483 import 'dart:io' show [[highlight]]File[[/highlight]], HttpServer, Platform; | |
| 484 import 'dart:async' show runZoned; | |
| 485 import 'package:http_server/http_server.dart' show [[highlight]]VirtualDirectory ;[[/highlight]] | |
| 486 import 'package:path/path.dart' show join, dirname; | |
| 487 | |
| 488 void main() { | |
| 489 ... | |
| 490 [[highlight]]var staticFiles = new VirtualDirectory(pathToBuild);[[/highlight] ] | |
| 491 [[highlight]]staticFiles.allowDirectoryListing = true;[[/highlight]] | |
| 492 [[highlight]]staticFiles.directoryHandler = (dir, request) {[[/highlight]] | |
| 493 [[highlight]]// Redirect directory-requests to piratebadge.html file.[[/high light]] | |
| 494 [[highlight]]var indexUri = new Uri.file(dir.path).resolve('piratebadge.html ');[[/highlight]] | |
| 495 [[highlight]]staticFiles.serveFile(new File(indexUri.toFilePath()), request) ;[[/highlight]] | |
| 496 [[highlight]]};[[/highlight]] | |
| 497 ... | |
| 498 } | |
| 499 {% endprettify %} | |
| 500 </div> | |
| 501 | |
| 502 <div class="trydart-filename">basic_http_server.dart</div> | |
| 503 | |
| 504 </div> <div class="col-md-5" markdown="1"> | |
| 505 | |
| 506 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong> | |
| 507 | |
| 508 * The `VirtualDirectory` class from the `http_server` package | |
| 509 provides a high-level interface for serving static files and directory listing s to HttpRequests. | |
| 510 | |
| 511 * The code sets `allowDirectoryListing` to `true` so that all files | |
| 512 from the app can be served [xx I made that up]. | |
| 513 | |
| 514 * This code redirects directory requests to `piratebadge.html`, | |
| 515 which is the main HTML file for this app. | |
| 516 This works because there is only one directory. | |
| 517 | |
| 518 * The `serveFile` method serves the requested static file | |
| 519 to the given HttpRequest object. | |
| 520 | |
| 521 * Without the `http_server` class, this code would be longer and more complex, | |
| 522 because you would have to use the lower-level `HttpServer`, `HttpRequest`, | |
| 523 and `HttpResponse` APIs. | |
| 524 | |
| 525 {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %} | |
| 526 | |
| 527 </div> </div> | |
| 528 | |
| 529 ### <i class="fa fa-anchor"> </i> Using variable port numbers. | |
| 530 | |
| 531 <div class="row"> <div class="col-md-7" markdown="1"> | |
| 532 | |
| 533 <div class="trydart-step-details" markdown="1"> | |
| 534 | |
| 535 {% prettify %} | |
| 536 import 'dart:io' show File, HttpServer, [[highlight]]Platform[[/highlight]]; | |
| 537 import 'dart:async' show runZoned; | |
| 538 import 'package:http_server/http_server.dart' show VirtualDirectory; | |
| 539 import 'package:path/path.dart' show join, dirname; | |
| 540 | |
| 541 void main() { | |
| 542 ... | |
| 543 [[highlight]]var portEnv = Platform.environment['PORT'];[[/highlight]] | |
| 544 [[highlight]]var port = portEnv == null ? 9999 : int.parse(portEnv);[[/highlig ht]] | |
| 545 ... | |
| 546 } | |
| 547 {% endprettify %} | |
| 548 </div> | |
| 549 | |
| 550 <div class="trydart-filename">basic_http_server.dart</div> | |
| 551 | |
| 552 </div> <div class="col-md-5" markdown="1"> | |
| 553 | |
| 554 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong> | |
| 555 | |
| 556 * Heroku uses variable ports. Heroku sets the `$PORT` environment variable | |
| 557 with the port number that your app should listen to. | |
| 558 | |
| 559 * This code uses the port specified in the `$PORT` environment variable, | |
| 560 if present. Otherwise, it uses port 9999. | |
| 561 | |
| 562 * The `$PORT` environment variable might not be present if the server is running | |
| 563 in a non-Heroku environment. | |
| 564 | |
| 565 {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %} | |
| 566 | |
| 567 </div> </div> | |
| 568 | |
| 569 | |
| 570 ### <i class="fa fa-anchor"> </i> Using `runZoned()`. | |
| 571 | |
| 572 <div class="row"> <div class="col-md-7" markdown="1"> | |
| 573 | |
| 574 <div class="trydart-step-details" markdown="1"> | |
| 575 | |
| 576 | |
| 577 {% prettify dart %} | |
| 578 import 'dart:io' show File, HttpServer, Platform; | |
| 579 import 'dart:async' show [[highlight]]runZoned[[/highlight]]; | |
| 580 import 'package:http_server/http_server.dart' show VirtualDirectory; | |
| 581 import 'package:path/path.dart' show join, dirname; | |
| 582 | |
| 583 void main() { | |
| 584 ... | |
| 585 [[highlight]]runZoned(() {[[/highlight]] | |
| 586 ... | |
| 587 [[highlight]]},[[/highlight]] | |
| 588 [[highlight]]onError: (e, stackTrace) => print('Oh noes! $e $stackTrace'));[[/ highlight]] | |
| 589 } | |
| 590 {% endprettify %} | |
| 591 </div> | |
| 592 | |
| 593 <div class="trydart-filename">basic_http_server.dart</div> | |
| 594 | |
| 595 </div> <div class="col-md-5" markdown="1"> | |
| 596 | |
| 597 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong> | |
| 598 | |
| 599 * `runZoned` helps provide stability to your program. | |
| 600 It catches exceptions from all call stacks within its bounds, | |
| 601 allowing your program to continue running in spite of errors. | |
| 602 | |
| 603 {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %} | |
| 604 | |
| 605 </div> </div> | |
| 606 | |
| 607 ### <i class="fa fa-anchor"> </i> About HTTP requests. | |
| 608 | |
| 609 <div class="row"> <div class="col-md-7" markdown="1"> | |
| 610 | |
| 611 <div class="trydart-step-details" markdown="1"> | |
| 612 | |
| 613 | |
| 614 {% prettify dart %} | |
| 615 import 'dart:io' show File, [[highlight]]HttpServer[[/highlight]], Platform; | |
| 616 import 'dart:async' show runZoned; | |
| 617 import 'package:http_server/http_server.dart' show VirtualDirectory; | |
| 618 import 'package:path/path.dart' show join, dirname; | |
| 619 | |
| 620 void main() { | |
| 621 ... | |
| 622 runZoned(() { | |
| 623 [[highlight]]HttpServer.bind('0.0.0.0', port).then((server) {[[/highlight]] | |
| 624 [[highlight]]server.listen(staticFiles.serveRequest);[[/highlight]] | |
| 625 [[highlight]]});[[/highlight]] | |
| 626 }, | |
| 627 onError: (e, stackTrace) => print('Oh noes! $e $stackTrace')); | |
| 628 } | |
| 629 {% endprettify %} | |
| 630 </div> | |
| 631 | |
| 632 <div class="trydart-filename">basic_http_server.dart</div> | |
| 633 | |
| 634 </div> <div class="col-md-5" markdown="1"> | |
| 635 | |
| 636 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong> | |
| 637 | |
| 638 * Creates an `HttpServer` object which responds to HTTP requests | |
|
Kathy Walrath
2014/01/16 00:01:29
Subject is missing here
mem
2014/01/16 22:49:00
Done.
| |
| 639 for this app. | |
| 640 | |
| 641 * The server binds to a host `0.0.0.0`. | |
| 642 which is is a special address that is guaranteed to receive from any network s ocket. | |
| 643 If you don't know what specific IP to listen to, you can use 0.0.0.0. | |
| 644 | |
| 645 * The server listens for requests and uses the `VirtualDirectory` object | |
| 646 named `staticFiles` to handle them. | |
| 647 | |
| 648 {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %} | |
| 649 | |
| 650 </div> </div> | |
| 651 | |
| 652 <hr> | |
| 653 | |
| 654 ##Step 4: Deploy to Heroku {#step-four} | |
| 655 | |
| 656 In this step, you deploy your server and app to Heroku. | |
| 657 | |
| 658 ### <i class="fa fa-anchor"> </i> Create a `Procfile` | |
| 659 | |
| 660 <div class="row"> <div class="col-md-7" markdown="1"> | |
| 661 | |
| 662 <div class="trydart-step-details" markdown="1"> | |
| 663 | |
| 664 In the `deploy-codelab-master` directory, create a file | |
| 665 named `Procfile` and add the following line to it. | |
| 666 | |
| 667 {% prettify bash %} | |
| 668 web: ./dart-sdk/bin/dart bin/basic_http_server.dart | |
| 669 {% endprettify %} | |
| 670 | |
| 671 </div> | |
| 672 | |
| 673 <div class="trydart-filename">Procfile</div> | |
| 674 | |
| 675 </div> <div class="col-md-5" markdown="1"> | |
| 676 | |
| 677 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong> | |
| 678 | |
| 679 * A `Procfile` tells Heroku what part of your app can be executed. | |
| 680 | |
| 681 * The `Procfile` for this example defines a web process type | |
| 682 in which the dart VM runs the static file server. | |
| 683 | |
| 684 {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %} | |
| 685 | |
| 686 </div> </div> | |
| 687 | |
| 688 | |
| 689 ### <i class="fa fa-anchor"> </i> Commit your changes to your local `deploy-code lab-master` git repo | |
| 690 | |
| 691 <div class="row"> <div class="col-md-7" markdown="1"> | |
| 692 | |
| 693 <div class="trydart-step-details" markdown="1"> | |
| 694 | |
| 695 {% prettify bash %} | |
| 696 $ cd deploy-codelab-master | |
| 697 $ git init | |
| 698 $ git add -A . | |
| 699 $ git commit -am "make it so" | |
| 700 $ | |
| 701 {% endprettify %} | |
| 702 | |
| 703 </div> | |
| 704 | |
| 705 <div class="trydart-filename">terminal</div> | |
| 706 | |
| 707 </div> <div class="col-md-5" markdown="1"> | |
| 708 | |
| 709 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong> | |
| 710 | |
| 711 * First, create a local repo with `git init`. | |
| 712 | |
| 713 * Next, add all of the files in `deploy-codelab-master` to it. | |
| 714 | |
| 715 * Finally, commit the new files in the `build` directory to your local | |
| 716 git repo. | |
| 717 | |
| 718 {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %} | |
| 719 | |
| 720 </div> </div> | |
| 721 | |
| 722 ### <i class="fa fa-anchor"> </i> Create a Heroku app and configure it. | |
| 723 | |
| 724 <div class="row"> <div class="col-md-7" markdown="1"> | |
| 725 | |
| 726 <div class="trydart-step-details" markdown="1"> | |
| 727 | |
| 728 <strong>Important:</strong> Replace | |
| 729 `myfirstdartappforheroku` with something unique. | |
| 730 | |
| 731 {% prettify bash %} | |
| 732 $ heroku create [[highlight]]myfirstdartappforheroku[[/highlight]] -s cedar | |
| 733 $ heroku labs:enable user-env-compile | |
| 734 $ heroku config:set DART_SDK_URL=https://github.com/selkhateeb/heroku-vagrant-da rt-build/releases/download/latest/dart-sdk.tar | |
| 735 $ heroku config:add BUILDPACK_URL=https://github.com/igrigorik/heroku-buildpack- dart.git | |
| 736 $ | |
| 737 {% endprettify %} | |
| 738 | |
| 739 </div> | |
| 740 | |
| 741 <div class="trydart-filename">terminal</div> | |
| 742 | |
| 743 </div> <div class="col-md-5" markdown="1"> | |
| 744 | |
| 745 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong> | |
| 746 | |
| 747 * First, create a Heroku app. | |
| 748 Provide it with a unique name (NOT `myfirstdartappforheroku`). | |
| 749 | |
| 750 * Next, enable `user-env-compile` so that environment variables | |
| 751 can be passed to the build script during deploy. | |
| 752 | |
| 753 * Official builds of Dart do not support Heroku due to mismatched glibc | |
| 754 versions. In this code lab, | |
| 755 you'll use the (unsupported) [Vagrant Dart Build](https://github.com/selkhatee b/heroku-vagrant-dart-build), | |
| 756 which is a special build of the SDK that is compatible with Heroku. | |
| 757 | |
| 758 * A buildpack extends Heroku. The buildpack used here extends Heroku | |
| 759 to support Dart apps. The location of the buildpack | |
| 760 is specified with the `BUILDPACK_URL`. | |
| 761 | |
| 762 {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %} | |
| 763 | |
| 764 </div> </div> | |
| 765 | |
| 766 ### <i class="fa fa-anchor"> </i> Push your app to Heroku | |
| 767 | |
| 768 <div class="row"> <div class="col-md-7" markdown="1"> | |
| 769 | |
| 770 <div class="trydart-step-details" markdown="1"> | |
| 771 | |
| 772 | |
| 773 {% prettify bash %} | |
| 774 $ [[highlight]]git push heroku master[[/highlight]] | |
| 775 Initializing repository, done. | |
| 776 | |
| 777 ... | |
| 778 -----> Fetching custom git buildpack... done | |
| 779 -----> Dart app detected | |
| 780 ... | |
| 781 Dart VM version: 1.0.0.5_r30248_vagrant (Thu Nov 14 13:43:29 2013) on "linux_x64 " | |
| 782 ... | |
| 783 *** Running pub build | |
| 784 Building with "pub build" | |
| 785 Building deploy_codelab.................... | |
| 786 [Info in Dart2JS]: | |
| 787 Generated deploy_codelab|web/piratebadge.dart.js (97701 characters) in 0:01:19.6 85114 | |
| 788 Built 6 files! | |
| 789 ... | |
| 790 -----> Launching... done, v6 | |
| 791 http://thawing-shore-4708.herokuapp.com deployed to Heroku | |
| 792 | |
| 793 To git@heroku.com:thawing-shore-4708.git | |
| 794 * [new branch] master -> master | |
| 795 $ | |
| 796 {% endprettify %} | |
| 797 | |
| 798 | |
| 799 </div> | |
| 800 | |
| 801 <div class="trydart-filename">terminal</div> | |
| 802 | |
| 803 </div> <div class="col-md-5" markdown="1"> | |
| 804 | |
| 805 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong> | |
| 806 | |
| 807 | |
| 808 * Heroku uses git for deploying applications. | |
| 809 | |
| 810 * Your local git repository is associated with a remote Heroku repository, | |
| 811 usually named `heroku`. | |
| 812 | |
| 813 * When you push, Heroku runs the buildpack that you specified. | |
| 814 | |
| 815 * In addition to other tasks, this buildpack runs `pub build`. | |
| 816 | |
| 817 * When the push is finished, the process displays a URL for your app. | |
| 818 | |
| 819 {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %} | |
| 820 | |
| 821 </div> </div> | |
| 822 | |
| 823 ### <i class="fa fa-anchor"> </i> Specify the scale | |
| 824 | |
| 825 <div class="row"> <div class="col-md-7" markdown="1"> | |
| 826 | |
| 827 <div class="trydart-step-details" markdown="1"> | |
| 828 | |
| 829 {% prettify bash %} | |
| 830 $ heroku ps:scale web=1 | |
| 831 $ | |
| 832 {% endprettify %} | |
| 833 | |
| 834 </div> | |
| 835 | |
| 836 <div class="trydart-filename">terminal</div> | |
| 837 | |
| 838 </div> <div class="col-md-5" markdown="1"> | |
| 839 | |
| 840 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong> | |
| 841 | |
| 842 * Scale to one _dyno_, which is a Heroku lightweight container | |
| 843 that runs a single user-specified command. | |
| 844 | |
| 845 {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %} | |
| 846 | |
| 847 </div> </div> | |
| 848 | |
| 849 <hr> | |
| 850 | |
| 851 ##Step 5: Run the app online and on mobile {#step-five} | |
| 852 | |
| 853 Now that your app is deployed, you can run it on the internet | |
| 854 and on a mobile device. | |
|
Kathy Walrath
2014/01/16 00:01:29
This seems to imply that you can't run the app on
mem
2014/01/16 22:49:00
I edited to make a new section.
mem
On 2014/01/1
| |
| 855 | |
| 856 ### <i class="fa fa-anchor"> </i> Test your app | |
| 857 | |
| 858 <div class="row"> <div class="col-md-7" markdown="1"> | |
| 859 | |
| 860 <div class="trydart-step-details" markdown="1"> | |
| 861 | |
| 862 <iframe class="running-app-frame" | |
| 863 style="height:220px;width:550px;" | |
| 864 src="/codelabs/darrrt/examples/6-piratebadge_json/piratebadge.html"> | |
| 865 </iframe> | |
| 866 | |
| 867 | |
| 868 </div> | |
| 869 | |
| 870 </div> <div class="col-md-5" markdown="1"> | |
| 871 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong> | |
| 872 | |
| 873 * Use the URL displayed by the previous step to visit your app: | |
| 874 `http://myfirstdartappforheroku.herokuapp.com` | |
| 875 | |
| 876 </div> </div> | |
| 877 | |
| 878 | |
| 879 ### <i class="fa fa-anchor"> </i> Enter the URL in a browser | |
| 880 | |
| 881 <div class="row"> <div class="col-md-7" markdown="1"> | |
| 882 | |
| 883 <div class="trydart-step-details" markdown="1"> | |
| 884 | |
| 885 <img src="images/calebthebrave.png" height="400"> | |
| 886 | |
| 887 </div> | |
| 888 | |
| 889 </div> <div class="col-md-5" markdown="1"> | |
| 890 | |
| 891 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong> | |
| 892 | |
| 893 * Use the URL from the previous step to load the app | |
| 894 into a browser on your mobile device. | |
| 895 `http://myfirstdartappforheroku.herokuapp.com` | |
| 896 | |
| 897 {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %} | |
| 898 | |
| 899 </div> </div> | |
| 900 | |
| 901 | |
| 902 | |
| 903 <div class="trydart-step-details" markdown="1"> | |
| 904 </div> | |
| 905 | |
| 906 | |
| 907 ##Resources {#resources} | |
| 908 | |
| 909 For more information about the tools and libraries used | |
| 910 in this code lab, check out these resources. | |
| 911 | |
| 912 <div class="trydart-step-details" markdown="1"> | |
| 913 | |
| 914 #### SDK libraries, classes, and functions | |
| 915 | |
| 916 * The API docs for the | |
| 917 <a href="https://api.dartlang.org/dart_io/File.html" target="_blank">File</a>, | |
| 918 <a href="https://api.dartlang.org/dart_io/HttpServer.html" target="_blank">HttpS erver</a>, and | |
| 919 <a href="https://api.dartlang.org/dart_io/Platform.html" target="_blank">Platfor m</a> | |
| 920 classes from the | |
| 921 <a href="https://api.dartlang.org/dart_io.html" target="_blank">dart:io</a> | |
| 922 library. | |
| 923 * The API docs for the | |
| 924 <a href="https://api.dartlang.org/dart_async.html#runZoned" target="_blank">runZ oned()</a> | |
| 925 function from the | |
| 926 <a href="https://api.dartlang.org/dart_async.html" target="_blank">dart:async</a > library. | |
| 927 | |
| 928 #### Classes and functions from pub packages | |
| 929 | |
| 930 * The code for the | |
| 931 <a href="https://pub.dartlang.org/packages/http_server">http_server pub package< /a>, | |
| 932 in particular | |
| 933 <a href="https://api.dartlang.org/http_server/VirtualDirectory.html" target="_bl ank">VirtualDirectory</a> | |
| 934 class. | |
| 935 * The code for the | |
| 936 <a href="http://pub.dartlang.org/packages/path">path pub package</a>, | |
| 937 in particular the | |
| 938 <a href="https://api.dartlang.org/path.html#join" target="_blank">join()</a> | |
| 939 and | |
| 940 <a href="https://api.dartlang.org/path.html#dirname" target="_blank">dirname()</ a> | |
| 941 functions. | |
| 942 | |
| 943 #### Heroku tools | |
| 944 | |
| 945 * The <a href="https://devcenter.heroku.com/">Heroku Dev Center</a> | |
| 946 * The Heroku-compatible build of the Dart SDK: | |
| 947 [heroku-vagrant-dart-build](https://github.com/selkhateeb/heroku-vagrant-dart-bu ild) | |
| 948 * The Heroku buildpack for Dart: | |
| 949 [heroku-buildpack-dart](https://github.com/igrigorik/heroku-buildpack-dart) | |
| 950 | |
| 951 #### The example code | |
| 952 | |
| 953 * The [deploy-codelab-master](https://github.com/marycampione/deploy-codelab) | |
| 954 example code | |
| 955 | |
| 956 </div> | |
| 957 | |
| 958 ##What next? {#what-next} | |
| 959 | |
| 960 Now that your app is deployed for the world to use. | |
|
Kathy Walrath
2014/01/16 00:01:29
sentence fragment
mem
2014/01/16 22:49:00
Done.
| |
| 961 What do you do now? | |
| 962 Here are some suggestions. | |
| 963 | |
| 964 ### <i class="fa fa-anchor"> </i> Share your app with the world! | |
| 965 | |
| 966 <div class="trydart-step-details" markdown="1"> | |
| 967 | |
| 968 Share your Heroku URL with your followers and circles. | |
| 969 | |
| 970 * <a href="https://twitter.com/share" class="twitter-share-button" data-text="Ar rr! I've deployed me app. http://dartlang.org/codelab/deploy/" data-count="none" data-hashtags="dartlang">Tweet</a> | |
| 971 <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.tes t(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js. id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore( js,fjs);}}(document, 'script', 'twitter-wjs');</script> | |
| 972 | |
| 973 {% comment %} | |
| 974 * <p class="share-button twitter"> | |
| 975 <a href="https://twitter.com/share" | |
| 976 class="twitter-share-button external-link" | |
| 977 data-count="none" | |
| 978 data-text="Arrr! I've deployed me app. http://dartlang.org/codelab/deploy/" | |
| 979 data-hashtags="dartlang">Tweet</a> </p> | |
| 980 {% endcomment %} | |
| 981 | |
| 982 * <p> | |
| 983 <script src="https://apis.google.com/js/plusone.js"></script> | |
| 984 <g:plus action="share"></g:plus> </p> | |
| 985 | |
| 986 </div> | |
| 987 | |
| 988 ### <i class="fa fa-anchor"> </i> Jump to another ship. | |
|
Kathy Walrath
2014/01/16 00:01:29
funny!
mem
2014/01/16 22:49:00
thx :-)
| |
| 989 | |
| 990 <div class="trydart-step-details" markdown="1"> | |
| 991 | |
| 992 * Write the pirate web app in [Code Lab: Ready the Pirates](/codelabs/darrrt/). | |
|
Kathy Walrath
2014/01/16 00:01:29
This seems weird, since it was kind of a prerequis
mem
2014/01/16 22:49:00
Done.
| |
| 993 * Visit [Dart by Example](/dart-by-example/) for a rich set of targeted Dart exa mples. | |
|
Kathy Walrath
2014/01/16 00:01:29
smacks of buzzwords: rich! targeted!
Be more spec
mem
2014/01/16 22:49:00
Done.
| |
| 994 * Play with some [samples](/samples/). | |
| 995 * Try the [tutorials](/docs/tutorials/). | |
| 996 * Go write some awesome code. | |
| 997 | |
| 998 </div> | |
| 999 | |
| 1000 {% comment %} | |
| 1001 ### <i class="fa fa-anchor"> </i> Run the app on a mobile device | |
| 1002 | |
| 1003 <div class="row"> <div class="col-md-7"> | |
| 1004 | |
| 1005 <div class="trydart-step-details" markdown="1"> | |
| 1006 Open `build/piratebadge.html` using **File > Open File...**. | |
| 1007 | |
| 1008 <img src="images/mobile_better.png" height="400"> | |
| 1009 </div> | |
| 1010 | |
| 1011 </div> <div class="col-md-5" markdown="1"> | |
| 1012 | |
| 1013 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong> | |
| 1014 | |
| 1015 * The <head> section of the HTML file contains this line, | |
| 1016 `&;lt;<meta name="viewport" content="width=device-width, initial-scale=1.0, us er-scalable=no">>', | |
| 1017 which controls the size and scalable of the app helping it to fill the screen | |
| 1018 of different sized mobile devices. | |
| 1019 | |
| 1020 * The CSS code contains a small adjustment to the pirate badge's padding | |
| 1021 to align it to the left when it floats under the widgets. | |
| 1022 | |
| 1023 * Use the URL from the previous step to load the app | |
| 1024 into a browser on your mobile device. | |
| 1025 `http://myfirstdartappforheroku.herokuapp.com` | |
| 1026 | |
| 1027 * The `dart.js` script included in `piratebadge.html` determines that the browse r | |
| 1028 does not support Dart and runs the JavaScript file instead. | |
| 1029 | |
| 1030 * You now know that the `pub build` process is working and that your app | |
| 1031 runs successfully as JavaScript. | |
| 1032 | |
| 1033 </div></div> | |
| 1034 | |
| 1035 | |
| 1036 {% endcomment %} | |
| OLD | NEW |