I’d like to make a custom plugin that does something kind of like this contrived example:
Examines the POST body
Does some sort of dynamic lookup based on the POST body
Uses the value of the lookup to determine the URL to write to.
Here’s a very contrived example:
Requests come in as POST to /foo w/ JSON payload {"destination": 42, ...other json.. }
Plugin intercepts this as lookups up destination 42 in it’s backend
In this case let’s say looking up destination 42 returns /bar
Request is rewritten to /bar keeping everything else about the request intact.
I think the route I want to look at is writing a custom middleware that acts like http.handlers.rewrite but before I dig too much deeper figured I’d post here and see if anyone knows of any examples that do something kind of similar.
You can just define a variable in your module (see the var handler for how that works, and read its source code), then you can just use Caddy’s existing rewrite handler like this:
func (v *RequestBodyDestinationVar) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
// TODO: actually parse the payload and construct a dynamic route
route := "/foo/bar"
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
repl.Set("request_body_destination", route)
// set a response header just so I know this is working
w.Header().Set("X-Debug-Destination", route)
return next.ServeHTTP(w, r)
}