-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Discussion around using variables in Caddyfile #4650
Comments
(Your original post in the website repo was the start of a great wiki entry you could submit!) Variables can already be declared with the (This is how the Other than the |
Thanks @mholt! This makes me laugh, I will drag you through my process... (I am sharing it because, most of my attempts to learn something new about caddy go like this) I read vars HTTP handler module. Thought bubble:
Try 1Is it a global options block?
nope: Try 2Maybe it should be a vars block?
nope: Try 3Maybe put it in the Site block?
nope: Try 4Maybe I need to put it in a vars block inside the global block?
nope: Try 5I know maybe it is a vars block in the Site block!
grrr: Oh yeah, I get most questions answered by going through the forum.errr, search for "vars" turns up some references but I cannot find vars on most of the pages. Does the module need to be installed?The module claims it is standard so it should be in the executable... No luck there. How do I reference a module? I cannot find anything on it in the docs. I included the tls godaddy module and that extends the Wait look for http.vars on the forum!Found this, a clue! Wait a minute, those are JSON files. What did @mholt say above?
D'oh! This is a JSON only feature? let me try it
Works like a charm! I guess it is not available in a Observations
What the heck is that?
erg: 2 hours I will never get back |
@brucejo75 Well, thank you for writing all that out. 😅 A very insightful read. A few thoughts: As you discovered, these are the JSON config docs. Caddy's native configuration is JSON, and we emphasize this from the Getting Started page. In other words, this is not the area of the site describing the Caddyfile config. I could have been clearer in my reply above, where I tried to contrast that with the Caddyfile (when I said "I'm not sure we directly expose the vars module to the Caddyfile"), but upon re-reading it, I realize why that wasn't clear and I'm sorry for the confusion. I was just saying that there is a module that lets you define variables. It just has to be done with JSON. Also sorry for the lost time. I think the module documentation is very clear and concise:
Hence, the two empty strings: We could probably use examples; it's always hard to draw the line between reference documentation and examples though. I like keeping them separate to maintain good organization. For examples, we have our wiki but not nearly enough contributors.
No one has asked for it before now. 🤷♂️ I guess we could go ahead and add it. Any thoughts @francislavoie ? Again, I really appreciate your time here, and apologize for the confusion. This is just a feature that not many people have used or asked about. |
Yeah we should add the Caddyfile directive, it was an oversight. No harm in it existing. It's really not that useful on its own though, IMO. |
Real quick, before we do add a |
Yes but it's always a 3-liner and it's kinda awkward I guess:
Something like that might work. But obviously it will still cause a string comparison on every request. |
Ah, clever. That is kinda awkward, you're right. What about a special-case syntax for the map directive that basically accepts |
I'm not a big fan of overloading directives that much. It's cheap to just add |
It's cheap, but IMO it makes more sense to have |
Am I correct in assuming that the I am requesting variables that get substituted before parsing not during runtime.
But a real objection is the documentation. Guys, this stuff is hard to connect the dots on... (And it is a shame, because the product is EXCELLENT!) Change the top portion to lay out the entire JSON object:
These simple changes to be more explicit would have saved me 2 hours. I am happy to take a shot at fixing these things as I run across them and submitting PRs. In my limited experience with |
Doesn't exactly set placeholders, it sets "vars" in the request context, which can be used with a placeholder (slight distinction). The
That's exactly how they work right now, in the Caddyfile Defining new variables though, would be a lot of added complexity I think, and I don't think that's warranted here. Just set your variables ahead of time. Or use snippets to reuse some common bits of config https://caddyserver.com/docs/caddyfile/concepts#snippets
The JSON docs are auto-generated from the code's comments and Go code structure. With the way it's done right now, there's currently no way to control what those string values end up containing in the generated output. But noted, that could be a future addition to the JSON doc generator, but it would also require adding something to the code to fill that in.
I don't think this makes sense. If you're in the JSON docs, you shouldn't assume at all that any of it is available in the Caddyfile. The Caddyfile adapter outputs a JSON config, but JSON can't go back to Caddyfile. It's a one-way relationship. |
This thread now has 3 separate concepts:
|
Ah, I see. You are requesting a programmable Caddyfile. I will have to read that link when I am not mobile, but I also resist this notion, instinctively. There is some history here... when I was designing Caddy 2 I contemplated and experimented and debated for months whether the config should be declarative or imperative. We obviously settled on declarative and there are a lot of reasons for this -- pros and cons for sure -- but mostly pros. Now, it turns out you can get in trouble mixing imperative and declarative. Ultimately, it boils down to you being able to start with a parameterized document and evaluate it into a declarative document, but you can't go the other way around, and you have to be very careful when combining them. So, in other words, the Caddyfile (or ideally, JSON) document you give Caddy should be the output of your parameterization, not the input. I may respond to this more once I get back into the office, but I wanted to dump a few initial thoughts for now. I think there is some good discussion here and even if it's not the point, I now want to enhance the map directive, improve documentation, and consider other elements you have brought up. (So I'm leaning toward the first of your "possible responses" but I need to evaluate things more.) |
@mholt , @francislavoie, following up on @mholt's comment. I took some of my learnings and put together a gist: Variables in Caddyfiles If you would not mind examining for bonehead errors or glaring omissions I would appreciate it, I am still pretty green in the world of Caddyfile authoring. I will post on the Wiki after your review. Thanks! |
@brucejo75 Great, I left some comments there. Am also drafting up a new commit to enhance the |
I actually ended up adding the |
Thoughts/reactions from a green user...: Will having a Can more than one variable be set with the |
@brucejo75 I don't think so, we have the I think it's valid to say the directive should be |
Yes:
or for just one:
|
@brucejo75 Does that |
If it can do this, then yes! 👍 👍
Number substitution would be a bonus, just curious if that would work Note: I think it may be a bit of an overkill. A simple pre-processor (similar to the way environment variables work) could probably accomplish all that I really want. So no placeholder lookup at runtime. |
That should work now, yes. (In a |
Right now, |
I can reorder that, still pretty easy to do. 🤷♂️ Or maybe we could make it work like And the vars might also need |
That's not a thing, we still sort |
Question from my ignorant point of view: Does the I had assumed that there would be a step to create an internal representation of the map and then each runtime invocation of the |
Yes, it's a request handler directive, so it runs for each request.
Yeah, it'll be basically instantaneous. |
To add to what Francis said, it's not recreating the table with each route evaluation, it's only performing a table lookup at each route evaluation, and ignores the rest of the table that's not being looked up. So it's really quite efficient. Since I think your needs have been addressed in the latest changes, I will close the issue now, but feel free to continue discussion if necessary. (Again, you might have to use |
I was reading the variable assignment thread in the forum essentially outlining that you could assign a placeholder using
map
orimport
. The author of the post specifically asked about being able to assignnon-environmental
variables (e.g. placeholders).It occurred to me that "Environment Variables" could be more generalized to "Variables" if you require a declaration before usage...
I would like to do something like this:
I can make this work by assigning an environment variable before running
caddy
, but oftentimes it would be useful to declare in theCaddyfile
(e.g. use of more than a few variables).Feature Request
The text was updated successfully, but these errors were encountered: