From 74e01cf57674a671a57333273eb1d030e73384c3 Mon Sep 17 00:00:00 2001 From: regiostech Date: Mon, 2 May 2016 19:11:25 -0400 Subject: [PATCH] 1.0.0 --- README.md | 11 ++++++++++- lib/angel_static.dart | 40 ++++++++++++++++++++++++++++++++-------- pubspec.yaml | 2 +- test/all_tests.dart | 19 ++++++++++++++++++- test/index.txt | 1 + 5 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 test/index.txt diff --git a/README.md b/README.md index f792efc4..9501e580 100644 --- a/README.md +++ b/README.md @@ -20,10 +20,19 @@ import 'package:angel_static/angel_static.dart'; main() async { Angel angel = new Angel(); - angel.registerMiddleware("static", serveStatic(new Directory("build/web"))); + angel.registerMiddleware("static", serveStatic()); + angel.get('/virtual*', serveStatic(virtualRoot: '/virtual')); angel.get("*", "static"); await angel.startServer(InternetAddress.LOOPBACK_IP_V4, 8080); } ``` +# Options +`serveStatic` accepts two named parameters. +- **sourceDirectory**: A `Directory` containing the files to be served. If left null, then Angel will serve either from `build` or + `build/web`, depending on your `ANGEL_ENV`. +- **indexFileNames**: A `List _sendFile(File file, ResponseContext res) async { + res + ..willCloseItself = true + ..header(HttpHeaders.CONTENT_TYPE, lookupMimeType(file.path)) + ..status(200); + await res.streamFile(file); + await res.underlyingResponse.close(); + return false; +} + /// Serves files statically from a given directory. -Middleware serveStatic([Directory sourceDirectory]) { +RequestMiddleware serveStatic({ +Directory sourceDirectory, +List indexFileNames: const['index.html'], +String virtualRoot: '/' +}) { if (sourceDirectory == null) { String dirPath = Platform.environment['ANGEL_ENV'] == 'production' ? './build/web' @@ -13,18 +28,27 @@ Middleware serveStatic([Directory sourceDirectory]) { sourceDirectory = new Directory(dirPath); } + RegExp requestingIndex = new RegExp(r'^((\/)|(\\))*$'); + return (RequestContext req, ResponseContext res) async { String requested = req.path.replaceAll(new RegExp(r'^\/'), ''); File file = new File.fromUri( sourceDirectory.absolute.uri.resolve(requested)); if (await file.exists()) { - res - ..willCloseItself = true - ..header(HttpHeaders.CONTENT_TYPE, lookupMimeType(file.path)) - ..status(200); - await res.streamFile(file); - await res.underlyingResponse.close(); - return false; + return await _sendFile(file, res); + } + + // Try to resolve index + String relative = req.path.replaceFirst(virtualRoot, "") + .replaceAll(new RegExp(r'^\/+'), ""); + if (requestingIndex.hasMatch(relative) || relative.isEmpty) { + for (String indexFileName in indexFileNames) { + file = + new File.fromUri(sourceDirectory.absolute.uri.resolve(indexFileName)); + if (await file.exists()) { + return await _sendFile(file, res); + } + } } return true; diff --git a/pubspec.yaml b/pubspec.yaml index bbfd262b..e0ac55cb 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,7 +2,7 @@ name: angel_static description: Static server middleware for Angel. homepage: https://github.com/angel-dart/angel_static author: thosakwe -version: 1.0.0-beta.2 +version: 1.0.0 dependencies: angel_framework: ">=0.0.0-dev < 0.1.0" mime: ">= 0.9.3 < 0.10.0" diff --git a/test/all_tests.dart b/test/all_tests.dart index e1861de3..da7c9412 100644 --- a/test/all_tests.dart +++ b/test/all_tests.dart @@ -12,7 +12,14 @@ main() { setUp(() async { angel = new Angel(); - angel.registerMiddleware("static", serveStatic(new Directory("test"))); + angel.registerMiddleware( + "static", serveStatic(sourceDirectory: new Directory("test"), + indexFileNames: ['index.php', 'index.txt'])); + angel.get('/virtual/*', "Fallback", + middleware: [serveStatic(sourceDirectory: new Directory("test"), + virtualRoot: '/virtual', + indexFileNames: ['index.txt']) + ]); angel.get("*", "Fallback", middleware: ["static"]); await angel.startServer(InternetAddress.LOOPBACK_IP_V4, 0); @@ -33,5 +40,15 @@ main() { var response = await client.get("$url/nonexist.ent"); expect(response.body, equals('"Fallback"')); }); + + test('can match index files', () async { + var response = await client.get(url); + expect(response.body, equals("index!")); + }); + + test('virtualRoots can match index', () async { + var response = await client.get("$url/virtual"); + expect(response.body, equals("index!")); + }); }); } \ No newline at end of file diff --git a/test/index.txt b/test/index.txt new file mode 100644 index 00000000..cb655f03 --- /dev/null +++ b/test/index.txt @@ -0,0 +1 @@ +index! \ No newline at end of file