unit icon indicating copy to clipboard operation
unit copied to clipboard

Chained route actions for a request processing pipeline

Open lcrilly opened this issue 2 years ago • 5 comments

Action chaining enables a route action to be configured as an array of action objects.

{
    "routes": [
        {
            "action": [
                {
                    "pass": "applications/auth_check"
                },
                {
                    "pass": "applications/my_app"
                }
            ]
        }
    ]
}

The client request is passed in sequence to every action in the array provided a 2xx response is returned from each action. Any non-2xx responses are returned to the client.

The initial implementation will support a variety of authentication use cases, including the equivalent of nginx auth_request.

lcrilly avatar Sep 30 '22 14:09 lcrilly

Hello, is there a rough timeline/planned version for this proposal?

aidam38 avatar Sep 11 '23 11:09 aidam38

Not yet but as this is a mayor enhancement we will have a chat about internally and discuss a potential implementation. We will update the timeline asap within the next weeks.

tippexs avatar Sep 11 '23 11:09 tippexs

Nice feature to combine chain of try_files for example to deliver static assets by checking for presence of css.brotli/css.gz/.css files

andypost avatar Sep 11 '23 12:09 andypost

Nice feature to combine chain of try_files for example to deliver static assets by checking for presence of css.brotli/css.gz/.css files

@andypost , this issue (chained routes) is to allow for Unit applications to be pipelined.

For your use case, you can pretty much do this now (as of 1.31) by combining a few features:

  • Match on Accept-Encoding request header
  • Look for the relevant filename extension on disk for compressed versions
  • Add the correct Content-Encoding response header on a match
  • Fallback to a regular share if no match
[
  {
    "match": {
      "headers": {
        "Accept-Encoding": "*gzip*"
      }
    },
    "action": {
      "share": "/www/html${uri}.gz",
      "response_headers": {
        "Content-Encoding": "gzip"
      },
      "fallback": {
        "share": "/www/html$uri"
      }
    }
  },
  {
    "action": {
      "share": "/www/html$uri"
    }
  }
]

This routes configuration works pretty well, with a couple of snags:

  1. Doesn't work for index files (needs an explicit filename in the URI)
  2. The Content-Type response header is missing (because the .gz filename doesn't match a MIME type), although browsers don't mind because they do their own filename-to-type mapping.

lcrilly avatar Sep 11 '23 13:09 lcrilly