#!/usr/bin/env perl6
use Cro::HTTP::Router;
use lib 'lib';
use Router;
my $application = build-router;
my Cro::Service $hello = Cro::HTTP::Server.new:
:host<localhost>, :port<10000>, :$application;
$hello.start;
react whenever signal(SIGINT) { $hello.stop; exit; }
get:
- Ctrl::Greet::&greet
- Ctrl::Greet::&bye
unit module Router;
use Cro::HTTP::Server;
use Cro::HTTP::Router;
use YAML::Parser::LibYAML;
sub build-router is export {
my %routes = yaml-parse('routes.yaml');
my %map = (
get => &get,
);
route {
for %routes.keys -> $method {
for @(%routes{$method}) -> $c {
say "Loading $c";
try {
CATCH { default { warn "Failed to load {$c.split('::&')[0]}\n"~$_.Str } };
my ($ct, $me) = $c.split('::&');
$me = "&{$me}";
require ::($ct.Str);
%map{$method}.(
::("{$ct.Str}::{$me.Str}")
);
};
}
}
};
}
use Cro::HTTP::Router;
module Ctrl::Greet {
our sub greet('greet', $word) {
content 'text/plain', "Hello $word!";
}
our sub bye('bye', $word) {
content 'text/plain', "Good bye $word!";
}
}