OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'dart:convert'; |
| 6 import 'dart:io'; |
| 7 |
| 8 /// Contains a collection of Pages. |
| 9 abstract class Site { |
| 10 final String title; |
| 11 List<Page> pages = []; |
| 12 |
| 13 Site(this.title); |
| 14 |
| 15 String get customCss => ''; |
| 16 |
| 17 void handleGetRequest(HttpRequest request) { |
| 18 try { |
| 19 String path = request.uri.path; |
| 20 |
| 21 if (path == '/') { |
| 22 respondRedirect(request, pages.first.path); |
| 23 return; |
| 24 } |
| 25 |
| 26 for (Page page in pages) { |
| 27 if (page.path == path) { |
| 28 HttpResponse response = request.response; |
| 29 response.headers.contentType = ContentType.HTML; |
| 30 response.write(page.generate(request.uri.queryParameters)); |
| 31 response.close(); |
| 32 return; |
| 33 } |
| 34 } |
| 35 |
| 36 respond(request, createUnknownPage(path), HttpStatus.NOT_FOUND); |
| 37 } catch (e, st) { |
| 38 try { |
| 39 respond(request, createExceptionPage('$e', st), |
| 40 HttpStatus.INTERNAL_SERVER_ERROR); |
| 41 } catch (e, st) { |
| 42 HttpResponse response = request.response; |
| 43 response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR; |
| 44 response.headers.contentType = ContentType.TEXT; |
| 45 response.write('$e\n\n$st'); |
| 46 response.close(); |
| 47 } |
| 48 } |
| 49 } |
| 50 |
| 51 Page createUnknownPage(String unknownPath); |
| 52 |
| 53 Page createExceptionPage(String message, StackTrace trace); |
| 54 |
| 55 void respond(HttpRequest request, Page page, [int code = HttpStatus.OK]) { |
| 56 HttpResponse response = request.response; |
| 57 response.statusCode = code; |
| 58 response.headers.contentType = ContentType.HTML; |
| 59 response.write(page.generate(request.uri.queryParameters)); |
| 60 response.close(); |
| 61 } |
| 62 |
| 63 void respondRedirect(HttpRequest request, String pathFragment) { |
| 64 HttpResponse response = request.response; |
| 65 response.statusCode = HttpStatus.MOVED_TEMPORARILY; |
| 66 response.redirect(request.uri.resolve(pathFragment)); |
| 67 } |
| 68 } |
| 69 |
| 70 /// An entity that knows how to serve itself over http. |
| 71 abstract class Page { |
| 72 final StringBuffer buf = new StringBuffer(); |
| 73 |
| 74 final String id; |
| 75 final String title; |
| 76 final String description; |
| 77 |
| 78 Page(this.id, this.title, {this.description}); |
| 79 |
| 80 String get path => '/$id'; |
| 81 |
| 82 String generate(Map<String, String> params) { |
| 83 buf.clear(); |
| 84 generatePage(params); |
| 85 return buf.toString(); |
| 86 } |
| 87 |
| 88 void generatePage(Map<String, String> params); |
| 89 |
| 90 void h1(String text, {String classes}) { |
| 91 if (classes != null) { |
| 92 buf.writeln('<h1 class="$classes">${escape(text)}</h1>'); |
| 93 } else { |
| 94 buf.writeln('<h1>${escape(text)}</h1>'); |
| 95 } |
| 96 } |
| 97 |
| 98 void h2(String text) { |
| 99 buf.writeln('<h2>${escape(text)}</h2>'); |
| 100 } |
| 101 |
| 102 void h3(String text, {bool raw: false}) { |
| 103 buf.writeln('<h3>${raw ? text : escape(text)}</h3>'); |
| 104 } |
| 105 |
| 106 void h4(String text, {bool raw: false}) { |
| 107 buf.writeln('<h4>${raw ? text : escape(text)}</h4>'); |
| 108 } |
| 109 |
| 110 void ul<T>(Iterable<T> items, void gen(T item)) { |
| 111 buf.writeln('<ul>'); |
| 112 for (T item in items) { |
| 113 buf.write('<li>'); |
| 114 gen(item); |
| 115 buf.write('</li>'); |
| 116 } |
| 117 buf.writeln('</ul>'); |
| 118 } |
| 119 |
| 120 void inputList<T>(Iterable<T> items, void gen(T item)) { |
| 121 buf.writeln('<select size="8" style="width: 100%">'); |
| 122 for (T item in items) { |
| 123 buf.write('<option>'); |
| 124 gen(item); |
| 125 buf.write('</option>'); |
| 126 } |
| 127 buf.writeln('</select>'); |
| 128 } |
| 129 |
| 130 void div(void gen(), {String classes}) { |
| 131 if (classes != null) { |
| 132 buf.writeln('<div class="$classes">'); |
| 133 } else { |
| 134 buf.writeln('<div>'); |
| 135 } |
| 136 gen(); |
| 137 buf.writeln('</div>'); |
| 138 } |
| 139 |
| 140 void p(String text, {String style, bool raw: false, String classes}) { |
| 141 String c = classes == null ? '' : ' class="$classes"'; |
| 142 |
| 143 if (style != null) { |
| 144 buf.writeln('<p$c style="$style">${raw ? text : escape(text)}</p>'); |
| 145 } else { |
| 146 buf.writeln('<p$c>${raw ? text : escape(text)}</p>'); |
| 147 } |
| 148 } |
| 149 |
| 150 void blankslate(String str) { |
| 151 div(() => buf.writeln(str), classes: 'blankslate'); |
| 152 } |
| 153 |
| 154 bool isCurrentPage(String pathToTest) => path == pathToTest; |
| 155 } |
| 156 |
| 157 String escape(String text) => text == null ? '' : HTML_ESCAPE.convert(text); |
| 158 |
| 159 String printInteger(int value) { |
| 160 return value.toString(); |
| 161 } |
| 162 |
| 163 String printMilliseconds(num value) { |
| 164 return '${value.toStringAsFixed(1)} ms'; |
| 165 } |
| 166 |
| 167 String printPercentage(num value) { |
| 168 return '${(value * 100).toStringAsFixed(1)}%'; |
| 169 } |
OLD | NEW |