platform/packages/cache/example/main.dart

36 lines
1 KiB
Dart
Raw Normal View History

2018-04-02 02:43:00 +00:00
import 'package:angel_cache/angel_cache.dart';
import 'package:angel_framework/angel_framework.dart';
import 'package:glob/glob.dart';
main() async {
2018-10-21 09:35:04 +00:00
var app = new Angel();
2018-04-02 02:43:00 +00:00
// Cache a glob
var cache = new ResponseCache()
..patterns.addAll([
new Glob('/*.txt'),
]);
// Handle `if-modified-since` header, and also send cached content
2018-10-21 09:35:04 +00:00
app.fallback(cache.handleRequest);
2018-04-02 02:43:00 +00:00
// A simple handler that returns a different result every time.
app.get('/date.txt',
2018-10-21 09:35:04 +00:00
(req, res) => res.write(new DateTime.now().toIso8601String()));
2018-04-02 02:43:00 +00:00
// Support purging the cache.
2018-10-21 09:35:04 +00:00
app.addRoute('PURGE', '*', (req, res) {
2018-04-02 02:43:00 +00:00
if (req.ip != '127.0.0.1') throw new AngelHttpException.forbidden();
cache.purge(req.uri.path);
print('Purged ${req.uri.path}');
});
// The response finalizer that actually saves the content
app.responseFinalizers.add(cache.responseFinalizer);
var http = new AngelHttp(app);
var server = await http.startServer('127.0.0.1', 3000);
print('Listening at http://${server.address.address}:${server.port}');
}