I want to modify the request and response in a flexible way, i.e. also replace it completely. I have to deal with JSON and YAML modifications. I found the GitHub - caddyserver/replace-response: Caddy module that performs replacements in response bodies module but this is only useful for regex replacements. I would like to include my own logic. Is the best and only way currently to write my own module?
I have written a little test plugin, but now I don’t know how to apply it. I’m extending a caddyhttp.MiddlewareHandler but how to integrate it in my existing file? My logreqresp should act as directive.
The referenced documentation is describing how to program a gizmo directive, but does not give a Caddyfile example. From what I see there it seems that a module is completely independent, does not inherit any settings and I cannot use to power of any other existing directives and modules? Is the standard approach to parse the Caddyfile file in UnmarshalCaddyfile and re-implement manually all of bind, reverse_proxy, log etc. behavior? This seems to be a lot of work.
I want to keep the bind, reverse_proxy, log etc directives and just need an additional behavior. What is the best way to extend caddy, i.e. adding a new directive by keeping what I is already provided?
You call RegisterDirective to register your directive so that your UnmarshalCaddyfile gets called when the Caddyfile parser encounters your directive in your config.
is exactly how your config will look.
You’ll need to add the order global option though to set an order for your directive (plugins don’t get an order automatically, only built-in ones do; see Caddyfile Directives — Caddy Documentation)
Also is that actually your entire config? If it is, then that error message doesn’t make sense. The error says order was on line 10, but it’s on line 7 in what you pasted.
Right, the error line was incorrect, it must be line 7. My other error was that I was using caddy run instead of xcaddy run and then my work in progress plugin could not be found.
Error: adapting config using caddyfile: parsing caddyfile tokens for ‘logreqresp’: wrong argument count or unexpected line ending after ‘logreqresp’, at Caddyfile:14
2024/01/02 14:54:28 [ERROR] exit status 1
This is trying to parse something like logreqresp <output> (arg on the same line as the directive) not a block with { }. If you wanted to parse a block you’d need to use d.NextBlock() and loop through the lines.
Is this plugin of general use if it works? Then I would publish it. I have searched if something like this exists, but right now it does not seem to be the case.