Using Caddy as library for go package?

Maybe we could add an exported function to the httpserver package to allow plugins to dynamically add their directive, but that kind of scares me since we lose the certainty of the absolute ordering, which is important.

Iā€™ve been digging into the source following your link about the directives list. To verify my understanding: at this time, there is no way to add a middleware to the directives list without modifying the source of the httpserver package - is that correct?

(I understand the complexity / concern about the absolute ordering. Itā€™s a hard problem. Iā€™ll see what I can do.)

Thatā€™s correct. It has to know in which order to execute the directives. :+1:

Maybe require some kind of priority/ordering value to go with it?

Not sure what you mean?

What exactly are the ordering requirements?

The order is whatever makes logical sense. For example, gzip runs before errors because the error pages must be gzipped. Basicauth runs before any middleware that would serve assets so that it can protect them. Itā€™s the same reason lines of code have to be written in a certain order; it canā€™t be arbitrary.

If we could get rid of absolute ordering thatā€™d be awesome but I donā€™t know of a way.

So some plugins have dependencies?
How about have a struct for registration that includes an array of dependencies or something.

type PluginRegistration struct {
    SetupFunc func
    // Other funcs needed
    Dependencies []string
}

caddy.RegisterPlugin(PluginRegistration{
    SetupFunc: errorsSetup,
    Dependencies: []string{"gzip"},
})

Please ignore my syntax errors :slight_smile:

Not dependencies per-se, but consider this example. Gzip works like this:

  1. Wraps the http.ResponseWriter with a gzip.Writer
  2. Pass the request to the next handler up the chain
  3. Gzip checks for errors in case no error handling was chained in

If gzip is executed after errors or markdown, for example, then error pages or markdown pages would not be gzipped because it would happen later in the chain. But it would never reach that part of the chain because once the request is handled, the chain is terminated.