I have an idea for a plugin that I’ve been trying to write, but I may be missing some theoretical understanding of Caddy internals (or it may be something completely impossible or undesirable given the current architecture). Please enlighten me.
I want to write a plugin that behaves similar to supervisor/exec: you pass a command line, it executes it and keeps the child process alive. The main difference is that it also sets an env var with an URL. The child process must POST to that URL at least once with a JSON that is a very simplified configuration (think like what php_fastcgi sets: index, files that should be proxied to it, etc). The plugin reads this config and recreates the config for this route with the proper Caddy routing.
This would work as sort of a meta frankenPHP / modern FastCGI with minimal configuration for the user, as the program is responsible for telling Candy what it can/should handle. Like “I’m a generic Markdown Server that caches/executes files ending in .md, only send those requests to me”. I want those decision to be dynamically changeable by the client process.
My initial attempt at implementing this: I created an empty HandlerDirective Middleware (I tried to do with Directive instead, but I wasn’t able to keep the configuration around long enough to have a ctx). I set up a AdminRouter (which would be the URL the child process would send to). I thought about using the Admin API itself to update the config.
The problem is a logical one: the initial config eventually gets replaces with the new one (as intended), which means that the only place where the configuration data would be available is on the old object (I then need to UsagePool to keep them around, I assume). This way the original Caddyfile works almost as a bootstrap step, that then gets replaced by the “real configuration” that each child process built. This seems hacky and brittle.
Here’s my current code: GitHub - fserb/substrate
Is what I’m trying to do even possible? Does this direction make any sense? Is there a better way to do this?