{"title":"Pipelines","description":"Compose Amber V2 request handlers in a deliberate order","section":"guides/routing","version":"v2","path":"guides/routing/pipelines","canonical_url":"https://amberframework.org/docs/v2/guides/routing/pipelines","markdown_url":"https://amberframework.org/docs/v2/guides/routing/pipelines.md","inherited":false,"content_markdown":"# Pipelines\n\nA pipeline is the ordered set of `HTTP::Handler`-compatible pipes applied to a\ngroup of routes. The V2 web template generates this configuration.\n\n**File: `config/routes.cr` — this is the generated baseline. Edit the existing\npipelines in place; do not create a second `Amber::Server.configure` block only\nto change their order.**\n\n```crystal\nAmber::Server.configure do\n  pipeline :web do\n    plug Amber::Pipe::Error.new\n    plug Amber::Pipe::Logger.new\n    plug Amber::Pipe::Session.new\n    plug Amber::Pipe::Flash.new\n    plug Amber::Pipe::CSRF.new\n  end\n\n  pipeline :static do\n    plug Amber::Pipe::Error.new\n    plug Amber::Pipe::Static.new(\"./public\")\n  end\n\n  routes :web do\n    get \"/\", HomeController, :index\n  end\n\n  routes :static do\n    get \"/*\", Amber::Controller::Static, :index\n  end\nend\n```\n\nOrder is behavior. `Session` must run before `Flash`, and error handling should\nwrap work that can fail. Add authentication, rate limiting, or application\nheaders deliberately to only the pipelines that need them.\n\n## A protected pipeline\n\nDefine a second pipeline when a route group needs additional handling.\n\n**File: `config/routes.cr` — add both the `:admin` pipeline and its route group\ninside the existing `Amber::Server.configure` block.**\n\n```crystal\nAmber::Server.configure do\n  pipeline :admin do\n    plug Amber::Pipe::Error.new\n    plug Amber::Pipe::Logger.new\n    plug Amber::Pipe::Session.new\n    plug Amber::Pipe::Flash.new\n    plug AuthenticateAdmin.new\n    plug Amber::Pipe::CSRF.new\n  end\n\n  routes :admin, \"/admin\" do\n    get \"/\", AdminController, :index\n  end\nend\n```\n\nCustom pipes implement `call(context)` and invoke the next handler when the\nrequest should continue. Put `AuthenticateAdmin` in its own source file, for\nexample `src/pipes/authenticate_admin.cr`, and require that file from the\napplication before `config/routes.cr` is compiled. A pipe that finalizes a\nresponse can stop the chain."}