Skip to content

Commit 6a34f1f

Browse files
authored
Add --quiet flag to disable request logging (#98)
* Add `--quiet` flag to disable request logging - Adds a new `-q` / `--quiet` flag to the CLI options to avoid adding the shelf `logRequests` middleware. - Fully documents the `Dhttpd.start` public API. - Updates `test/command_test.dart` to verify that no output is emitted when `--quiet` is set. - Rebuilds `options.g.dart` and `version.dart`. - Syncs the generated README output and bumps version to 4.3.0. * formatted
1 parent 7bac6b2 commit 6a34f1f

File tree

9 files changed

+68
-9
lines changed

9 files changed

+68
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
## 4.2.1-wip
1+
## 4.3.0
2+
3+
- Added `--quiet` flag to disable request logging.
24

35
## 4.2.0
46

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ $ dhttpd --help
6161
--path=<path> The path to serve. If not set, the current directory is used.
6262
-p, --port=<port> The port to listen on. Provide `0` to use a random port.
6363
(defaults to "8080")
64+
-q, --quiet Disable logging.
6465
--sslcert=<sslcert> The SSL certificate to use. Also requires sslkey
6566
--sslkey=<sslkey> The key of the SSL certificate to use. Also requires sslcert
6667
--sslkeypassword=<sslkeypassword> The password for the key of the SSL certificate to use.

bin/dhttpd.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Future<void> main(List<String> args) async {
3737
sslKey: options.sslkey,
3838
sslPassword: options.sslkeypassword,
3939
listFiles: options.listFiles,
40+
quiet: options.quiet,
4041
);
4142

4243
print('Serving ${httpd.path} at ${httpd.urlBase}');

lib/dhttpd.dart

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,37 @@ final class Dhttpd {
2323

2424
bool get isSSL => _securityContext != null;
2525

26-
/// [address] can either be a [String] or an
26+
/// Starts an HTTP server serving static files from a directory.
27+
///
28+
/// The [path] specifies the directory to serve. If omitted, the current
29+
/// working directory is used.
30+
///
31+
/// The server will listen on the specified [port], which defaults to
32+
/// [defaultPort] (`8080`). If [port] is `0`, a random available port
33+
/// will be chosen.
34+
///
35+
/// The [address] can either be a [String] or an
2736
/// [InternetAddress]. If [address] is a [String], [start] will
2837
/// perform a [InternetAddress.lookup] and use the first value in the
2938
/// list. To listen on the loopback adapter, which will allow only
3039
/// incoming connections from the local host, use the value
3140
/// [InternetAddress.loopbackIPv4] or
3241
/// [InternetAddress.loopbackIPv6]. To allow for incoming
33-
/// connection from the network use either one of the values
42+
/// connections from the network, use either one of the values
3443
/// [InternetAddress.anyIPv4] or [InternetAddress.anyIPv6] to
35-
/// bind to all interfaces or the IP address of a specific interface.
44+
/// bind to all interfaces, or use the IP address of a specific interface.
45+
///
46+
/// HTTP headers provided in [headers] will be applied to every response
47+
/// returned by the server.
48+
///
49+
/// To serve over HTTPS, provide both [sslCert] and [sslKey]. These should
50+
/// be paths to the certificate chain and private key, respectively. If the
51+
/// private key is password-protected, [sslPassword] must also be provided.
52+
///
53+
/// If [listFiles] is `true`, a directory listing will be displayed when
54+
/// navigating to a directory that does not contain an `index.html` file.
55+
///
56+
/// If [quiet] is `true`, request logging will be disabled.
3657
static Future<Dhttpd> start({
3758
String? path,
3859
int port = defaultPort,
@@ -42,6 +63,7 @@ final class Dhttpd {
4263
String? sslKey,
4364
String? sslPassword,
4465
bool listFiles = false,
66+
bool quiet = false,
4567
}) async {
4668
path ??= Directory.current.path;
4769

@@ -52,8 +74,13 @@ final class Dhttpd {
5274
..usePrivateKey(sslKey, password: sslPassword);
5375
}
5476

55-
final pipeline = const Pipeline()
56-
.addMiddleware(logRequests())
77+
var pipeline = const Pipeline();
78+
79+
if (!quiet) {
80+
pipeline = pipeline.addMiddleware(logRequests());
81+
}
82+
83+
final handler = pipeline
5784
.addMiddleware(_headersMiddleware(headers))
5885
.addHandler(
5986
createStaticHandler(
@@ -64,7 +91,7 @@ final class Dhttpd {
6491
);
6592

6693
final server = await io.serve(
67-
pipeline,
94+
handler,
6895
address,
6996
port,
7097
securityContext: securityContext,

lib/src/options.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ class Options {
4848
)
4949
final int port;
5050

51+
@CliOption(abbr: 'q', negatable: false, help: 'Disable logging.')
52+
final bool quiet;
53+
5154
@CliOption(
5255
valueHelp: 'sslcert',
5356
help: 'The SSL certificate to use. Also requires sslkey',
@@ -78,6 +81,7 @@ class Options {
7881
this.listFiles = false,
7982
this.path,
8083
required this.port,
84+
this.quiet = false,
8185
this.sslcert,
8286
this.sslkey,
8387
this.sslkeypassword,

lib/src/options.g.dart

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/version.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: dhttpd
2-
version: 4.2.1-wip
2+
version: 4.3.0
33

44
description: A static HTTP file server for easy local hosting of a directory.
55
repository: https://github.com/kevmoo/dhttpd

test/command_test.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ void main() {
1515
test('serves on specified port', _outputCheck);
1616
test('handles custom headers', _headersCheck);
1717
test('rejects invalid headers', _invalidHeadersCheck);
18+
test('does not log requests when quiet', _quietCheck);
1819
}
1920

2021
Future<void> _versionCheck() async {
@@ -50,6 +51,7 @@ $ dhttpd --help
5051
--path=<path> The path to serve. If not set, the current directory is used.
5152
-p, --port=<port> The port to listen on. Provide `0` to use a random port.
5253
(defaults to "8080")
54+
-q, --quiet Disable logging.
5355
--sslcert=<sslcert> The SSL certificate to use. Also requires sslkey
5456
--sslkey=<sslkey> The key of the SSL certificate to use. Also requires sslcert
5557
--sslkeypassword=<sslkeypassword> The password for the key of the SSL certificate to use.
@@ -121,6 +123,26 @@ Future<void> _outputCheck() async {
121123
await process.kill();
122124
}
123125

126+
Future<void> _quietCheck() async {
127+
await d.file('index.html', 'Hello World').create();
128+
129+
final process = await _runApp([
130+
'--port=8002',
131+
'--path',
132+
d.sandbox,
133+
'--quiet',
134+
]);
135+
final line = await process.stdout.next;
136+
expect(line, 'Serving ${d.sandbox} at http://localhost:8002');
137+
138+
final response = await http.get(Uri.parse('http://localhost:8002'));
139+
expect(response.statusCode, 200);
140+
expect(response.body, 'Hello World');
141+
142+
await process.kill();
143+
expect(await process.stdout.rest.toList(), isEmpty);
144+
}
145+
124146
Future<TestProcess> _runApp(List<String> args, {String? workingDirectory}) =>
125147
TestProcess.start(Platform.resolvedExecutable, [
126148
'bin/dhttpd.dart',

0 commit comments

Comments
 (0)