| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library pub.command.serve; | 5 library pub.command.serve; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:barback/barback.dart'; | 10 import 'package:barback/barback.dart'; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 }); | 71 }); |
| 72 | 72 |
| 73 barback.errors.listen((error) { | 73 barback.errors.listen((error) { |
| 74 log.error("${_red}Build error:\n$error$_none"); | 74 log.error("${_red}Build error:\n$error$_none"); |
| 75 }); | 75 }); |
| 76 | 76 |
| 77 // TODO(rnystrom): Watch file system and update sources again when they | 77 // TODO(rnystrom): Watch file system and update sources again when they |
| 78 // are added or modified. | 78 // are added or modified. |
| 79 | 79 |
| 80 HttpServer.bind("localhost", port).then((server) { | 80 HttpServer.bind("localhost", port).then((server) { |
| 81 log.message("Serving ${entrypoint.root.name} " | |
| 82 "on http://localhost:${server.port}"); | |
| 83 | |
| 84 // Add all of the visible files. | 81 // Add all of the visible files. |
| 85 for (var package in provider.packages) { | 82 for (var package in provider.packages) { |
| 86 barback.updateSources(provider.listAssets(package)); | 83 barback.updateSources(provider.listAssets(package)); |
| 87 } | 84 } |
| 88 | 85 |
| 86 log.message("Serving ${entrypoint.root.name} " |
| 87 "on http://localhost:${server.port}"); |
| 88 |
| 89 server.listen((request) { | 89 server.listen((request) { |
| 90 var id = getIdFromUri(request.uri); | 90 var id = getIdFromUri(request.uri); |
| 91 if (id == null) { | 91 if (id == null) { |
| 92 return notFound(request, "Path ${request.uri.path} is not valid."); | 92 return notFound(request, "Path ${request.uri.path} is not valid."); |
| 93 } | 93 } |
| 94 | 94 |
| 95 barback.getAssetById(id).then((asset) { | 95 barback.getAssetById(id).then((asset) { |
| 96 log.message( | 96 return validateStream(asset.read()).then((stream) { |
| 97 "$_green${request.method}$_none ${request.uri} -> $asset"); | 97 log.message( |
| 98 // TODO(rnystrom): Set content-type based on asset type. | 98 "$_green${request.method}$_none ${request.uri} -> $asset"); |
| 99 return request.response.addStream(asset.read()).then((_) { | 99 // TODO(rnystrom): Set content-type based on asset type. |
| 100 return request.response.addStream(stream).then((_) { |
| 101 request.response.close(); |
| 102 }); |
| 103 }).catchError((error) { |
| 104 log.error("$_red${request.method}$_none " |
| 105 "${request.uri} -> $error"); |
| 106 |
| 107 // If we couldn't read the asset, handle the error gracefully. |
| 108 if (error is FileException) { |
| 109 // Assume this means the asset was a file-backed source asset |
| 110 // and we couldn't read it, so treat it like a missing asset. |
| 111 notFound(request, error); |
| 112 return; |
| 113 } |
| 114 |
| 115 // Otherwise, it's some internal error. |
| 116 request.response.statusCode = 500; |
| 117 request.response.reasonPhrase = "Internal Error"; |
| 118 request.response.write(error); |
| 100 request.response.close(); | 119 request.response.close(); |
| 101 }); | 120 }); |
| 102 // TODO(rnystrom): Serve up a 500 if we get an error reading the | |
| 103 // asset. | |
| 104 }).catchError((error) { | 121 }).catchError((error) { |
| 105 log.error("$_red${request.method}$_none ${request.uri} -> $error"); | 122 log.error("$_red${request.method}$_none ${request.uri} -> $error"); |
| 106 if (error is! AssetNotFoundException) { | 123 if (error is! AssetNotFoundException) { |
| 107 completer.completeError(error); | 124 completer.completeError(error); |
| 108 return; | 125 return; |
| 109 } | 126 } |
| 110 | 127 |
| 111 notFound(request, error); | 128 notFound(request, error); |
| 112 }); | 129 }); |
| 113 }); | 130 }); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 | 180 |
| 164 // If we got here, we had a path like "/packages" which is a special | 181 // If we got here, we had a path like "/packages" which is a special |
| 165 // directory, but not a valid path since it lacks a following package name. | 182 // directory, but not a valid path since it lacks a following package name. |
| 166 if (isSpecial) return null; | 183 if (isSpecial) return null; |
| 167 | 184 |
| 168 // Otherwise, it's a path in current package's web directory. | 185 // Otherwise, it's a path in current package's web directory. |
| 169 return new AssetId(entrypoint.root.name, | 186 return new AssetId(entrypoint.root.name, |
| 170 path.url.join("web", path.url.joinAll(parts))); | 187 path.url.join("web", path.url.joinAll(parts))); |
| 171 } | 188 } |
| 172 } | 189 } |
| OLD | NEW |