-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathexample.dart
More file actions
68 lines (57 loc) · 2.13 KB
/
example.dart
File metadata and controls
68 lines (57 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io';
import 'package:args/args.dart';
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_static/shelf_static.dart';
void main(List<String> args) {
final parser = _getParser();
String address;
int port;
bool logging;
bool listDirectories;
try {
final result = parser.parse(args);
address = result['address'] as String;
port = int.parse(result['port'] as String);
logging = result['logging'] as bool;
listDirectories = result['list-directories'] as bool;
} on FormatException catch (e) {
stderr
..writeln(e.message)
..writeln(parser.usage);
// http://linux.die.net/include/sysexits.h
// #define EX_USAGE 64 /* command line usage error */
exit(64);
}
if (!FileSystemEntity.isFileSync('example/example.dart')) {
throw StateError('Server expects to be started the '
'root of the project.');
}
var pipeline = const shelf.Pipeline();
if (logging) {
pipeline = pipeline.addMiddleware(shelf.logRequests());
}
String? defaultDoc = _defaultDoc;
if (listDirectories) {
defaultDoc = null;
}
final handler = pipeline.addHandler(createStaticHandler('example/files',
defaultDocument: defaultDoc, listDirectories: listDirectories));
io.serve(handler, address, port).then((server) {
print('Serving at http://${server.address.host}:${server.port}');
});
}
ArgParser _getParser() => ArgParser()
..addFlag('logging', abbr: 'l', defaultsTo: true, help: 'Enable logging')
..addOption('port', abbr: 'p', defaultsTo: '8080', help: 'Port to listen on')
..addOption('address',
abbr: 'a', defaultsTo: 'localhost', help: 'Address to listen on')
..addFlag('list-directories',
abbr: 'f',
negatable: false,
help: 'List the files in the source directory instead of serving the '
'default document - "$_defaultDoc".');
const _defaultDoc = 'index.html';