Procedural Caddyfile blocks

(Matt Holt) #1

I hesitate to suggest this because of its implications on complexity, but it’s on my mind so I want to jot it down before I forget and at least entertain some discussion.

The Caddyfile uses a declarative syntax (ordering of the directives mostly doesn’t matter). Sometimes this is problematic, like when people want redir to happen before rewrite.

Now… again, I hesitate to suggest this, because it borderlines turns the Caddyfile into some sort of scripting language which I really want to avoid because complexity. But would it be useful in general (not just the redir/rewrite case) to be able to define a block where directives are executed in the order they appear? For example:

example.com

root /www/example.com
gzip
ordered {
    redir ...
    rewrite ...
    redir ...
}
templates

Or something like that. The directives in the ordered block are executed in the order they are shown here.

Used improperly, this could seriously break some configurations. It would be a sharp tool. Should we even entertain this possibility?

(Matthew Fay) #2

On principle, I’m FOR making more tools available to the user.

That said, I don’t see a use case for it right now. The only ordering question that enters my mind is redir vs rewrite, and I’d rather see them combined into one directive than have to specify their ordering. At the moment, I find the static order works well.

(Guilherme Bernal) #3

The particular problem with the order of redir and rewrite could be solved with the proposal of making the redir behavior a property of rewrite (see https://github.com/mholt/caddy/pull/1750). That with the fact that all rewrites will be processed in the order they appear should give all power needed for this.

A much more useful change to Caddyfile’s syntax would be to create “locations”, kind of similar with nginx. Instead of every single directive having to process path restrictions in it own way (lots and lots of duplication and poor implementations), have a primitive match location, including the conditionals that rewrite has. This also keeps the syntax fully declarative.

1 Like
(Matt Holt) #4

You’re probably right.

I’m actually playing with something like this in a branch, but with much more powerful matching features than what nginx has. Stay tuned…

1 Like
(Matt Holt) closed #5

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.