OLD | NEW |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
| 5 import 'dart:async'; |
5 import 'dart:convert'; | 6 import 'dart:convert'; |
6 import 'dart:io'; | 7 import 'dart:io'; |
7 | 8 |
8 import 'package:intl/intl.dart'; | 9 import 'package:intl/intl.dart'; |
9 | 10 |
10 /// Contains a collection of Pages. | 11 /// Contains a collection of Pages. |
11 abstract class Site { | 12 abstract class Site { |
12 final String title; | 13 final String title; |
13 List<Page> pages = []; | 14 List<Page> pages = []; |
| 15 List<Page> secondaryPages = []; |
14 | 16 |
15 Site(this.title); | 17 Site(this.title); |
16 | 18 |
17 String get customCss => ''; | 19 String get customCss => ''; |
18 | 20 |
19 void handleGetRequest(HttpRequest request) { | 21 Future<Null> handleGetRequest(HttpRequest request) async { |
20 try { | 22 try { |
21 String path = request.uri.path; | 23 String path = request.uri.path; |
22 | 24 |
23 if (path == '/') { | 25 if (path == '/') { |
24 respondRedirect(request, pages.first.path); | 26 respondRedirect(request, pages.first.path); |
25 return; | 27 return; |
26 } | 28 } |
27 | 29 |
28 for (Page page in pages) { | 30 for (Page page in pages) { |
29 if (page.path == path) { | 31 if (page.path == path) { |
30 HttpResponse response = request.response; | 32 HttpResponse response = request.response; |
31 response.headers.contentType = ContentType.HTML; | 33 response.headers.contentType = ContentType.HTML; |
32 response.write(page.generate(request.uri.queryParameters)); | 34 response.write(await page.generate(request.uri.queryParameters)); |
33 response.close(); | 35 response.close(); |
34 return; | 36 return; |
35 } | 37 } |
36 } | 38 } |
37 | 39 |
38 respond(request, createUnknownPage(path), HttpStatus.NOT_FOUND); | 40 for (Page page in secondaryPages) { |
| 41 if (page.path == path) { |
| 42 HttpResponse response = request.response; |
| 43 response.headers.contentType = ContentType.HTML; |
| 44 response.write(await page.generate(request.uri.queryParameters)); |
| 45 response.close(); |
| 46 return; |
| 47 } |
| 48 } |
| 49 |
| 50 await respond(request, createUnknownPage(path), HttpStatus.NOT_FOUND); |
39 } catch (e, st) { | 51 } catch (e, st) { |
40 try { | 52 try { |
41 respond(request, createExceptionPage('$e', st), | 53 await respond(request, createExceptionPage('$e', st), |
42 HttpStatus.INTERNAL_SERVER_ERROR); | 54 HttpStatus.INTERNAL_SERVER_ERROR); |
43 } catch (e, st) { | 55 } catch (e, st) { |
44 HttpResponse response = request.response; | 56 HttpResponse response = request.response; |
45 response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR; | 57 response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR; |
46 response.headers.contentType = ContentType.TEXT; | 58 response.headers.contentType = ContentType.TEXT; |
47 response.write('$e\n\n$st'); | 59 response.write('$e\n\n$st'); |
48 response.close(); | 60 response.close(); |
49 } | 61 } |
50 } | 62 } |
51 } | 63 } |
52 | 64 |
53 Page createUnknownPage(String unknownPath); | 65 Page createUnknownPage(String unknownPath); |
54 | 66 |
55 Page createExceptionPage(String message, StackTrace trace); | 67 Page createExceptionPage(String message, StackTrace trace); |
56 | 68 |
57 void respond(HttpRequest request, Page page, [int code = HttpStatus.OK]) { | 69 Future<Null> respond(HttpRequest request, Page page, |
| 70 [int code = HttpStatus.OK]) async { |
58 HttpResponse response = request.response; | 71 HttpResponse response = request.response; |
59 response.statusCode = code; | 72 response.statusCode = code; |
60 response.headers.contentType = ContentType.HTML; | 73 response.headers.contentType = ContentType.HTML; |
61 response.write(page.generate(request.uri.queryParameters)); | 74 response.write(await page.generate(request.uri.queryParameters)); |
62 response.close(); | 75 response.close(); |
63 } | 76 } |
64 | 77 |
65 void respondRedirect(HttpRequest request, String pathFragment) { | 78 void respondRedirect(HttpRequest request, String pathFragment) { |
66 HttpResponse response = request.response; | 79 HttpResponse response = request.response; |
67 response.statusCode = HttpStatus.MOVED_TEMPORARILY; | 80 response.statusCode = HttpStatus.MOVED_TEMPORARILY; |
68 response.redirect(request.uri.resolve(pathFragment)); | 81 response.redirect(request.uri.resolve(pathFragment)); |
69 } | 82 } |
70 } | 83 } |
71 | 84 |
72 /// An entity that knows how to serve itself over http. | 85 /// An entity that knows how to serve itself over http. |
73 abstract class Page { | 86 abstract class Page { |
74 final StringBuffer buf = new StringBuffer(); | 87 final StringBuffer buf = new StringBuffer(); |
75 | 88 |
76 final String id; | 89 final String id; |
77 final String title; | 90 final String title; |
78 final String description; | 91 final String description; |
79 | 92 |
80 Page(this.id, this.title, {this.description}); | 93 Page(this.id, this.title, {this.description}); |
81 | 94 |
82 String get path => '/$id'; | 95 String get path => '/$id'; |
83 | 96 |
84 String generate(Map<String, String> params) { | 97 Future<String> generate(Map<String, String> params) async { |
85 buf.clear(); | 98 buf.clear(); |
86 generatePage(params); | 99 await generatePage(params); |
87 return buf.toString(); | 100 return buf.toString(); |
88 } | 101 } |
89 | 102 |
90 void generatePage(Map<String, String> params); | 103 void generatePage(Map<String, String> params); |
91 | 104 |
92 void h1(String text, {String classes}) { | 105 void h1(String text, {String classes}) { |
93 if (classes != null) { | 106 if (classes != null) { |
94 buf.writeln('<h1 class="$classes">${escape(text)}</h1>'); | 107 buf.writeln('<h1 class="$classes">${escape(text)}</h1>'); |
95 } else { | 108 } else { |
96 buf.writeln('<h1>${escape(text)}</h1>'); | 109 buf.writeln('<h1>${escape(text)}</h1>'); |
(...skipping 25 matching lines...) Expand all Loading... |
122 void inputList<T>(Iterable<T> items, void gen(T item)) { | 135 void inputList<T>(Iterable<T> items, void gen(T item)) { |
123 buf.writeln('<select size="8" style="width: 100%">'); | 136 buf.writeln('<select size="8" style="width: 100%">'); |
124 for (T item in items) { | 137 for (T item in items) { |
125 buf.write('<option>'); | 138 buf.write('<option>'); |
126 gen(item); | 139 gen(item); |
127 buf.write('</option>'); | 140 buf.write('</option>'); |
128 } | 141 } |
129 buf.writeln('</select>'); | 142 buf.writeln('</select>'); |
130 } | 143 } |
131 | 144 |
| 145 Future<Null> asyncDiv(void gen(), {String classes}) async { |
| 146 if (classes != null) { |
| 147 buf.writeln('<div class="$classes">'); |
| 148 } else { |
| 149 buf.writeln('<div>'); |
| 150 } |
| 151 await gen(); |
| 152 buf.writeln('</div>'); |
| 153 } |
| 154 |
132 void div(void gen(), {String classes}) { | 155 void div(void gen(), {String classes}) { |
133 if (classes != null) { | 156 if (classes != null) { |
134 buf.writeln('<div class="$classes">'); | 157 buf.writeln('<div class="$classes">'); |
135 } else { | 158 } else { |
136 buf.writeln('<div>'); | 159 buf.writeln('<div>'); |
137 } | 160 } |
138 gen(); | 161 gen(); |
139 buf.writeln('</div>'); | 162 buf.writeln('</div>'); |
140 } | 163 } |
141 | 164 |
(...skipping 26 matching lines...) Expand all Loading... |
168 | 191 |
169 String escape(String text) => text == null ? '' : HTML_ESCAPE.convert(text); | 192 String escape(String text) => text == null ? '' : HTML_ESCAPE.convert(text); |
170 | 193 |
171 final NumberFormat numberFormat = new NumberFormat.decimalPattern(); | 194 final NumberFormat numberFormat = new NumberFormat.decimalPattern(); |
172 | 195 |
173 String printInteger(int value) => numberFormat.format(value); | 196 String printInteger(int value) => numberFormat.format(value); |
174 | 197 |
175 String printMilliseconds(num value) => '${numberFormat.format(value)} ms'; | 198 String printMilliseconds(num value) => '${numberFormat.format(value)} ms'; |
176 | 199 |
177 String printPercentage(num value) => '${(value * 100).toStringAsFixed(1)}%'; | 200 String printPercentage(num value) => '${(value * 100).toStringAsFixed(1)}%'; |
OLD | NEW |