# Pastebin kCcHlKfw use v6; use Cro::HTTP::Server; use Cro::HTTP::Router; use Cro::HTTP::Response; use Cro::HTTP::Middleware::Request; # --------------------------- # Middleware: strip trailing slash # --------------------------- class StripTrailingSlash does Cro::HTTP::Middleware::Request { method process($request) { my $path = $request.target; # Ignore root "/" if $path ne '/' && $path.ends-with('/') { my $new-path = $path.substr(0, *-1); return Cro::HTTP::Response.new( status => 308, headers => [ Location => $new-path ] ); } $request; } } # --------------------------- # Handlers (keep logic separate) # --------------------------- sub list-users() { content 'application/json', '[{"id":1,"name":"Alice"}]' } sub get-user($id) { content 'application/json', qq|{"id": $id, "name": "User$id"}| } # --------------------------- # Router # --------------------------- sub routes() is export { route { # /users get -> 'users' { list-users() } # /users/123 get -> 'users', Int $id { get-user($id) } # Example root get -> { content 'text/plain', 'Welcome to Cro!' } } } # --------------------------- # Server setup # --------------------------- my $app = routes(); my Cro::HTTP::Server $server .= new: :host<127.0.0.1>, :port(3000), :application($app), :before(StripTrailingSlash.new); # attach middleware $server.start; say "Server running at http://127.0.0.1:3000/"; react whenever signal(SIGINT) { $server.stop; exit; }