{"title":"Pipelines","description":"","section":"guides/routing","version":"v1.5","path":"guides/routing/pipelines","canonical_url":"https://amberframework.org/docs/v1.5/guides/routing/pipelines","markdown_url":"https://amberframework.org/docs/v1.5/guides/routing/pipelines.md","inherited":true,"content_markdown":"# Pipelines\n\nA _pipeline_ is a set of of transformations that is performed to a HTTP request. These transformations come in the form of a pipe. A _pipe_ is a class which includes [HTTP::Handler](https://crystal-lang.org/api/latest/HTTP/Handler.html) and implements the [\\#call](https://crystal-lang.org/api/latest/HTTP/Handler.html#call%28context%3AHTTP%3A%3AServer%3A%3AContext%29-instance-method) method.\n\nYou can use a handler to intercept any incoming request and modify the response. These can be used for request throttling, ip-based whitelisting, adding custom headers.\n\nEvery Amber application needs to define a _pipeline_ with a set of _pipes_ each pipeline allow a set of transformations to be applied to different sets of route, this give you granular control and explicitness of which transformation to run for each of the requests.\n\n```crystal\nAmber::Server.configure do |app|\n  pipeline :web do\n    # Plug is the method to use connect a pipe (middleware)\n    # A plug accepts an instance of HTTP::Handler\n    # plug Amber::Pipe::Params.new\n    plug Amber::Pipe::Logger.new\n    plug Amber::Pipe::Flash.new\n    plug Amber::Pipe::Session.new\n    plug Amber::Pipe::CSRF.new\n  end\n\n  # All static content will run these transformations\n  pipeline :static do\n    plug HTTP::StaticFileHandler.new(\"./public\")\n    plug HTTP::CompressHandler.new\n  end\n\n  routes :static do\n    # Each route is defined as follow\n    # Verb resource : String, controller : Symbol, action : Symbol\n    get \"/*\", StaticController, :index\n  end\n\n  routes :web do\n    get \"/\", HomeController, :static\n  end\nend\n```\n\nAbove we define a pipeline called `:web` as you can see the `:web` pipeline transforms the request using a _Logger_, _StaticFileHandler_ and a _CompressHandler_, all of which extends from the Crystal [_HTTP::Handler_](https://crystal-lang.org/api/latest/HTTP/Handler.html) class.\n\nAmber provides us some default pipes for a number of common tasks. In turn we can customize them as well as create new pipelines to meet our needs.\n\n* **Pipe::Static** serves static assets\n* **Pipe::Logger** logs incoming requests\n* **Pipe::CORS** Handler adds support for Cross Origin Resource Sharing.\n* **Pipe::CSRF** Handler adds support for Cross Site Request Forgery.\n* **Pipe::Error** Handler catches RouteNotFound and returns.\n* **Pipe::Flash** handler provides a mechanism to pass flash message between\n* **Pipe::Session** a plug that sets up session management.\n\nWith our `:web` pipeline now define we can use it in our routes definitions.\n\n{% hint style=\"warning\" %}\nDon't get rid of `StaticController` if you need to serve static assets.\n{% endhint %}\n\n## Sharing Pipelines\n\nIf you have two pipelines that share a lot of the same pipes, you can assign the shared pipes in one block: `pipeline :web, :auth do ... end`\n\nFull example for `config/routes.cr`:\n\n```crystal\nAmber::Server.configure do |app|\n  pipeline :web, :auth do\n    plug Amber::Pipe::Logger.new\n    plug Amber::Pipe::Flash.new\n    plug Amber::Pipe::Session.new\n    plug Amber::Pipe::CSRF.new\n  end\n\n  pipeline :auth do\n    plug Authenticate.new\n  end\n\n  routes :web do\n    get \"/\", HomeController, :static\n  end\n\n  routes :auth do\n    get \"/admin\", AdminController, :dashboard\n  end\nend\n```"}