From 196f79af9444c4872a05432490a10186f892cbb1 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Wed, 23 Nov 2016 15:14:05 -0500 Subject: [PATCH] Breaking changes ;) --- README.md | 8 +-- lib/src/virtual_directory.dart | 90 ++++++++++++++++++++-------------- test/all_test.dart | 29 +++++------ 3 files changed, 66 insertions(+), 61 deletions(-) diff --git a/README.md b/README.md index 9a442161..9e186754 100644 --- a/README.md +++ b/README.md @@ -25,13 +25,7 @@ import 'package:angel_static/angel_static.dart'; main() async { final app = new Angel(); - - app.mount('/virtual', new VirtualDirectory( - source: new Directory('./foo/bar'), - publicPath: '/virtual')); - - app.mount('/', new VirtualDirectory(source: new Directory('./public'))); - + await app.configure(new VirtualDirectory(source: new Directory('./public'))); await app.startServer(); } ``` diff --git a/lib/src/virtual_directory.dart b/lib/src/virtual_directory.dart index 5037d5a9..a16ffce4 100644 --- a/lib/src/virtual_directory.dart +++ b/lib/src/virtual_directory.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'dart:io'; import 'package:angel_framework/angel_framework.dart'; +import 'package:angel_route/angel_route.dart'; import 'package:mime/mime.dart' show lookupMimeType; final RegExp _param = new RegExp(r':([A-Za-z0-9_]+)(\((.+)\))?'); @@ -22,7 +23,9 @@ String _pathify(String path) { return p; } -class VirtualDirectory extends Router { +class VirtualDirectory { + final bool debug; + String _prefix; Directory _source; Directory get source => _source; final List indexFileNames; @@ -30,10 +33,11 @@ class VirtualDirectory extends Router { VirtualDirectory( {Directory source, - bool debug: false, + this.debug: false, this.indexFileNames: const ['index.html'], - this.publicPath: '/'}) - : super(debug: debug) { + this.publicPath: '/'}) { + _prefix = publicPath.replaceAll(_straySlashes, ''); + if (source != null) { _source = source; } else { @@ -42,45 +46,14 @@ class VirtualDirectory extends Router { : './web'; _source = new Directory(dirPath); } - - final prefix = publicPath.replaceAll(_straySlashes, ''); - _printDebug('Source directory: ${source.absolute.path}'); - _printDebug('Public path prefix: "$prefix"'); - - get('*', (RequestContext req, ResponseContext res) async { - var path = req.path.replaceAll(_straySlashes, ''); - - if (prefix.isNotEmpty) { - path = path.replaceAll(new RegExp('^' + _pathify(prefix)), ''); - } - - final file = new File.fromUri(source.absolute.uri.resolve(path)); - _printDebug('Attempting to statically serve file: ${file.absolute.path}'); - - if (await file.exists()) { - return sendFile(file, res); - } else { - // Try to resolve index - if (path.isEmpty) { - for (String indexFileName in indexFileNames) { - final index = - new File.fromUri(source.absolute.uri.resolve(indexFileName)); - if (await index.exists()) { - return await sendFile(index, res); - } - } - } else { - _printDebug('File "$path" does not exist, and is not an index.'); - return true; - } - } - }); } _printDebug(msg) { if (debug) print(msg); } + call(AngelBase app) async => serve(app); + Future sendFile(File file, ResponseContext res) async { _printDebug('Streaming file ${file.absolute.path}...'); res @@ -88,7 +61,48 @@ class VirtualDirectory extends Router { ..header(HttpHeaders.CONTENT_TYPE, lookupMimeType(file.path)) ..status(200); await res.streamFile(file); - await res.underlyingResponse.close(); + await res.io.close(); return false; } + + void serve(Router router) { + _printDebug('Source directory: ${source.absolute.path}'); + _printDebug('Public path prefix: "$_prefix"'); + + handler(RequestContext req, ResponseContext res) async { + var path = req.path.replaceAll(_straySlashes, ''); + + return serveFile(path, res); + } + + router.get('$publicPath/*', handler); + router.get(publicPath, (req, res) => serveFile('', res)); + } + + serveFile(String path, ResponseContext res) async { + if (_prefix.isNotEmpty) { + path = path.replaceAll(new RegExp('^' + _pathify(_prefix)), ''); + } + + final file = new File.fromUri(source.absolute.uri.resolve(path)); + _printDebug('Attempting to statically serve file: ${file.absolute.path}'); + + if (await file.exists()) { + return sendFile(file, res); + } else { + // Try to resolve index + if (path.isEmpty) { + for (String indexFileName in indexFileNames) { + final index = + new File.fromUri(source.absolute.uri.resolve(indexFileName)); + if (await index.exists()) { + return await sendFile(index, res); + } + } + } else { + _printDebug('File "$path" does not exist, and is not an index.'); + return true; + } + } + } } diff --git a/test/all_test.dart b/test/all_test.dart index a4a7baaa..9b41167c 100644 --- a/test/all_test.dart +++ b/test/all_test.dart @@ -11,34 +11,31 @@ main() { Client client = new Client(); setUp(() async { - app = new Angel(); + app = new Angel(debug: true); - app.mount( - '/virtual', - new VirtualDirectory( - debug: true, - source: testDir, - publicPath: '/virtual', - indexFileNames: ['index.txt'])); + await app.configure(new VirtualDirectory( + debug: true, + source: testDir, + publicPath: '/virtual', + indexFileNames: ['index.txt'])); - app.mount( - '/', - new VirtualDirectory( - debug: true, - source: testDir, - indexFileNames: ['index.php', 'index.txt'])); + await app.configure(new VirtualDirectory( + debug: true, + source: testDir, + indexFileNames: ['index.php', 'index.txt'])); app.get('*', 'Fallback'); + app ..normalize() - ..dumpTree(); + ..dumpTree(showMatchers: true); await app.startServer(InternetAddress.LOOPBACK_IP_V4, 0); url = "http://${app.httpServer.address.host}:${app.httpServer.port}"; }); tearDown(() async { - await app.httpServer.close(force: true); + if (app.httpServer != null) await app.httpServer.close(force: true); }); test('can serve files, with correct Content-Type', () async {