spry 2.0.2 spry: ^2.0.2 copied to clipboard
Spry is an HTTP middleware framework for Dart to make web applications and APIs server more enjoyable to write.
import 'dart:io';
import 'package:spry/session.dart';
import 'package:spry/spry.dart';
void main() async {
final Spry spry = Spry();
// logger
spry.use((Context context, Next next) async {
await next();
final Response response = context.response;
final String? rt = response.headers.value('x-response-time');
print('${context.request.method} ${context.request.uri} - $rt');
});
// x-response-time
spry.use((Context context, Next next) async {
final Stopwatch stopwatch = Stopwatch()..start();
print(context.session['foo']);
await next();
stopwatch.stop();
context.response.headers
.set('x-response-time', '${stopwatch.elapsedMilliseconds}ms');
});
// Create handler
handler(Context context) {
context.response.statusCode = HttpStatus.ok;
context.response.text('Hello World!');
context.session['foo'] = 'bar';
context.response.cookies.add(Cookie('a', 'b'));
// context.response.cookies.add(value);
// context.session.destroy();
}
// Listen
final server = await spry.listen(handler, port: 3000);
print('Server running at http://localhost:${server.port}/');
}