How to enable the use of a module by a global directive?

Please, consider the code here: caddy-pixbooster/module.go at global-directive · PixyBlue/caddy-pixbooster · GitHub

Maybe I’m missing something obvious, I apologize if so, but I can’t figure it out.

My module works quite well when used in site configuration with configuration like:

mysite {
   route {
      pixbooster
   }
}

I want to provide a global option:

{
   pixbooster
}

But it does not work. There is no error on compilation, nor in the logs when running. ServeHTTP is simply never called when using the global directive.

The configuration should handle sub-directives that are the same in both cases (global and site configurations). That’s why parseCaddyfile() and parseCaddyfileGlobalOption() are so similar. But I suspect I misunderstood something with the latest.

Why though? A global option is something that does not fit in site configurations. Thus you can’t define HTTP routes in global options. Global options are for things like configuring how the server works, or changing the administration endpoint, or altering logging.

Is it not a way to enable something globally ? Should not such module able to be enabled globally like the module cache ?

1 Like

There’s two different concepts here.

  • HTTP handler modules: these run in response to a request, and are configured as route middleware inside a site block.
  • App modules: these are “global”, in that they live outside the request-response lifecycle.

If you need to keep a worker running, or keep a database connection alive, then it can make sense to have both an App module and an HTTP handler module which communicate with eachother.

There’s no way to have “globally configured HTTP routes”.

The cache package has both, because the app keeps the connection to the cache storage/database open, and the cache HTTP handler is what intercepts HTTP responses to write them to cache.

Hope that clarifies.

2 Likes

Thank you, Francis. As I expected, I missed something obvious, but it was in the overall design, not at the code level. You made things clearer.
And, BTW, thanks @matt for your first answer.

1 Like