Skip to content

Instantly share code, notes, and snippets.

@FROGGS
Created March 10, 2018 10:41
Show Gist options
  • Save FROGGS/8c583a250f175a18f56580aac3c8e46d to your computer and use it in GitHub Desktop.
Save FROGGS/8c583a250f175a18f56580aac3c8e46d to your computer and use it in GitHub Desktop.
adding HTTP methods to cro
use Cro::HTTP::Router;
sub r(Str $method, &handler --> Nil) { $*CRO-ROUTE-SET.add-handler($method, &handler) }
sub routes() is export {
route {
get -> {
content 'text/html', '<h1> hello </h1>';
}
r 'LINK', -> {
content 'text/html', '<h1> hello LINK </h1>';
}
r 'UNLINK', -> {
content 'text/html', '<h1> hello UNLINK </h1>';
}
get -> 'js', *@path {
static 'static/js', @path
}
}
}
use Cro::HTTP::Log::File;
use Cro::HTTP::Server;
use Routes;
Cro::HTTP::RequestParser.^find_method('TWEAK').wrap(method {
callwith(self, :allowed-methods<GET HEAD POST PUT DELETE PATCH CONNECT OPTIONS LINK UNLINK>);
});
my Cro::Service $http = Cro::HTTP::Server.new(
http => <1.1>,
host => %*ENV<FOO_HOST> || die("Missing FOO_HOST in environment"),
port => %*ENV<FOO_PORT> || die("Missing FOO_PORT in environment"),
application => routes(),
after => [
Cro::HTTP::Log::File.new(logs => $*OUT, errors => $*ERR)
]
);
$http.start;
say "Listening at http://%*ENV<FOO_HOST>:%*ENV<FOO_PORT>";
react {
whenever signal(SIGINT) {
say "Shutting down...";
$http.stop;
done;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment