Okay yeah, so with the “optimal” solution I was thinking of for your situation, it would look something along these lines:
(Note, it’s import
, not include
)
(cors) {
@origin header Origin {args.0}
header @origin Access-Control-Allow-Origin "{args.0}"
}
myawesomewebsite.com {
root * /srv/public/
file_server
import cors https://member.myawesomewebsite.com
import cors https://customer.myawesomewebsite.com
}
Unfortunately, this won’t work if you import more than once, because the named matcher @origin
will already be defined with the first import, so it will fail on the second import.
The workaround is to disambiguate the named matcher. There’s a couple of ways to do this, both are kinda non-optimal and look wacky.
(cors) {
@origin{args.0} header Origin {args.1}
header @origin{args.0} Access-Control-Allow-Origin "{args.1}"
}
myawesomewebsite.com {
root * /srv/public/
file_server
import cors 1 https://member.myawesomewebsite.com
import cors 2 https://customer.myawesomewebsite.com
}
Here we add another argument to the import and use that as a suffix for the named matcher. This will work, but you need to add another argument on each import.
Another option which is a bit easier to use and read:
(cors) {
@origin{args.0} header Origin {args.0}
header @origin{args.0} Access-Control-Allow-Origin "{args.0}"
}
myawesomewebsite.com {
root * /srv/public/
file_server
import cors https://member.myawesomewebsite.com
import cors https://customer.myawesomewebsite.com
}
This just uses the domain itself as the suffix for the named matcher. This should work just fine because named matchers only end when a space character is encountered. But it’s super dumb to think about the named matcher being originhttps://member.myawesomewebsite.com
internally 
I’ve been brainstorming ways to make this nicer to read but it’s a tricky problem to solve.
Yeah, the Caddyfile is just an adapter that maps to JSON. You can see the adapted JSON for your Caddyfile with the caddy adapt
command. With the API, you can manipulate that JSON on the fly. You’d be adding/removing route
objects. See JSON Config Structure - Caddy Documentation
I dunno. I don’t think it’s necessary frankly, because everything you need can be done without a plugin, as far as I understand.