{"body":"use v6;\nuse Cro::HTTP::Server;\nuse Cro::HTTP::Router;\nuse Cro::HTTP::Response;\nuse Cro::HTTP::Middleware::Request;\n\n# ---------------------------\n# Middleware: strip trailing slash\n# ---------------------------\nclass StripTrailingSlash does Cro::HTTP::Middleware::Request {\n    method process($request) {\n        my $path = $request.target;\n\n        # Ignore root \"/\"\n        if $path ne '/' && $path.ends-with('/') {\n            my $new-path = $path.substr(0, *-1);\n\n            return Cro::HTTP::Response.new(\n                status  => 308,\n                headers => [ Location => $new-path ]\n            );\n        }\n\n        $request;\n    }\n}\n\n# ---------------------------\n# Handlers (keep logic separate)\n# ---------------------------\nsub list-users() {\n    content 'application/json', '[{\"id\":1,\"name\":\"Alice\"}]'\n}\n\nsub get-user($id) {\n    content 'application/json', qq|{\"id\": $id, \"name\": \"User$id\"}|\n}\n\n# ---------------------------\n# Router\n# ---------------------------\nsub routes() is export {\n    route {\n        # /users\n        get -> 'users' {\n            list-users()\n        }\n\n        # /users/123\n        get -> 'users', Int $id {\n            get-user($id)\n        }\n\n        # Example root\n        get -> {\n            content 'text/plain', 'Welcome to Cro!'\n        }\n    }\n}\n\n# ---------------------------\n# Server setup\n# ---------------------------\nmy $app = routes();\n\nmy Cro::HTTP::Server $server .= new:\n    :host<127.0.0.1>,\n    :port(3000),\n    :application($app),\n    :before(StripTrailingSlash.new);  # attach middleware\n\n$server.start;\n\nsay \"Server running at http://127.0.0.1:3000/\";\nreact whenever signal(SIGINT) {\n    $server.stop;\n    exit;\n}\n","name":"","extension":"txt","url":"https://www.irccloud.com/pastebin/kCcHlKfw","modified":1776352872,"id":"kCcHlKfw","size":1683,"lines":78,"own_paste":false,"theme":"","date":1776352872}