Skip to content

Commit 60390f6

Browse files
authored
feat(dart_frog): createStaticFileHandler (#103)
1 parent 6ca4af7 commit 60390f6

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

packages/dart_frog/lib/dart_frog.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export 'src/_internal.dart'
1111
Response,
1212
Router,
1313
serve;
14+
export 'src/create_static_file_handler.dart' show createStaticFileHandler;
1415
export 'src/handler.dart' show Handler;
1516
export 'src/hot_reload.dart' show hotReload;
1617
export 'src/http_method.dart' show HttpMethod;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import 'package:dart_frog/dart_frog.dart';
2+
import 'package:dart_frog/src/_internal.dart';
3+
import 'package:shelf_static/shelf_static.dart';
4+
5+
/// Creates a [Handler] that serves static files within provided [path].
6+
/// Defaults to the `public` directory.
7+
Handler createStaticFileHandler({String path = 'public'}) {
8+
return fromShelfHandler(createStaticHandler(path));
9+
}

packages/dart_frog/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ dependencies:
1010
http_methods: ^1.1.0
1111
shelf: ^1.1.0
1212
shelf_hotreload: ^1.3.0
13+
shelf_static: ^1.1.1
1314

1415
dev_dependencies:
1516
http: ^0.13.4
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import 'dart:convert';
2+
import 'dart:io';
3+
4+
import 'package:dart_frog/dart_frog.dart';
5+
import 'package:http/http.dart' as http;
6+
import 'package:path/path.dart' as path;
7+
import 'package:test/test.dart';
8+
9+
void main() {
10+
group('createStaticFileHandler', () {
11+
test('serves static files', () async {
12+
const port = 3003;
13+
final tempDir = Directory.systemTemp.createTempSync();
14+
15+
const messageContents = 'hello world';
16+
File(
17+
path.join(tempDir.path, 'message.txt'),
18+
).writeAsStringSync(messageContents);
19+
20+
final profileContents = json.encode(
21+
<String, dynamic>{'name': 'dash', 'age': 42, 'lovesDart': true},
22+
);
23+
File(
24+
path.join(tempDir.path, 'profile.json'),
25+
).writeAsStringSync(profileContents);
26+
27+
final server = await serve(
28+
createStaticFileHandler(path: tempDir.path),
29+
'localhost',
30+
port,
31+
);
32+
33+
final messageResponse = await http.get(
34+
Uri.parse('http://localhost:$port/message.txt'),
35+
);
36+
expect(messageResponse.statusCode, equals(HttpStatus.ok));
37+
expect(messageResponse.body, equals(messageContents));
38+
39+
final profileResponse = await http.get(
40+
Uri.parse('http://localhost:$port/profile.json'),
41+
);
42+
expect(profileResponse.statusCode, equals(HttpStatus.ok));
43+
expect(profileResponse.body, equals(profileContents));
44+
45+
final notFoundResponse = await http.get(
46+
Uri.parse('http://localhost:$port/not-found'),
47+
);
48+
expect(notFoundResponse.statusCode, equals(HttpStatus.notFound));
49+
50+
await server.close();
51+
tempDir.delete(recursive: true).ignore();
52+
});
53+
});
54+
}

0 commit comments

Comments
 (0)