relic 0.10.0
relic: ^0.10.0 copied to clipboard
A lightweight and flexible web server inspired by Shelf for building APIs and backend services.
example/example.dart
import 'package:relic/io_adapter.dart';
import 'package:relic/relic.dart';
/// A simple 'Hello World' server
// doctag<hello-world-app>
Future<void> main() async {
// Setup app
final app =
RelicApp()
..get(
'/user/:name/age/:age',
hello,
) // route with parameters (:name & :age)
..use('/', logRequests()) // middleware on all paths below '/'
// custom fallback - optional (default is 404 Not Found)
..fallback = respondWith(
(_) => Response.notFound(
body: Body.fromString("Sorry, that doesn't compute"),
),
);
// Start the server. Defaults to using port 8080 on loopback interface
await app.serve();
}
// end:doctag<hello-world-app>
// doctag<hello-world-greeter-handler>
Response hello(final Request req) {
final name = req.pathParameters[#name];
final age = int.parse(req.pathParameters[#age]!);
return Response.ok(
body: Body.fromString('Hello $name! To think you are $age years old.'),
);
}
// end:doctag<hello-world-greeter-handler>