1.0.0
This commit is contained in:
parent
3bff493d36
commit
74e01cf576
5 changed files with 62 additions and 11 deletions
11
README.md
11
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<String` of filenames that should be served as index pages. Default is `['index.html']`.
|
||||
- **virtualRoot**: To serve index files, you need to specify the virtual path under which
|
||||
angel_static is serving your files. If you are not serving static files at the site root,
|
||||
please include this.
|
||||
|
|
|
@ -1,11 +1,26 @@
|
|||
library angel_static;
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'package:angel_framework/angel_framework.dart';
|
||||
import 'package:mime/mime.dart' show lookupMimeType;
|
||||
|
||||
Future<bool> _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<String> 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;
|
||||
|
|
|
@ -2,7 +2,7 @@ name: angel_static
|
|||
description: Static server middleware for Angel.
|
||||
homepage: https://github.com/angel-dart/angel_static
|
||||
author: thosakwe <thosakwe@gmail.com>
|
||||
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"
|
||||
|
|
|
@ -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!"));
|
||||
});
|
||||
});
|
||||
}
|
1
test/index.txt
Normal file
1
test/index.txt
Normal file
|
@ -0,0 +1 @@
|
|||
index!
|
Loading…
Reference in a new issue