Refactor per abarth feedback
This commit is contained in:
@@ -8,18 +8,33 @@ import 'package:args/args.dart';
|
||||
import 'package:shelf_static/shelf_static.dart';
|
||||
import 'package:shelf/shelf_io.dart' as io;
|
||||
import 'package:shelf/shelf.dart';
|
||||
import 'package:shelf_route/shelf_route.dart' as shelf_route;
|
||||
|
||||
void printUsage(parser) {
|
||||
print('Usage: sky_server [-v] PORT');
|
||||
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 {
|
||||
ArgParser parser = new ArgParser();
|
||||
parser.addFlag('help', abbr: 'h', negatable: false,
|
||||
help: 'Display this help message.');
|
||||
parser.addFlag('verbose', abbr: 'v', negatable: false,
|
||||
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);
|
||||
|
||||
@@ -36,8 +51,18 @@ main(List<String> argv) async {
|
||||
return;
|
||||
}
|
||||
|
||||
Handler handler = createStaticHandler(Directory.current.path,
|
||||
serveFilesOutsidePath: true, listDirectories: true);
|
||||
var router = shelf_route.router();
|
||||
|
||||
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'])
|
||||
handler = const Pipeline().addMiddleware(logRequests()).addHandler(handler);
|
||||
|
||||
@@ -11,3 +11,4 @@ dependencies:
|
||||
args: ^0.13.0
|
||||
shelf: ^0.6.2
|
||||
shelf_static: ^0.2.3
|
||||
shelf_route: ^0.13.4
|
||||
|
||||
Reference in New Issue
Block a user