Course of Raku / Functional, concurrent, reactive, and web programming / Web programming / Cro 101 / Exercises / A second route
Solution: A second route
Here is a possible solution to the task.
Code
use Cro::HTTP::Router;
use Cro::HTTP::Server;
my $application = route {
get -> 'hello' {
content 'text/plain', 'Hello from Cro!';
}
get -> 'bye' {
content 'text/plain', 'Goodbye!';
}
}
my $server = Cro::HTTP::Server.new(
:host('127.0.0.1'),
:port(8080),
:application($application),
);
$server.start;
say 'Listening on http://127.0.0.1:8080 — press Ctrl-C to stop';
react {
whenever signal(SIGINT) {
$server.stop;
done;
}
}🦋 You can find the source code in the file second-route.raku.
Output
Goodbye!Comments
A
routeblock can hold as many routes as you like; eachgethandles one path.Cro matches the request path to the right route, so
/helloand/byereturn their own responses.As on the theory page,
.startreturns immediately, so the closingreactblock keeps the program alive until you press Ctrl-C —whenever signal(SIGINT)then stops the server and ends the reaction.