Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(128)

Side by Side Diff: src/site/codelabs/deploy/index.markdown

Issue 138823002: Draft of new codelab for review (Closed) Base URL: https://github.com/dart-lang/dartlang.org.git@master
Patch Set: integrating more feedback from Kathy and myself. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/site/codelabs/deploy/images/wee-arrow.png ('k') | src/site/css/dart-style.css » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 ---
2 layout: tutorial
3 title: "Weigh Anchor: Deploy a Server and App"
4 description: "Deploy a Dart server and serve a Dart app."
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 <div style="font-size:164px;font-weight:bold;color:#DCDCDC">
22 DRAFT
23 </div>
24
25 Now, we switch from client-side programming to server-side.
sethladd 2014/01/24 23:03:03 This sentence is very context-heavy. That is, what
mem 2014/01/24 23:55:05 Done.
26
27 <div class="trydart-note" markdown="1">
28 <strong>Note:</strong>
29 This code lab is for those who want to learn
30 how to write a DIY server for deploying a web app.
31 If you are not interested in server-side programming,
32 this code lab is probably not for you.
33 Perhaps you'd prefer the
34 [Avast, Ye Pirates](/codelabs/darrrt/) code lab
35 for building a web app.
36 </div>
37
38 To put your app on the internet, you need to deploy it.
39 For that, you need an app, a server, and a host.
sethladd 2014/01/24 23:03:03 server is overloaded... I need a physical (or virt
mem 2014/01/24 23:55:05 Done.
40
41 <div class="trydart-step-details" markdown="1">
42
43 ![The process for deploying a Web app](images/process.png)
44
45 </div>
46
47 You can use any server to serve your Dart app,
sethladd 2014/01/24 23:03:03 global: clarify server to web server
mem 2014/01/24 23:55:05 Done.
48 but one of the interesting things about Dart is that it
49 runs on both the client and the server, so you
sethladd 2014/01/24 23:03:03 this seems like a long sentence
mem 2014/01/24 23:55:05 Done.
50 can use Dart for both ends of your project.
51 One benefit of doing so is that the client and server
52 can share code.
53 Dart also runs on Windows, Linux, or Mac systems.
sethladd 2014/01/24 23:03:03 not sure we need the word 'also' here. Should we
mem 2014/01/24 23:55:05 Done.
54
55 * **Server**&mdash;In this code lab, you learn how to build a server in Dart
sethladd 2014/01/24 23:03:03 clarify to web server
mem 2014/01/24 23:55:05 Done.
56 that can serve static files (those that have no dynamic content).
57 You use this server to respond to HTTP requests for the app's pages.
58
59 * **Host**&mdash;In addition to a server, you need a host for your app.
60 Many public hosting services are available.
61 We chose the Heroku hosting service for this project
62 because it has a free tier and is easy to use.
63
64 * **App**&mdash;As for the app, you will be deploying the Pirate Badge app from the
65 [Avast, Ye Pirates](/codelabs/darrrt/) code lab.
66
67 * **Fun debugging on mobile devices**&mdash;In addition,
68 you learn how to use DevTools to inspect and debug your app on a mobile device.
69
70 <hr>
71
72 <div markdown="1">
73
74 ## Table of Contents
75
76 * [Step 0: Set up](#step-zero)
77 * [Step 1: Run the app in Dartium](#step-one)
sethladd 2014/01/24 23:03:03 global: qualify app as client app?
mem 2014/01/24 23:55:05 Done.
78 * [Step 2: Build the app and run as JavaScript](#step-two)
79 * [Step 3: Walk through the static server's code](#step-three)
80 * [Step 4: Inspect your app on a mobile device](#step-four)
81 * [Step 5: Deploy to Heroku](#step-five)
82 * [Step 6: Run the app on the web and on a mobile device](#step-six)
83 * [Resources](#resources)
84 * [What next?](#what-next)
85
86 </div>
87
88 <hr>
89
90 ##Step 0: Set up {#step-zero}
91
92 In this step, you download Dart and get the code for the app that you will be de ploying.
93 This code lab uses Dart Editor.
94
95 ### <i class="fa fa-anchor"> </i> Get Dart.
96
97 <div class="trydart-step-details" markdown="1">
98 If you haven't already done so,
99 get the Dart download.
100 Unzip the ZIP file, which creates a directory called `dart`.
101
102 <!--style here is a hack to remove the arrow, which was only partially showing. -->
103 <div style="padding-left: 10px">
104 {% include downloads/_dart-editor.html buttonclass="btn btn-primary btn-lg" %}
105 </div>
106
107 <p class="os-choices" markdown="1">
108 The Dart tools
109 work in recent versions of
110 {% include os-choices.html %}
111 </p>
112 </div>
113
114 ### <i class="fa fa-anchor"> </i> Start Dart Editor.
115
116 <div class="trydart-step-details" markdown="1">
117 Go to the `dart` directory and double-click **DartEditor**.
118
119 **Got questions? Having trouble?** Go to the
120 [Troubleshooting Dart Editor](/tools/editor/troubleshoot.html) page.
121
122 </div>
123
124 ### <i class="fa fa-anchor"> </i> Get the code.
125
126 <div class="trydart-step-details" markdown="1">
127 <a href="https://github.com/marycampione/deploy-codelab/archive/master.zip">Down load</a>
128 the code for the app that you will be deploying.
129 Unzip the ZIP file,
130 which creates a directory called `deploy-codelab`.
131 </div>
132
133 ### <i class="fa fa-anchor"> </i> Open deploy-codelab.
134
135 <div class="trydart-step-details" markdown="1">
136 In Dart Editor,
137 use **File > Open Existing Folder...**
138 to open the `deploy-codelab` directory.
139 </div>
140
141 <div class="row"> <div class="col-md-7" markdown="1">
142
143 ![The files and directories in the deploy-codelab directory](images/filesanddirs .png)
144
145 <div class="trydart-note" markdown="1">
146 <strong>Note:</strong>
147 If you see <span style="color:red">red X's</span> at the left of the
148 filenames,
149 the packages are not properly installed.
150 Right click `pubspec.yaml` and select **Pub Get**.
151 </div>
152
153 </div> <div class="col-md-5" markdown="1">
154
155 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong>
156
157 * The `packages` directory,
158 as well as the `pubspec.yaml` and `pubspec.lock` files,
159 are related to package dependencies.
sethladd 2014/01/24 23:03:03 what are package dependencies? suggestion: The pa
mem 2014/01/24 23:55:05 Done.
160 This project has all the dependencies set up for you.
161 Dart Editor automatically installs the necessary packages.
162
163 * The `web` directory contains the Pirate Badge app to deploy.
164
165 * The `bin` directory contains the code for a static file server.
sethladd 2014/01/24 23:03:03 The bin directory contains Dart scripts that run f
mem 2014/01/24 23:55:05 Done.
166 You can use the server for testing and debugging your app
sethladd 2014/01/24 23:03:03 The first-order function of the server is to serve
mem 2014/01/24 23:55:05 Done.
167 on your local machine and on mobile devices.
168 Also you can use it to deploy to Heroku,
169 which requires that you provide a server with your project.
170
171
172 &nbsp; {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %}
173
174 </div> </div>
175
176 <hr>
177
178 ##Step 1: Run the app in Dartium {#step-one}
179
180 Open the code for the Pirate Badge web app and run it locally.
181
182 ### <i class="fa fa-anchor"> </i> Expand the `web` directory.
183
184 <div class="trydart-step-details" markdown="1">
185 In Dart Editor, expand the `web` directory
186 by clicking the little arrow
187 ![wee arrow](images/wee-arrow.png) to the left of its name.
188 The directory contains four files:
189
190 ![Files for the app to deploy](images/appfiles.png)
191
192 </div>
193
194 ### <i class="fa fa-anchor"> </i> Look at `piratebadge.html`.
195
196 <div class="row"> <div class="col-md-7">
197
198 <div class="trydart-step-details" markdown="1">
199
200 Double-click `piratebadge.html` to open it.
201
202 {% prettify html %}
203 ...
204 <head>
205 <meta charset="utf-8">
206 <title>Pirate badge</title>
207 [[highlight]]<meta name="viewport"[[/highlight]]
208 [[highlight]]content="width=device-width, initial-scale=1.0">[[/highligh t]]
209 <link rel="stylesheet" href="piratebadge.css">
210 </head>
211 <body>
212 ...
sethladd 2014/01/24 23:03:03 I really wanted to see a reference to the script t
mem 2014/01/24 23:55:05 Done.
213 [[highlight]]<script src="packages/browser/dart.js"></script>[[/highlight]]
214 ...
215 </body>
216 ...
217 {% endprettify %}
218
219 </div>
220
221 <div class="trydart-filename">piratedbadge.dart</div>
222
223 </div> <div class="col-md-5" markdown="1">
224
225 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong>
226
227 * The <code>&lt;meta&gt;</code> tag sets attributes to
228 make the app scale correctly on mobile devices.
229
230 * `dart.js` is a script that determines if the browser supports Dart,
231 and if it does, runs the app natively.
232 If it does not, the script loads the JavaScript version of the app.
233
234 * `dart.js` is part of the `browser` pub package.
235
236 </div></div>
237
238 ### <i class="fa fa-anchor"> </i> Run the app.
239
240
241 <div class="row"> <div class="col-md-7">
242
243 <div class="trydart-step-details" markdown="1">
244
245 Right-click `piratebadge.html`
246 and choose **Run in Dartium** from the menu.
247
248 ![Run the app using the menu](images/runtheapp.png)
249
250 </div>
251
252 </div> <div class="col-md-5" markdown="1">
253
254 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong>
255
256 * Dart Editor launches *Dartium*&mdash;a special build of Chromium
257 that has the Dart Virtual Machine built in&mdash;and loads the app.
258
259 * At this time, Dartium is the only browser that has the Dart VM built in.
260 To run a Dart app in other browsers you need to convert it to JavaScript,
261 which you do in the next step.
262
263 </div></div>
264
265 <div class="row"> <div class="col-md-7">
266
267 <div class="trydart-step-details" markdown="1">
268
269 </div>
270
271 <iframe class="running-app-frame"
272 style="height:220px;width:550px;margin-top:20px;margin-bottom:20px"
273 src="/codelabs/darrrt/examples/6-piratebadge/piratebadge.html">
274 </iframe>
275
276 </div> <div class="col-md-5" markdown="1">
277
278 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong>
279
280 * The Pirage Badge app should appear.
281
282 * Try out the app running to the left.
283 Click the button or type in the text field to generate a pirate name.
284
285 &nbsp; {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %}
286
287 </div></div>
288
289 <hr>
290
291 ##Step 2: Build the app and run as JavaScript {#step-two}
292
293 In this step, you use `pub build` to
294 generate the assets for the app
295 and put them into a new directory named `build`.
296 In addition to other tasks,
297 this process generates minified JavaScript that
298 works in any modern browser.
299
300 ### <i class="fa fa-anchor"> </i> Check out pubspec.yaml
301
302 <div class="row"> <div class="col-md-7">
303
304 <div class="trydart-step-details" markdown="1">
305
306 Double-click the `pubspec.yaml` file to open it.
307 Click the **Source** tab at the bottom of the editing pane.
308
309 {% prettify dart %}
sethladd 2014/01/24 23:03:03 Why is this important to building the app?
mem 2014/01/24 23:55:05 Done.
310 name: deploy_codelab
311 description: A sample deployment
312 dependencies:
313 [[highlight]]browser: any[[/highlight]]
314 [[highlight]]http_server: any[[/highlight]]
315 [[highlight]]path: any[[/highlight]]
316 {% endprettify %}
317
318 </div>
319
320 <div class="trydart-filename">pubspec.yaml</div>
321
322 </div> <div class="col-md-5" markdown="1">
323
324 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong>
325
326 * A `pubspec.yaml` file in a directory identifies the directory
327 and its contents as a package.
328
329 * `pubspec.yaml` provides meta-data for the application,
330 such as its name.
331
332 * The `pubspec.yaml` file also lists the packages on which the app depends.
333 The three packages needed by this app are all hosted on
334 [pub.dartlang.org](https://pub.dartlang.org/).
335
336 * `any` selects the latest version of the package that matches your SDK.
337
338 </div></div>
339
340 ### <i class="fa fa-anchor"> </i> Look at the packages directory
341
342 <div class="row"> <div class="col-md-7">
343
344 <div class="trydart-step-details" markdown="1">
345
346 In Dart Editor, expand the top-level `packages` directory.
sethladd 2014/01/24 23:03:03 why is this important to building the app? that is
mem 2014/01/24 23:55:05 Done.
347
348 ![Packages contains the code for the package dependencies](images/packagesfiles. png)
349
350 </div>
351
352 </div> <div class="col-md-5" markdown="1">
353
354 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong>
355
356 * The `packages` directory contains the code for all of the dependencies
357 listed in the `pubspec.yaml` file.
358 These are installed automatically by Dart Editor.
359
360 * The `browser` package contains the `dart.js` script
361 that checks for native Dart support.
362
363 * The `http_server` package implements high-level HTTP server
364 functionality, making it easier to write HTTP server code.
365
366 * The `path` package provides common path manipulation operations,
367 such as joining, splitting, and normalizing.
368
369 * The `mime` package is included because `http_server` depends on it.
370
371 * You're likely to see multiple `packages` directories. One is installed
372 for every subdirectory that needs it.
373
374 </div></div>
375
376 ### <i class="fa fa-anchor"> </i> Run pub build
377
378 <div class="row"> <div class="col-md-7">
379
380 <div class="trydart-step-details" markdown="1">
381
382 With `pubspec.yaml` still selected,
383 select **Tools > Pub Build**.
384 The output looks something like this:
385
386 {% prettify bash %}
387 --- Jan 16, 2014 4:14:36 PM Running pub build ... ---
388 Building deploy_codelab.....
389 [Info from Dart2JS]:
390 Took 0:00:06.811770 to compile deploy_codelab|web/piratebadge.dart.
391 Built 10 files!
392 {% endprettify %}
393
394 </div>
395
396 <div class="trydart-filename">terminal</div>
397
398 </div> <div class="col-md-5" markdown="1">
399
400 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong>
401
402 * The `pub build` command creates a `build` directory that contains
403 everything your app needs to be deployed, including a minified
404 JavaScript file and the required packages.
405
406 </div></div>
407
408 ### <i class="fa fa-anchor"> </i> Look at the `build` directory
409
410 <div class="row"> <div class="col-md-7">
411
412 <div class="trydart-step-details" markdown="1">
413
414 The `pub build` command creates a `build` directory
415 under `deploy-codelab`.
416
417 ![The build directory contains everything you need to deploy.](images/builddir.p ng)
418 </div>
419
420 </div> <div class="col-md-5" markdown="1">
421
422 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong>
423
424 * The `piratebadge.dart.js` file is a JavaScript file that has been minified.
425 When deployed, this file runs in the browser.
426
427 * The `packages` directory contains the package dependencies.
428
429 * Note that the `build` directory contains no `piratebadge.dart` file.
430 It is not needed to deploy the app to JavaScript.
431
432 </div></div>
433
434
435 ### <i class="fa fa-anchor"> </i> Run the app as JavaScript
436
437 <div class="row"> <div class="col-md-7">
438
439 <div class="trydart-step-details" markdown="1">
440
441 Open the app.
442 Select **File > Open File...**
443 in a browser such a Firefix or Safari
444 and select the
445 `deploy-codelab/build/piratebadge.html` file.
446
447 </div>
448
449 </div> <div class="col-md-5" markdown="1">
450
451 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong>
452
453 * The app is running on the local machine using the `file` protocol.
454 To share your app with others, you need to deploy the app to a hosting service ,
455 such as Heroku.
456
457 </div></div>
458
459 <hr>
460
461 ##Step 3: Walk through the static server's code {#step-three}
462
463 A static file server is a stand-alone application that listens
sethladd 2014/01/24 23:03:03 can you clarify that you can write dynamic HTTP se
mem 2014/01/24 23:55:05 Done.
464 for and responds to HTTP requests with the contents of pages that have no
465 dynamic content.
466 Typically, static file servers get requests for `index.html` and other
467 HTML files plus the assets, such as images, required by that file.
468
469 You need to provide a static server for this project
470 because Heroku requires one for deployment.
sethladd 2014/01/24 23:03:03 add: "Also, it's fun to serve your Dart client app
mem 2014/01/24 23:55:05 Done.
471 The code you downloaded for this project contains a server.
472 This step walks through the code for the server,
473 which uses some interesting Dart APIs and
474 two helpful pub packages.
475
476 Links to the relevant API docs are provided in [Resources](#resources).
477
478 ### <i class="fa fa-anchor"> </i> Setting the path to the app.
479
480 <div class="row"> <div class="col-md-7" markdown="1">
481
482 <div class="trydart-step-details" markdown="1">
483
484 Double click `bin/basic_http_server.dart` to open it. The beginning of
485 the code is as follows:
486
487 {% prettify dart %}
488 import 'dart:io' show File, HttpServer, [[highlight]]Platform[[/highlight]];
489 import 'dart:async' show runZoned;
490 import 'package:http_server/http_server.dart' show VirtualDirectory;
491 import 'package:path/path.dart' show [[highlight]]join, dirname;[[/highlight]]
492
493 void main() {
494 [[highlight]]// Assumes the server lives in bin/ and that `pub build` ran.[[/h ighlight]]
495 [[highlight]]var pathToBuild = join(dirname(Platform.script.toFilePath()),[[/h ighlight]]
496 [[highlight]]'..', 'build');[[/highlight]]
497
498 ...
499 }
500 {% endprettify %}
501 </div>
502
503 <div class="trydart-filename">basic_http_server.dart</div>
504
505 </div> <div class="col-md-5" markdown="1">
506
507 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong>
508
509 * This code assumes that `pub build` has run, because
510 it relies on the `build` directory existing.
511
512 * In the next step, you will configure a Heroku buildpack
513 (a collection of scripts)
514 that runs `pub build` when you upload your app.
515
516 * This code uses `join` along with `dirname` to
517 create a path to the `build` directory
518 based on the path of the running script.
519
520 * The top-level `Platform` object provides information about the
521 environment in which the program is running.
522 Here, the code gets the filepath to the program.
523
524 &nbsp; {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %}
525
526 </div> </div>
527
528 ### <i class="fa fa-anchor"> </i> Serving `piratebadge.html`.
529
530 <div class="row"> <div class="col-md-7" markdown="1">
531
532 <div class="trydart-step-details" markdown="1">
533
534 {% prettify dart %}
535 import 'dart:io' show [[highlight]]File[[/highlight]], HttpServer, Platform;
536 import 'dart:async' show runZoned;
537 import 'package:http_server/http_server.dart' show [[highlight]]VirtualDirectory ;[[/highlight]]
538 import 'package:path/path.dart' show join, dirname;
539
540 void main() {
541 ...
542 [[highlight]]var staticFiles = new VirtualDirectory(pathToBuild);[[/highlight] ]
543 [[highlight]]staticFiles.allowDirectoryListing = true;[[/highlight]]
544 [[highlight]]staticFiles.directoryHandler = (dir, request) {[[/highlight]]
545 [[highlight]]// Redirect directory-requests to piratebadge.html file.[[/high light]]
546 [[highlight]]var indexUri = new Uri.file(dir.path).resolve('piratebadge.html ');[[/highlight]]
547 [[highlight]]staticFiles.serveFile(new File(indexUri.toFilePath()), request) ;[[/highlight]]
548 [[highlight]]};[[/highlight]]
549 ...
550 }
551 {% endprettify %}
552 </div>
553
554 <div class="trydart-filename">basic_http_server.dart</div>
555
556 </div> <div class="col-md-5" markdown="1">
557
558 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong>
559
560 * The `VirtualDirectory` class from the `http_server` package
561 provides a high-level interface for serving static files and directory listing s to HttpRequests.
562
563 * This code redirects directory requests to `piratebadge.html`,
564 which is the main HTML file for this app.
565 This approach works because the deployed app has only one directory.
566
567 * The `serveFile` method serves the requested static file
568 to the given HttpRequest object.
569
570 * Without the `http_server` package, this code would be longer and more complex,
571 because you would have to use the lower-level `HttpServer`, `HttpRequest`,
572 and `HttpResponse` APIs.
573
574 &nbsp; {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %}
575
576 </div> </div>
577
578 ### <i class="fa fa-anchor"> </i> Using variable port numbers.
579
580 <div class="row"> <div class="col-md-7" markdown="1">
581
582 <div class="trydart-step-details" markdown="1">
583
584 {% prettify %}
585 import 'dart:io' show File, HttpServer, [[highlight]]Platform[[/highlight]];
586 import 'dart:async' show runZoned;
587 import 'package:http_server/http_server.dart' show VirtualDirectory;
588 import 'package:path/path.dart' show join, dirname;
589
590 void main() {
591 ...
592 [[highlight]]var portEnv = Platform.environment['PORT'];[[/highlight]]
593 [[highlight]]var port = portEnv == null ? 9999 : int.parse(portEnv);[[/highlig ht]]
594 ...
595 }
596 {% endprettify %}
597 </div>
598
599 <div class="trydart-filename">basic_http_server.dart</div>
600
601 </div> <div class="col-md-5" markdown="1">
602
603 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong>
604
605 * Heroku uses variable ports. Heroku sets the `$PORT` environment variable
606 with the port number that your app should listen to.
607
608 * This code uses the port specified in the `$PORT` environment variable,
609 if present. Otherwise, it uses port 9999.
610
611 * The `$PORT` environment variable might not be present if the server is running
612 in a non-Heroku environment.
613
614 &nbsp; {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %}
615
616 </div> </div>
617
618
619 ### <i class="fa fa-anchor"> </i> Using `runZoned()`.
620
621 <div class="row"> <div class="col-md-7" markdown="1">
622
623 <div class="trydart-step-details" markdown="1">
624
625
626 {% prettify dart %}
627 import 'dart:io' show File, HttpServer, Platform;
628 import 'dart:async' show [[highlight]]runZoned[[/highlight]];
629 import 'package:http_server/http_server.dart' show VirtualDirectory;
630 import 'package:path/path.dart' show join, dirname;
631
632 void main() {
633 ...
634 [[highlight]]runZoned(() {[[/highlight]]
635 ...
636 [[highlight]]},[[/highlight]]
637 [[highlight]]onError: (e, stackTrace) => print('Oh noes! $e $stackTrace'));[[/ highlight]]
638 }
639 {% endprettify %}
640 </div>
641
642 <div class="trydart-filename">basic_http_server.dart</div>
643
644 </div> <div class="col-md-5" markdown="1">
645
646 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong>
647
648 * `runZoned` helps provide stability to your program.
649 It catches exceptions from all call stacks within its bounds,
650 allowing your program to continue running in spite of errors.
651
652 &nbsp; {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %}
653
654 </div> </div>
655
656 ### <i class="fa fa-anchor"> </i> Listening for HTTP requests.
657
658 <div class="row"> <div class="col-md-7" markdown="1">
659
660 <div class="trydart-step-details" markdown="1">
661
662
663 {% prettify dart %}
664 import 'dart:io' show File, [[highlight]]HttpServer[[/highlight]], Platform;
665 import 'dart:async' show runZoned;
666 import 'package:http_server/http_server.dart' show VirtualDirectory;
667 import 'package:path/path.dart' show join, dirname;
668
669 void main() {
670 ...
671 runZoned(() {
672 [[highlight]]HttpServer.bind('0.0.0.0', port).then((server) {[[/highlight]]
673 [[highlight]]server.listen(staticFiles.serveRequest);[[/highlight]]
674 [[highlight]]});[[/highlight]]
675 },
676 onError: (e, stackTrace) => print('Oh noes! $e $stackTrace'));
677 }
678 {% endprettify %}
679 </div>
680
681 <div class="trydart-filename">basic_http_server.dart</div>
682
683 </div> <div class="col-md-5" markdown="1">
684
685 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong>
686
687 * `HttpServer.bind()` creates an HTTP server, which responds to HTTP requests
688 for this app.
689
690 * The server binds to a host `0.0.0.0`.
691 which is a special address that is guaranteed to receive from any network sock et.
sethladd 2014/01/24 23:03:03 long line. also, receive what?
mem 2014/01/24 23:55:05 Done.
692 If you don't know what specific IP to listen to, you can use 0.0.0.0.
693
694 * The server listens for requests and uses the `VirtualDirectory` object
695 named `staticFiles` to handle them.
696
697 &nbsp; {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %}
sethladd 2014/01/24 23:03:03 long line
mem 2014/01/24 23:55:05 Done.
698
699 </div> </div>
700
701
702 ### <i class="fa fa-anchor"> </i> Run the server, run the app
703
704 <div class="row"> <div class="col-md-7" markdown="1">
705
706 <div class="trydart-step-details" markdown="1">
707
708 In Dart Editor,
709 right click `bin/basic_http_server.dart` and select `Run`.
710 Dart Editor brings up a new output pane in which the server
711 displays its output, if any.
712
713 Now, type `0.0.0.0:9999` in the URL text field of any browser.
sethladd 2014/01/24 23:03:03 how about: "Now, open `http://0.0.0.0:9999` in yo
mem 2014/01/24 23:55:05 Done.
714 The app appears and you can generate pirate names.
715
716 <strong>Leave the server running.</strong>
717
718 </div>
719
720
721 </div> <div class="col-md-5" markdown="1">
722
723 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong>
724
725 * 0.0.0.0:9999 requests the root directory of that host and port.
726 The static file server resolves that request to `piratebadge.html`
727 and returns the contents of that file.
728
729 * 0.0.0.0:9999 requests work only on your local machine.
730 Another machine cannot run your app using that URL.
731 In [Step 5](#step-five), you deploy your app to Heroku,
732 which gives you a public URL.
733
734 &nbsp; {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %}
735
736 </div> </div>
737
738 <hr>
739
740 ##Step 4: Inspect your app on a mobile device {#step-four}
741
742 Before deploying your app,
743 you can use the Chrome DevTools to check, debug, and modify
744 your app on a mobile device.
745 DevTools can debug Android devices natively
746 and other devices using
747 [Mobile Emulation](https://developers.google.com/chrome-developer-tools/docs/mob ile-emulation).
748 This section shows you how to use DevTools with your app on an Android device.
749
750 You don't need to do this step to deploy your app.
751 You can skip and move on to [Step 5](#step-five).
752 However, this does help you to test and debug your app on mobile devices
753 and it's pretty cool.
754
755 ### <i class="fa fa-anchor"> </i> Set up your computer and device
756
757 <div class="trydart-step-details" markdown="1">
758
759 Follow the instructions at
760 [Remote Debugging Chrome on Android](https://developers.google.com/chrome-develo per-tools/docs/remote-debugging)
761 to set up your device, Chrome, and your computer.
762
763 </div>
764
765 ### <i class="fa fa-anchor"> </i> Run the app on your mobile device
766
767 <div class="trydart-step-details" markdown="1">
768
769 Make sure your server is still running from [Step 3](#step-three).
770
771 On your mobile device, bring up Chrome,
772 and type the `0.0.0.0:9999` URL into the URL text field.
sethladd 2014/01/24 23:03:03 "open http://0.0.0.0:9999 in your browser.
mem 2014/01/24 23:55:05 Done.
773
774 <img src="images/calebthebrave.png" height="400">
775
776 </div>
777
778 ### <i class="fa fa-anchor"> </i> Bring up Devices page
779
780 <div class="trydart-step-details" markdown="1">
781
782 In Chrome on your computer,
783 click the Android icon at the right side of any window
784 and select **View Inspection Targets**.
785 If the Android icon is not there,
786 then the mobile device or your computer is not properly connected or setup.
787 Refer back to the
788 [Remote Debugging Chrome on Android](https://developers.google.com/chrome-develo per-tools/docs/remote-debugging)
789 instructions.
790
791 ![Click the Android icon](images/androidicon.png)
792
793 The Devices page appears and an entry for your app appears.
794
795 ![Your tab appears under the Chrome heading](images/tabappears.png)
796
797 </div>
798
799 ### <i class="fa fa-anchor"> </i> Bring up DevTools for the app
800
801 <div class="trydart-step-details" markdown="1">
802
803 On the Devices page, click `inspect` under the entry for your app.
804 A DevTools window appears that is attached to your app running
805 on the mobile device.
sethladd 2014/01/24 23:03:03 can you double-clarify what's going on? "You are
mem 2014/01/24 23:55:05 Done.
806
807 ![Your tab appears under the Chrome heading](images/devtoolsforphone.png)
808
809 </div>
810
811 ### <i class="fa fa-anchor"> </i> Experiment with changing the app
812
813 <div class="trydart-step-details" markdown="1">
814
815 Experiment by modifying the font size or color.
sethladd 2014/01/24 23:03:03 clarify that you mean for them to use dev tools to
mem 2014/01/24 23:55:05 Done.
816 The change happens instantly on your mobile device.
817
818 </div>
819
820 ### <i class="fa fa-anchor"> </i> Save your changes
sethladd 2014/01/24 23:03:03 maybe drop this section? it sort of implies you ca
mem 2014/01/24 23:55:05 Done.
821
822 <div class="trydart-step-details" markdown="1">
823
824 If you want, make any permanent changes to your source code and build the app ag ain.
825
826 </div>
827
828
829 ##Step 5: Deploy to Heroku {#step-five}
830
831 In this step, you deploy your server and app to Heroku.
sethladd 2014/01/24 23:03:03 maybe talk about why heroku? heroku is a cloud hos
mem 2014/01/24 23:55:05 Done.
832
833 ### <i class="fa fa-anchor"> </i> Get Heroku and a Heroku account
834
835 <div class="row"> <div class="col-md-7" markdown="1">
836
837 <div class="trydart-step-details" markdown="1">
838
839 Follow Steps 1, 2, and 3 at
840 [Getting Started with Heroko](https://devcenter.heroku.com/articles/quickstart)
sethladd 2014/01/24 23:03:03 typo: heroku
mem 2014/01/24 23:55:05 Done.
841 to download the Heroku tools and get an account with Heroku.
842
843 </div>
844
845 </div> <div class="col-md-5" markdown="1">
846
847 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong>
848
849 * Now you can deploy your app to Heroku.
sethladd 2014/01/24 23:03:03 This seems like a status update, not a "what this
mem 2014/01/24 23:55:05 Done.
850
851 &nbsp; {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %}
852
853 </div> </div>
854
855
856 ### <i class="fa fa-anchor"> </i> Create a `Procfile`
857
858 <div class="row"> <div class="col-md-7" markdown="1">
859
860 <div class="trydart-step-details" markdown="1">
861
862 In the `deploy-codelab` directory, create a file
863 named `Procfile` and add the following line to it.
864
865 {% prettify bash %}
866
867 web: ./dart-sdk/bin/dart bin/basic_http_server.dart
868
869 {% endprettify %}
870
871 </div>
872
873 <div class="trydart-filename">Procfile</div>
874
875 </div> <div class="col-md-5" markdown="1">
876
877 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong>
878
879 * A `Procfile` tells Heroku what part of your app can be executed.
880
881 * The `Procfile` for this example defines a web process type
882 in which the dart VM runs the static file server.
883
884 &nbsp; {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %}
885
886 </div> </div>
887
888
889 ### <i class="fa fa-anchor"> </i> Commit your changes to your local `deploy-code lab` git repo
sethladd 2014/01/24 23:03:03 couple things to define: what is git? what is a g
mem 2014/01/24 23:55:05 Done.
890
891 <div class="trydart-step-details" markdown="1">
892
893 `git` is the means for deploying apps to Heroku.
894 Before you can push an app to Heroku,
895 you need to initialize a local `git` repo and commit your files to it.
896 </div>
897
898 <div class="row"> <div class="col-md-7" markdown="1">
899
900 <div class="trydart-step-details" markdown="1">
901
902 {% prettify bash %}
903 $ cd deploy-codelab
904 $ git init
905 $ git add -A .
906 $ git commit -am "Hoist the mizzen!"
907 $
sethladd 2014/01/24 23:03:03 blank prompt?
mem 2014/01/24 23:55:05 Done.
908 {% endprettify %}
909
910 </div>
911
912 <div class="trydart-filename">terminal</div>
913
914 </div> <div class="col-md-5" markdown="1">
915
916 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong>
917
918 * First, create a local repo with `git init`.
919
920 * Next, add all of the files in `deploy-codelab` to it.
921
922 * Finally, commit the new files in the `build` directory to your local
923 git repo.
924
925 &nbsp; {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %}
926
927 </div> </div>
928
929 ### <i class="fa fa-anchor"> </i> Create a Heroku app and configure it.
930
931 <div class="row"> <div class="col-md-7" markdown="1">
932
933 <div class="trydart-step-details" markdown="1">
934
935 <strong>Important:</strong> Replace
936 `myfirstdartappforheroku` with something **unique**.
937
938 {% prettify bash %}
939 $ heroku create [[highlight]]myfirstdartappforheroku[[/highlight]] -s cedar
940 $ heroku labs:enable user-env-compile
941 $ heroku config:set DART_SDK_URL=https://github.com/selkhateeb/heroku-vagrant-da rt-build/releases/download/latest/dart-sdk.tar
942 $ heroku config:add BUILDPACK_URL=https://github.com/igrigorik/heroku-buildpack- dart.git
943 $
944 {% endprettify %}
945
946 </div>
947
948 <div class="trydart-filename">terminal</div>
949
950 </div> <div class="col-md-5" markdown="1">
951
952 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong>
953
954 * First, create a Heroku app.
955 Provide it with a **unique** name (not `myfirstdartappforheroku`).
956
957 * Next, enable `user-env-compile` so that environment variables
958 can be passed to the build script during deploy.
959
960 * Official builds of Dart do not support Heroku due to mismatched glibc
961 versions. In this code lab,
962 you'll use the (unsupported) [Vagrant Dart Build](https://github.com/selkhatee b/heroku-vagrant-dart-build),
963 which is a special build of the Dart SDK that is compatible with Heroku.
964
965 * A buildpack extends Heroku. The buildpack used here extends Heroku
966 to support Dart apps. The location of the buildpack
967 is specified with the `BUILDPACK_URL`.
968
969 &nbsp; {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %}
970
971 </div> </div>
972
973 ### <i class="fa fa-anchor"> </i> Push your app to Heroku
974
975 <div class="row"> <div class="col-md-7" markdown="1">
976
977 <div class="trydart-step-details" markdown="1">
978
979 Some output has been replaced with ellipses to save space,
980 but the output from the push command will look something this:
981
982
983 {% prettify bash %}
984 $ [[highlight]]git push heroku master[[/highlight]]
985 Initializing repository, done.
986
987 ...
988 -----> Fetching custom git buildpack... done
989 -----> Dart app detected
990 ...
991 Dart VM version: 1.0.0.5_r30248_vagrant (Thu Nov 14 13:43:29 2013) on "linux_x64 "
992 ...
993 *** Running pub build
994 Building with "pub build"
995 Building deploy_codelab....................
996 [Info in Dart2JS]:
997 Generated deploy_codelab|web/piratebadge.dart.js (97701 characters) in 0:01:19.6 85114
998 Built 6 files!
999 ...
1000 -----> Launching... done, v6
1001 [[highlight]]http://myfirstdartappforheroku.herokuapp.com deployed to Her oku[[/highlight]]
1002
1003 To git@heroku.com:myfirstdartappforheroku.git
1004 * [new branch] master -> master
1005 $
1006 {% endprettify %}
1007
1008
1009 </div>
1010
1011 <div class="trydart-filename">terminal</div>
1012
1013 </div> <div class="col-md-5" markdown="1">
1014
1015 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong>
1016
1017
1018 * Your local `git` repository becomes associated with a remote Heroku repository ,
1019 usually named `heroku`, when you push.
1020
1021 * When you push, Heroku runs the buildpack that you specified.
1022
1023 * In addition to other tasks, this buildpack runs `pub build`.
1024
1025 * When the push is finished, the process displays a URL for your app.
1026
1027 &nbsp; {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %}
1028
1029 </div> </div>
1030
1031 ### <i class="fa fa-anchor"> </i> Specify the scale
1032
1033 <div class="row"> <div class="col-md-7" markdown="1">
1034
1035 <div class="trydart-step-details" markdown="1">
1036
1037 {% prettify bash %}
1038 $ heroku ps:scale web=1
sethladd 2014/01/24 23:03:03 two spaces after scale
mem 2014/01/24 23:55:05 Done.
1039 $
1040 {% endprettify %}
1041
1042 </div>
1043
1044 <div class="trydart-filename">terminal</div>
1045
1046 </div> <div class="col-md-5" markdown="1">
1047
1048 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong>
1049
1050 * Scale to one _dyno_, which is a Heroku lightweight container
1051 that runs a single user-specified command.
1052
1053 &nbsp; {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %}
1054
1055 </div> </div>
1056
1057 <hr>
1058
1059 ##Step 6: Run the app on the web and on a mobile device {#step-six}
1060
1061 Now that your app is deployed, you can run it on the web
1062 and on a mobile device.
1063
1064 ### <i class="fa fa-anchor"> </i> Run the app in a browser
1065
1066 <div class="row"> <div class="col-md-7" markdown="1">
1067
1068 <iframe class="running-app-frame"
1069 style="height:220px;width:550px;"
1070 src="/codelabs/darrrt/examples/6-piratebadge/piratebadge.html">
1071 </iframe>
1072
1073
1074 </div> <div class="col-md-5" markdown="1">
1075 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong>
1076
1077 * Use the URL displayed by the previous step to visit your app:
1078 `http://myfirstdartappforheroku.herokuapp.com`
1079
1080 </div> </div>
1081
1082
1083 ### <i class="fa fa-anchor"> </i> Run the app on a mobile device
1084
1085 <div class="row"> <div class="col-md-7" markdown="1">
1086
1087 <div class="trydart-step-details" markdown="1">
1088
1089 <img src="images/calebthebrave.png" height="400">
1090
1091 </div>
1092
1093 </div> <div class="col-md-5" markdown="1">
1094
1095 <i class="fa fa-key key-header"> </i> <strong> Key Information </strong>
1096
1097 * Use the URL from the previous step to load the app
1098 into a browser on your mobile device.
1099 `http://myfirstdartappforheroku.herokuapp.com`
1100
1101 &nbsp; {% comment %} non-breaking space required for bootstrap/markdown bogosity {% endcomment %}
1102
1103 </div> </div>
1104
1105
1106
1107 <div class="trydart-step-details" markdown="1">
1108 </div>
1109
1110 <hr>
1111
1112 ##Resources {#resources}
1113
1114 For more information about the tools and libraries used
1115 in this code lab, check out these resources.
1116
1117 <div class="trydart-step-details" markdown="1">
1118
1119 #### SDK libraries, classes, and functions
1120
1121 * The API docs for the
1122 <a href="https://api.dartlang.org/dart_io/File.html" target="_blank">File</a>,
1123 <a href="https://api.dartlang.org/dart_io/HttpServer.html" target="_blank">HttpS erver</a>, and
1124 <a href="https://api.dartlang.org/dart_io/Platform.html" target="_blank">Platfor m</a>
1125 classes from the
1126 <a href="https://api.dartlang.org/dart_io.html" target="_blank">dart:io</a>
1127 library.
1128 * The API docs for the
1129 <a href="https://api.dartlang.org/dart_async.html#runZoned" target="_blank">runZ oned()</a>
1130 function from the
1131 <a href="https://api.dartlang.org/dart_async.html" target="_blank">dart:async</a > library.
1132
1133 #### Classes and functions from pub packages
1134
1135 * The code for the
1136 <a href="https://pub.dartlang.org/packages/http_server">http_server</a> pub pack age,
1137 in particular
1138 <a href="https://api.dartlang.org/http_server/VirtualDirectory.html" target="_bl ank">VirtualDirectory</a>
1139 class.
1140 * The code for the
1141 <a href="http://pub.dartlang.org/packages/path">path</a> pub package,
1142 in particular the
1143 <a href="https://api.dartlang.org/path.html#join" target="_blank">join()</a>
1144 and
1145 <a href="https://api.dartlang.org/path.html#dirname" target="_blank">dirname()</ a>
1146 functions.
1147
1148 #### Heroku tools
1149
1150 * The <a href="https://devcenter.heroku.com/">Heroku Dev Center</a>.
1151 * The Heroku-compatible build of the Dart SDK:
1152 [heroku-vagrant-dart-build](https://github.com/selkhateeb/heroku-vagrant-dart-bu ild).
1153 * The Heroku buildpack for Dart:
1154 [heroku-buildpack-dart](https://github.com/igrigorik/heroku-buildpack-dart).
1155
1156 #### The example code
1157
1158 * The [deploy-codelab](https://github.com/dart-lang/deploy-codelab)
1159 example code.
1160
1161 </div>
1162
1163 <hr>
1164
1165 ##What next? {#what-next}
1166
1167 Now that your app is deployed for the world to use,
1168 what do you do now?
1169 Here are some suggestions.
1170
1171 ### <i class="fa fa-anchor"> </i> Share your app with the world!
1172
1173 <div class="trydart-step-details" markdown="1">
1174
1175 Share your Heroku URL with your followers and circles.
1176
1177 * <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>
1178 <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>
1179
1180 {% comment %}
1181 * <p class="share-button twitter">
1182 <a href="https://twitter.com/share"
1183 class="twitter-share-button external-link"
1184 data-count="none"
1185 data-text="Arrr! I've deployed me app. http://dartlang.org/codelab/deploy/"
1186 data-hashtags="dartlang">Tweet</a> </p>
1187 {% endcomment %}
1188
1189 * <p>
1190 <script src="https://apis.google.com/js/plusone.js"></script>
1191 <g:plus action="share"></g:plus> </p>
1192
1193 </div>
1194
1195 ### <i class="fa fa-anchor"> </i> Jump to another ship.
1196
1197 <div class="trydart-step-details" markdown="1">
1198
1199 * If you never went through the [Avast, Ye Pirates](/codelabs/darrrt/) code lab, do it now.
1200 * For more command-line application and server code,
1201 visit [Dart by Example](/dart-by-example/).
1202 * Play with some [samples](/samples/).
1203 * Try the [tutorials](/docs/tutorials/),
1204 in particular the [command-line app tutorial](/docs/tutorials/cmdline/).
1205 * Go write some awesome code.
1206
1207 </div>
1208
OLDNEW
« no previous file with comments | « src/site/codelabs/deploy/images/wee-arrow.png ('k') | src/site/css/dart-style.css » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698