Skip to content

Commit 61b3a9e

Browse files
committed
feat: add default document on static file handlers
1 parent 9699fb0 commit 61b3a9e

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,22 @@ 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 serve a 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 `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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,46 @@ 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 profileContents = json.encode(
69+
<String, dynamic>{'name': 'dash', 'age': 42, 'lovesDart': true},
70+
);
71+
File(
72+
path.join(tempDir.path, 'profile.json'),
73+
).writeAsStringSync(profileContents);
74+
75+
final server = await serve(
76+
createStaticFileHandler(
77+
path: tempDir.path,
78+
defaultDocument: 'message.txt',
79+
),
80+
'localhost',
81+
port,
82+
);
83+
84+
final messageResponse = await http.get(
85+
Uri.parse('http://localhost:$port/'),
86+
);
87+
expect(messageResponse.statusCode, equals(HttpStatus.ok));
88+
expect(messageResponse.body, equals(messageContents));
89+
5090
await server.close();
5191
tempDir.delete(recursive: true).ignore();
5292
});

0 commit comments

Comments
 (0)