Commit 471d7b48 authored by Collin Jackson's avatar Collin Jackson

Refactor per abarth feedback

parent 66657a81
...@@ -8,18 +8,33 @@ import 'package:args/args.dart'; ...@@ -8,18 +8,33 @@ import 'package:args/args.dart';
import 'package:shelf_static/shelf_static.dart'; import 'package:shelf_static/shelf_static.dart';
import 'package:shelf/shelf_io.dart' as io; import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf/shelf.dart'; import 'package:shelf/shelf.dart';
import 'package:shelf_route/shelf_route.dart' as shelf_route;
void printUsage(parser) { void printUsage(parser) {
print('Usage: sky_server [-v] PORT'); print('Usage: sky_server [-v] PORT');
print(parser.usage); print(parser.usage);
} }
void addRoute(var router, String route, String path) {
router.add(
route,
['GET'],
createStaticHandler(
path,
serveFilesOutsidePath: true,
listDirectories: true
), exactMatch: false
);
}
main(List<String> argv) async { main(List<String> argv) async {
ArgParser parser = new ArgParser(); ArgParser parser = new ArgParser();
parser.addFlag('help', abbr: 'h', negatable: false, parser.addFlag('help', abbr: 'h', negatable: false,
help: 'Display this help message.'); help: 'Display this help message.');
parser.addFlag('verbose', abbr: 'v', negatable: false, parser.addFlag('verbose', abbr: 'v', negatable: false,
help: 'Log requests to stdout.'); help: 'Log requests to stdout.');
parser.addOption('route', allowMultiple: true, splitCommas: false,
help: 'Adds a virtual directory to the root.');
ArgResults args = parser.parse(argv); ArgResults args = parser.parse(argv);
...@@ -36,8 +51,18 @@ main(List<String> argv) async { ...@@ -36,8 +51,18 @@ main(List<String> argv) async {
return; return;
} }
Handler handler = createStaticHandler(Directory.current.path, var router = shelf_route.router();
serveFilesOutsidePath: true, listDirectories: true);
if (args['route'] != null) {
for (String arg in args['route']) {
List<String> parsedArgs = arg.split(',');
addRoute(router, parsedArgs[0], parsedArgs[1]);
}
}
addRoute(router, '/', Directory.current.path);
var handler = router.handler;
if (args['verbose']) if (args['verbose'])
handler = const Pipeline().addMiddleware(logRequests()).addHandler(handler); handler = const Pipeline().addMiddleware(logRequests()).addHandler(handler);
......
...@@ -11,3 +11,4 @@ dependencies: ...@@ -11,3 +11,4 @@ dependencies:
args: ^0.13.0 args: ^0.13.0
shelf: ^0.6.2 shelf: ^0.6.2
shelf_static: ^0.2.3 shelf_static: ^0.2.3
shelf_route: ^0.13.4
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment