Hi. How do I add environment variables in the code. For example I want to get data from .env?
Not sure what you mean by “code”, but if you’re asking about Caddyfile:
Do I need to modify from bash? As when I do it according to the documentation it throws error
No idea what you’re doing. Could you share your configuration and the exact error you’re getting?
For example I have this .env variable DOMAIN_NAME = *.test *.smth . In CADDYFILE now I have hardcoded values path *.test *.smth
which works now but I need to make it path {$DOMAIN_NAME}
which doesn’t work for now.
It’s really hard to help without seeing the actual configuration you’re running. At this point, it’s all guesswork, and honestly, it’s getting a bit tiring trying to figure out what you’re actually doing.
If you can share a concrete example or config, I’ll be happy to take another look. Otherwise, this might be my last reply on this one.
Here’s an example Caddyfile
:
{
http_port 8080
}
{$DOMAIN_NAME:localhost:8080} {
respond "{host} is alive!"
}
http:// {
respond "default is alive!"
}
Running it with:
$ DOMAIN_NAME="*.test:8080, *.smth:8080" caddy run --config Caddyfile
Produces this autosave.json
:
$ cat autosave.json | jq .
{
"apps": {
"http": {
"http_port": 8080,
"servers": {
"srv0": {
"listen": [
":8080"
],
"routes": [
{
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"body": "{http.request.host} is alive!",
"handler": "static_response"
}
]
}
]
}
],
"match": [
{
"host": [
"*.test",
"*.smth"
]
}
],
"terminal": true
},
{
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"body": "default is alive!",
"handler": "static_response"
}
]
}
]
}
],
"terminal": true
}
]
}
}
}
}
}
And the test results:
$ curl http://127.0.0.1:8080 -H 'Host: foo.test'
foo.test is alive!
$ curl http://127.0.0.1:8080 -H 'Host: bar.smth'
bar.smth is alive!
curl http://127.0.0.1:8080 -H 'Host: example.com'
default is alive!
So yep, the environment variable gets picked up by Caddy just fine.