Created
March 10, 2018 10:41
-
-
Save FROGGS/8c583a250f175a18f56580aac3c8e46d to your computer and use it in GitHub Desktop.
adding HTTP methods to cro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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