Breaking changes ;)

This commit is contained in:
thosakwe 2016-11-23 15:14:05 -05:00
parent 8fc27551fa
commit 196f79af94
3 changed files with 66 additions and 61 deletions

View file

@ -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();
}
```

View file

@ -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<String> 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<bool> 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;
}
}
}
}

View file

@ -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 {