A simple handler for the Shelf ecosystem to serve files from Flutter assets.
Bind as root handler:
import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_flutter_asset/shelf_flutter_asset.dart';
void main() {
var assetHandler = createAssetHandler(defaultDocument: 'index.html');
io.serve(assetHandler, 'localhost', 8080);
}
Bind with shelf_router
:
import 'package:shelf_router/shelf_router.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as io;
void main() {
var app = Router();
final assetHandler = createAssetHandler();
app.get('/hello', (Request request) {
return Response.ok('hello-world');
});
app.get('/assets/<ignored|.*>', (Request request) {
return assetHandler(request.change(path: 'assets'));
});
io.serve(app, 'localhost', 8080);
}
Bind with shelf_router
and custom root path:
import 'package:shelf_router/shelf_router.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as io;
void main() {
var app = Router();
final assetHandler = createAssetHandler(rootPath: 'assets/html');
app.get('/hello', (Request request) {
return Response.ok('hello-world');
});
app.get('/assets/<ignored|.*>', (Request request) {
return assetHandler(request.change(path: 'assets/html'));
});
io.serve(app, 'localhost', 8080);
}