Skip to content

Commit fb66298

Browse files
mtwichelfelangel
andauthored
feat(dart_frog): add defaultDocument to createStaticFileHandler (#1901)
Co-authored-by: Felix Angelov <felangelov@gmail.com>
1 parent 88778f0 commit fb66298

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

docs/src/content/docs/basics/static-files.mdx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,23 @@ Future<HttpServer> run(Handler handler, InternetAddress ip, int port) {
8282

8383
In the above example, we're using `api/static` as our static file directory but
8484
you can specify a path to any directory for Dart Frog to use.
85+
86+
You can also configure the default document by specifying the `defaultDocument`
87+
parameter when creating the `createStaticFileHandler` handler.
88+
89+
```dart
90+
import 'dart:io';
91+
92+
import 'package:dart_frog/dart_frog.dart';
93+
94+
Future<HttpServer> run(Handler handler, InternetAddress ip, int port) {
95+
const customStaticFilePath = 'api/static';
96+
final cascade = Cascade()
97+
.add(createStaticFileHandler(path: customStaticFilePath, defaultDocument: 'index.html'))
98+
.add(handler);
99+
return serve(cascade.handler, ip, port);
100+
}
101+
```
102+
103+
In this example, requests made to the root of the server will serve the
104+
`index.html` file.

packages/dart_frog/lib/src/create_static_file_handler.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import 'package:shelf_static/shelf_static.dart';
33

44
/// Creates a [Handler] that serves static files within provided [path].
55
/// Defaults to the `public` directory.
6-
Handler createStaticFileHandler({String path = 'public'}) {
7-
return fromShelfHandler(createStaticHandler(path));
6+
Handler createStaticFileHandler({
7+
String path = 'public',
8+
String? defaultDocument,
9+
}) {
10+
return fromShelfHandler(
11+
createStaticHandler(path, defaultDocument: defaultDocument),
12+
);
813
}

packages/dart_frog/test/src/create_static_file_handler_test.dart

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,39 @@ void main() {
4747
);
4848
expect(notFoundResponse.statusCode, equals(HttpStatus.notFound));
4949

50+
final rootResponse = await http.get(
51+
Uri.parse('http://localhost:$port/'),
52+
);
53+
expect(rootResponse.statusCode, equals(HttpStatus.notFound));
54+
55+
await server.close();
56+
tempDir.delete(recursive: true).ignore();
57+
});
58+
59+
test('serves default document', () async {
60+
const port = 3003;
61+
final tempDir = Directory.systemTemp.createTempSync();
62+
63+
const messageContents = 'hello world';
64+
File(
65+
path.join(tempDir.path, 'message.txt'),
66+
).writeAsStringSync(messageContents);
67+
68+
final server = await serve(
69+
createStaticFileHandler(
70+
path: tempDir.path,
71+
defaultDocument: 'message.txt',
72+
),
73+
'localhost',
74+
port,
75+
);
76+
77+
final messageResponse = await http.get(
78+
Uri.parse('http://localhost:$port/'),
79+
);
80+
expect(messageResponse.statusCode, equals(HttpStatus.ok));
81+
expect(messageResponse.body, equals(messageContents));
82+
5083
await server.close();
5184
tempDir.delete(recursive: true).ignore();
5285
});

0 commit comments

Comments
 (0)