Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

httpcaddyfile: Add RegisterDirectiveOrder function for plugin authors #5865

Merged
merged 4 commits into from
Mar 6, 2024

Conversation

francislavoie
Copy link
Member

We (@mholt @dunglas and I) were talking in Slack about the ergonomics of using the Caddyfile for end-users of custom builds of Caddy which may include many handler plugins. Forcing users to specify order or wrap things in route causes some friction that would be nice to smooth out.

The reason we never did this in the past is because we didn't want to allow plugins to depend upon eachother, because we can't guarantee the order in which plugins are loaded. We also didn't want the ordering to become unreliable when the order of directives in the standard distribution changes.

We can mitigate those factors by doing two things:

  • Only allow plugins to register an order for their directive in relation to a directive in Caddy's standard distribution (we keep a defaultDirectiveOrder which doesn't change, to ensure this, and modify directiveOrder accordingly)
  • Only allow plugins to order "before" and "after" another directive (and no "first" or "last" like the order global option allows) to reduce the chance of ordering that doesn't make sense.

The API looks like this:

httpcaddyfile.RegisterHandlerDirective("my_directive", parseDirective)
httpcaddyfile.RegisterDirectiveOrder("my_directive", "after", "redir") // new

Or course, there's still a slight problem if two plugins try to both order the same way in relation to a standard directive (e.g. both are "before headers") because their order will not be guaranteed. There are some complicated ways we could mitigate this though, for example we could invalidate the ordering for both directives since they're in conflict, forcing the user to specify their own order for both. But ultimately I think this it probably not an immediate need, and we can expand upon that later.

@francislavoie francislavoie added the feature ⚙️ New feature or request label Oct 7, 2023
@francislavoie francislavoie added this to the v2.8.0 milestone Jan 14, 2024
Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is quite a limited function but it sounds like some people will still find it useful -- we can try it! But I do want to mark it as experimental for now.

caddyconfig/httpcaddyfile/directives.go Show resolved Hide resolved
@francislavoie francislavoie enabled auto-merge (squash) March 6, 2024 19:36
@francislavoie francislavoie merged commit 258d906 into master Mar 6, 2024
25 checks passed
@francislavoie francislavoie deleted the plugin-directive-order branch March 6, 2024 19:41
dev-polymer pushed a commit to dev-polymer/caddy that referenced this pull request Mar 13, 2024
…rs (caddyserver#5865)

* httpcaddyfile: Add `RegisterDirectiveOrder` function for plugin authors

* Set up Positional enum

* Linter doesn't like a switch on an enum with default

* Update caddyconfig/httpcaddyfile/directives.go

Co-authored-by: Matt Holt <[email protected]>

---------

Co-authored-by: Matt Holt <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature ⚙️ New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants